Click here to Skip to main content
13,894,465 members
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 19 Mar 2009
Licenced CPOL

Helper Class for Resizable MFC Dialogs

, 19 Mar 2009
Rate this:
Please Sign up or sign in to vote.
This class takes care of the child window placement when you resize an MFC dialog. Also stores the size in the Registry for the next call.


Hi, this is my first article here, have mercy.

This article describes a very easy way to take care of resizable dialogs in MFC applications, using Visual Studio 6 to 2005 (I don't know about 2008 or newer versions, yet).


I like MFC. Believe it or not, I do. But, I dislike the way you have to be careful when you want a resizable dialog. You have to program the position for each child window manually into the OnSize method of every dialog you program. So, I took an afternoon investigating how this could be simplified.

So, starting from what I know about a CDialog, I can get the current window size (that you set in the resource editor) in the OnInitialUpdate() method of the dialog. Store that, so the next time the dialog gets resized, you can make sure it won't be smaller than this, in the OnGetMinMaxInfo() method.

void Init(CDialog* pDialog);

Easy, see. You just pass the "this" pointer from your dialog.

I can also get the current position of the child windows, so I can use that information later to keep the distance to the left/right and top/bottom border when the dialog gets resized.

By default, all child windows stick their position to the top, left hand corner automatically if you don't move them. If, however, you want to have them move, use this function to give the resizer a rule to move your child:

void AddCtrl(int id, int align=DT_LEFT|DT_TOP, 
             BOOL keep_dx=TRUE, BOOL keep_dy=TRUE);

For the alignment flags, I chose the DrawText API function constants, since they (on contrary to the TA_ constants for the TextOut API) have a DT_VCENTER constant. So, yes, you can have the child windows align to the center of the dialog, too.

The flags "keep_dx" and "keep_dy" tell the resize helper whether you allow to stretch that child window, or keep the size. For a button, you mostly want to keep the size, whereas you want the main edit control stretched, most likely.

This is done here:

// ---------------------------------------------------
// OnSize handling of the Child Ctrls
// ---------------------------------------------------
void GFResizeDialogHelper::OnSize(int cx, int cy)
{ = cx; = cy;

    for(int i=0; i<(int)m_Ctrls.size(); ++i)
        CTRL_ALIGN& ctrl = m_Ctrls[i];

        int x =;
        int width =;
            width += (cx -;
            // new size increment = dialog increment
        if(ctrl.align & DT_RIGHT)
            x = - width +;
        else if(ctrl.align & DT_CENTER)
            // ctrl center
            int xcenter = (;
            // dialog offset
            xcenter += (;
            x = xcenter-width/2;
        int y =;
        int height =;
        if(ctrl.align & DT_BOTTOM)
            // y +=;
            y = - height +;
        else if(ctrl.align & DT_VCENTER)
            // ctrl center
            int ycenter = ( +;
            // dialog offset
            ycenter += (;
            y = ycenter - height/2;
        width = max(width, 10);
        height = max(height, 10);
        x = max(0,x);
        y = max(0,y);

        m_pDialog->GetDlgItem(ctrl.idWnd)->MoveWindow(x,y,width, height, FALSE);
        // ::MoveWindow(ctrl.pWnd->GetSafeHwnd(), x,y,width, height, TRUE);
    if(m_pDialog && m_pDialog->GetSafeHwnd() && 

Next, I found out that every dialog has an IDD constant that is the resource ID of the dialog. Fine! We can use that ID for storing the dialog's size at closing time in the Registry, I thought, but then I found out that: m_pDialog->GetRuntimeClass()->m_lpszClassName returns a readable string for the dialog class' name, and I took that one.

Using the code

The code is a piece of cake to use. You just include the two files from above in your project. Then, open the mydialog.h file and add these two lines:

include "gfresizehelper.h" // include header

// and add a member variable to your CDialog derived class:
GFResizeDialogHelper    m_Resizer;

The next step is to tell the helper about your child windows and how you want them to behave when resized:

BOOL CMyDialog::OnInitDialog()
    // ...
    m_Resizer.AddCtrl(IDOK, DT_BOTTOM|DT_RIGHT);
    // ... more controls
    // optionally - read and write position and size to registy
// Usage of AddCtrl(int id, int align, BOOL keep_dx, BOOL keep_dy)
int id:
    The ID of the control (IDC_...)
int align:
    The alignment of the control. If you align it
        DT_LEFT,    the left-coordinate will stay where it is.
        DT_CENTER,    the center of the control will stay where it was,
                    relative to the dialog.
        DT_RIGHT,    the right border of the control will keep the distance
                    to the right border of the dialog.
        DT_TOP,        the top position will stay
        DT_VCENTER,    the vertical center will stay where it was relative
                    to the dialog.
        DT_BOTTOM,    the controll will keep the distance to the bottom of
                    the dialog
BOOL keep_dx
    Should the control keep its width?
BOOL keep_dy
    Should the control keep its height?

The call to ReadWriteToRegistry() will try to get the last stored size and position from the Registry, and will store the size from the last WM_SIZE message when the class destructs, which is usually after a OnOK() or OnCancel().

Then, use the application wizard or the dialog properties, get a message handler for OnSize (the WM_SIZE message), and tell the resize helper to take care of your children.

void CMyDialog::OnSize(UINT nType, int cx, int cy)
    CDialog::OnSize(nType, cx, cy);

Finally, you can handle the WM_GETMINMAXINFO message to tell the program not to make the window smaller than you designed it in the dialog editor:

void CMyDialog::OnGetMinMaxInfo(MINMAXINFO* lpMMI)

I hope this article is of some use for someone. I use this class a lot now, and I really like the new look of my programs with the resizable dialogs.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gernot Frisch
Software Developer (Senior) Dream Design Entertainment
Germany Germany
I was a bricklayer. It was cold and rainy, so I went to school. Studied civil engineering, made some games in C/C++, got a nice job where I can code 24/7, mostly C++/MFC.
My other project is GLBasic, a very easy to learn programming language, that compiles for various platforms.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Blur-Blur31-Jul-13 17:06
memberBlur-Blur31-Jul-13 17:06 
GeneralA simpler way Pin
Mizan Rahman6-Dec-10 4:25
memberMizan Rahman6-Dec-10 4:25 
Questiongetting CChildView resized ? Pin
George Kofman16-Jun-09 9:00
memberGeorge Kofman16-Jun-09 9:00 
QuestionHow about Paolo Messina's ResizableLib? Pin
Victor Nijegorodov24-Mar-09 0:24
memberVictor Nijegorodov24-Mar-09 0:24 
AnswerRe: How about Paolo Messina's ResizableLib? Pin
Gernot Frisch24-Mar-09 0:53
memberGernot Frisch24-Mar-09 0:53 
GeneralRe: How about Paolo Messina's ResizableLib? Pin
Victor Nijegorodov24-Mar-09 1:13
memberVictor Nijegorodov24-Mar-09 1:13 
GeneralRe: How about Paolo Messina's ResizableLib? Pin
SBJ25-Mar-09 6:41
memberSBJ25-Mar-09 6:41 
GeneralBad CodeProject member Pin
k7776-Mar-10 12:06
memberk7776-Mar-10 12:06 
QuestionDoes use in CFormView? Pin
chenyu220286320-Mar-09 21:36
memberchenyu220286320-Mar-09 21:36 
AnswerRe: Does use in CFormView? Pin
Gernot Frisch22-Mar-09 4:22
memberGernot Frisch22-Mar-09 4:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190306.1 | Last Updated 19 Mar 2009
Article Copyright 2009 by Gernot Frisch
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid