Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OnGetMinMaxInfo - how to set minimum size for dialog

895 views
Skip to first unread message

Martin Pospisil

unread,
Jan 15, 2002, 3:20:56 AM1/15/02
to
Hi,

I need to controll minumum size of dilalog. This code works very strange.
Could you temm me what is wrong ?

Thanks

Martin

void CSearchDlg::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
// TODO: Add your message handler code here and/or call default
RECT rect;
GetWindowRect( &rect );
lpMMI->ptMinTrackSize.x = rect.left + SD_MINWIDTH;
lpMMI->ptMinTrackSize.y = rect.top + SD_MINHEIGHT;
CWnd::OnGetMinMaxInfo(lpMMI);
}


Joseph M. Newcomer

unread,
Jan 15, 2002, 5:49:49 AM1/15/02
to
The easiest way is to draw a CStatic frame on the dialog to represent the minimum size you
want, change its id from IDC_STATIC to IDC_MINMAX or other useful name, create a control
variable to reference it, and use code of the form

CWnd::OnGetMinMaxInfo(lpMMI);
CRect r;
c_MinMax.GetWindowRect(&r);
lpMMI->ptMinTrackSize = CPoint(t.Width(), r.Height());

Note that calling the CWnd::OnGetMinMaxInfo version afterwards does no good, it likely
will undo the changes you have made. The only reason to call it at all is because it will
set other values you may not care about. What is odd about your code is that it sets the
minimum tracking size to be progressively larger than the actual window itself.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

ML

unread,
Jan 15, 2002, 9:02:19 AM1/15/02
to
Hi,

GetWindowRect() return the window's rect in screen coordinates. But, the
struct MINMAXINFO uses relative coordinates.

The following should do the job :

void CSearchDlg::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
// TODO: Add your message handler code here and/or call default

lpMMI->ptMinTrackSize.x = SD_MINWIDTH;
lpMMI->ptMinTrackSize.y = SD_MINHEIGHT;
CWnd::OnGetMinMaxInfo(lpMMI);
}


ML


"Martin Pospisil" <pospi...@volny.cz> a écrit dans le message de news:
#mCzB5ZnBHA.1716@tkmsftngp02...

Ruben Jönsson

unread,
Jan 15, 2002, 9:24:46 AM1/15/02
to
I use a temporary dialog class that creates an invisible dialog using the template string
of the dialog i want to know the size of. In OnInitDialog() of this dialog the size is
retrieved using GetWindowRect(), saving it in a member variable of the temporary dialog
class. The dialog window itself is destroyed before OnInitDialog() returns. When the
create function returns the member variable for the size can be examined. There is a
global access function that creates the desired dialog and returns its size.

If I remember correct this is taken from the book "The MFC Answer Book" by Eugene Kaine -
an excellent book, by the way.

The .h file:

#if !defined(AFX_TEMPDLG_H__438D2240_D7F1_11D4_B4DA_A2E73A688D1D__INCLUDED_)
#define AFX_TEMPDLG_H__438D2240_D7F1_11D4_B4DA_A2E73A688D1D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// TempDlg.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CTempDlg dialog

CSize GetDialogSize(LPCTSTR pszTemplName,CWnd* pParent);

class CTempDlg : public CDialog
{
// Construction
public:
CRect m_rcDlg;
CTempDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CTempDlg)
enum { IDD = 0 };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA


// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTempDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:

// Generated message map functions
//{{AFX_MSG(CTempDlg)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the
previous line.

#endif // !defined(AFX_TEMPDLG_H__438D2240_D7F1_11D4_B4DA_A2E73A688D1D__INCLUDED_)

The .cpp file:

// TempDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TempDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CSize GetDialogSize(LPCTSTR pszTemplName,CWnd* pParent)
{
CTempDlg dlg;
dlg.Create(pszTemplName, pParent);
return dlg.m_rcDlg.Size();
}

/////////////////////////////////////////////////////////////////////////////
// CTempDlg dialog


CTempDlg::CTempDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTempDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTempDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}


void CTempDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTempDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTempDlg, CDialog)
//{{AFX_MSG_MAP(CTempDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTempDlg message handlers

BOOL CTempDlg::OnInitDialog()
{
GetWindowRect(&m_rcDlg);
m_rcDlg -= m_rcDlg.TopLeft();
EndDialog(0);
return TRUE;
}


On Tue, 15 Jan 2002 09:20:56 +0100, "Martin Pospisil" <pospi...@volny.cz> wrote:

Ruben

Joseph M. Newcomer

unread,
Jan 15, 2002, 1:53:53 PM1/15/02
to
Why do you need an invisible dialog? This seems to be a serious waste of effort. What do
you do with the information? What are you really trying to accomplish (I've never needed
to create an invisible dialog for any purpose, least of all for something as trivial as
getting its size!)
joe

Joseph M. Newcomer [MVP]

Ruben Jönsson

unread,
Jan 15, 2002, 4:13:27 PM1/15/02
to
I use it whenever I need to know the size of the dialog template as it was created in the
resource editor. The problem with the size it has in the resource is that it is in dialog
units. To find out the screen size, simply create it (invisible so it wont be seen) and
get the size in screen coordinates and the destroy it. In other words, let Windows tell
you it's size from the template in the resource.

Useful when you want to restrict sizing of a dialog or form view (with OnGetMinMaxInfo) so
that it will never get smaller than it's template size.

Have you any better suggestion to get the size in screen coordinates of the dialog from
the resource?

Ruben

Joseph M. Newcomer

unread,
Jan 15, 2002, 5:05:05 PM1/15/02
to
Why not do this in OnInitDialog when the dialog is created? Why create it twice? I have
done this many times in making CFormViews that won't resize, but I've never needed to
create the dialog to do it! The simplest way is to create an invisible frame as large as
the dialog, and use that as the method for restricting OnGetMinMaxInfo to that. I've
written this code three times in the last 48 hours and it is trivial. It would never have
occurred to me to use such a convoluted solution for a simple problem.
joe

Ruben Jönsson

unread,
Jan 16, 2002, 3:21:34 AM1/16/02
to
One correction: The code is actually taken from MSJ, september 1998, C++ Q&A by Paul
DiLascia.

When I have made SDI apps with form views I usually let the user expand the frame but not
reduce it to a size smaller than the template size, in order to not show the scroll bars.
I also save the position, size and style (maximized or normal) of the frame window when
the app is closed and use this info to position and size the frame in PreCreateWindow() of
the main frame - the style I set in InitInstance() of the app class.

The form view calls ResizeParentToFit() in OnInitialUpdate() by default. I remove this
because otherwise my saved settings for the frame, done in pre create window, is lost.

To find out the minimal size of the form view (the client area of the frame window) I
simply call the GetDialogSize() function once in OnIitialUpdate() of the form and save
this as a member variable. GetDialogSize() creates an invisible dialog from the given
template, gets the size and immediately destroys the dialog.
m_sizeInitial=GetDialogSize(MAKEINTRESOURCE(IDTemplate_For_This_View),this); - can it get
any easier? For me, that is - windows may have to put a little bit more effort in here,
creating the dialog and all, but I don't care.

The mainframe asks the form view for it's minimum size in OnGetMinMaxInfo() and adds size
for menu, statusbar, toolbars, border and so on, and passes this on to the ptMinTrackSize
in the MINMAXINFO structure.

Is there anywhere I can find the size of the form view as created in the resource editor,
with this method, without calculating it from the dialog units in the resource or creating
it as I have done?

Why is this any different from creating the invisible frame as you do? I assume you also
have to add the size of status bar, menu bar, tool bars and whatever else you have in the
frame unless you create them also. Or have I missed something?

Well, everything is simple when you know how to do it. I remember that I spent several
days before I got the result I wanted, when I just started out to learn MFC, a year and a
half ago.

Ruben

Ruben Jönsson

unread,
Jan 16, 2002, 9:10:03 AM1/16/02
to
To answer my own question, see below...

On Wed, 16 Jan 2002 09:21:34 +0100, Ruben Jönsson <ru...@pp.sbbs.se> wrote:

>One correction: The code is actually taken from MSJ, september 1998, C++ Q&A by Paul
>DiLascia.
>
>When I have made SDI apps with form views I usually let the user expand the frame but not
>reduce it to a size smaller than the template size, in order to not show the scroll bars.
>I also save the position, size and style (maximized or normal) of the frame window when
>the app is closed and use this info to position and size the frame in PreCreateWindow() of
>the main frame - the style I set in InitInstance() of the app class.
>
>The form view calls ResizeParentToFit() in OnInitialUpdate() by default. I remove this
>because otherwise my saved settings for the frame, done in pre create window, is lost.
>
>To find out the minimal size of the form view (the client area of the frame window) I
>simply call the GetDialogSize() function once in OnIitialUpdate() of the form and save
>this as a member variable. GetDialogSize() creates an invisible dialog from the given
>template, gets the size and immediately destroys the dialog.
>m_sizeInitial=GetDialogSize(MAKEINTRESOURCE(IDTemplate_For_This_View),this); - can it get
>any easier? For me, that is - windows may have to put a little bit more effort in here,
>creating the dialog and all, but I don't care.
>
>The mainframe asks the form view for it's minimum size in OnGetMinMaxInfo() and adds size
>for menu, statusbar, toolbars, border and so on, and passes this on to the ptMinTrackSize
>in the MINMAXINFO structure.
>
>Is there anywhere I can find the size of the form view as created in the resource editor,
>with this method, without calculating it from the dialog units in the resource or creating
>it as I have done?

Thinking about it, the scroll view (which the form view is derived from) should know about
the size of the form as created in the resource editor, since it has to know when the
scroll bars should be visible.

GetTotalSize() in OnInitialUpdate() returns this size in pixels. No need for a static
invisible control, an invisible dialog or an invisible frame any more. For a formview,
anyway.

>
>Why is this any different from creating the invisible frame as you do? I assume you also
>have to add the size of status bar, menu bar, tool bars and whatever else you have in the
>frame unless you create them also. Or have I missed something?
>
>Well, everything is simple when you know how to do it. I remember that I spent several
>days before I got the result I wanted, when I just started out to learn MFC, a year and a
>half ago.
>
>Ruben
>

Ruben

0 new messages