Click here to Skip to main content
12,302,330 members (24,058 online)
Click here to Skip to main content
Add your own
alternative version


81 bookmarked

Using WTL's Built-in Dialog Resizing Class

, 20 Jul 2001
Rate this:
Please Sign up or sign in to vote.
How to use WTL's built-in support for resizable dialogs
<!-- Download Links --> <!-- Add the rest of your HTML here -->


Recently I've been reading up on WTL, and I came across a rather interesting class that I hadn't seen mentioned anywhere, CDialogResize. Given the large number of MFC implementations of resizable dialogs, it's great that WTL provides its own, so you only have to learn one class and one way of specifying what gets resized. In this article, I will outline WTL's resizing support and provide a sample program to illustrate some of the features. You should already be familiar with WTL and how to install it. If you need help with this, there are articles on those subjects in the WTL section.

Using CDialogResize

The basics

As with many other WTL features, you begin by adding CDialogResize to the inheritance list of your dialog class. So, if you make a dialog-based app with the WTL AppWizard, you would add the line listed here in red:

class CMainDlg : public CAxDialogImpl<CMainDlg>,
                 public CDialogResize<CMainDlg>

CDialogResize is declared in atlframe.h, so add that header to your includes if it isn't there already.

The next step is to initialize the CDialogResize code in your dialog's OnInitDialog handler:

LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    // Init the CDialogResize code

DlgResize_Init() has a few optional parameters, which I'll cover later.

Next, add an entry in the dialog's message map that passes sizing messages to CDialogResize:


Finally, add a new map to the dialog class that lists which controls in the dialog will be resized:

class CMainDlg : public CAxDialogImpl<CMainDlg>, public CDialogResize<CMainDlg>

I'll describe how to fill in this map in the "Setting up the resize map" section.

Initializing CDialogResize

CDialogResize is initialized by calling to DlgResize_Init(). Its prototype is:

void CDialogResize::DlgResize_Init (
    bool bAddGripper = true,
    bool bUseMinTrackSize = true,

The parameters are:

  • bAddGripper: This controls whether CDialogResize adds a size box to the bottom-right corner of the dialog. Pass true to add the size box, or false to not add it.
  • bUseMinTrackSize: This parameter controls whether CDialogResize restricts the minimum size of the dialog. If you pass true, the dialog is not allowed to be sized smaller than its initial size (as stored in the resource file). Pass false if you don't want to restrict the dialog's minimum size.
  • dwForceStyle: Specifies window styles to apply to the dialog. The default value is usually sufficient.

Setting up the resize map

The dialog resize map tells CDialogResize which controls to move or size. An entry looks like this:


ControlID is the ID of the dialog control. The possible flags and their meanings are:

  • DLSZ_SIZE_X: Resize the width of the control as the dialog resizes horizontally.
  • DLSZ_SIZE_Y: Resize the height of the control as the dialog resizes vertically.
  • DLSZ_MOVE_X: Move the control horizontally as the dialog resizes horizontally.
  • DLSZ_MOVE_Y: Move the control vertically as the dialog resizes vertically.
  • DLSZ_REPAINT: Invalidate the control after every move/resize so it repaints every time.

Note that you cannot move and size a control in the same dimension. If you specify, say DLSZ_MOVE_X and DLSZ_SIZE_X together, the size flag is ignored.

You can also group controls together so that they move and size proportionally to each other. I will cover this subject later.

Sample Project

The sample project included with this article is a simple dialog-based app that functions as a browser (using the WebBrowser ActiveX control). The control IDs are listed below; you should refer back to this diagram later when I discuss moving and resizing these controls.

 [Dlg control IDs - 17K]

The controls will move and size according to these rules:

  • The Location edit box will resize horizontally.
  • The Go, Exit, and About buttons will move horizontally.
  • The Back, Forward, Stop, and Refresh buttons will resize horizontally in a group.
  • The browser control will resize horizontally and vertically

The browser's OnInitDialog() function initializes CDialogResize like this:

LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/,
                     BOOL& /*bHandled*/)
    // Init the CDialogResize code

This uses the default parameters to DlgResize_Init(), which results in a size box being added. Also, the dialog cannot be sized smaller than its initial size. Here's what the dialog looks like on startup:

 [Main dialog - 28K] \

Here is the resize map, which lists the moving and sizing behavior for the controls. Note the new macros - BEGIN_DLGRESIZE_GROUP() and END_DLGRESIZE_GROUP() - that put the four browser control buttons into a resizing group.

    // Location edit box

    // Go, Exit, About buttons

    // IE control buttons

    // WebBrowser control

Here is the dialog after being resized:

 [Bigger dialog - 61K]

Notice how the edit box is wider, and the browser control is wider and taller. The behavior of the four grouped buttons is a bit tough to put into words, and the WTL code offers little guidance since there are few comments. But the basic idea is: imagine a bounding rectangle that surrounds all four buttons. That rectangle resizes like any other control, and all the buttons are sized proportionally so they remain within the bounding rectangle. If the buttons were to be moved, instead of resized, they would be positioned to always be evenly spaced apart. Note that all the controls in a group should have the same DLSZ_* flags to produce meaningful behavior.

Bugs and Problems with CDialogResize

So far, I've only seen two problems. One is that there seems to be an off-by-one bug somewhere, because the first time you resize the dialog, some of the controls shift the wrong direction by one pixel. The other, more serious problem, is a bug in the WTL AppWizard that is exposed when you add CDialogResize as a base class of your dialog. The AppWizard-generated code that displays the dialog looks like this:

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, 
                     LPTSTR lpstrCmdLine, int nCmdShow)
    // ...
    CMainDlg dlgMain;
    int nRet = dlgMain.DoModal();


    return nRet;

The trouble with that is the CMainDlg destructor is called after _Module.Term(). This causes a crash in a release build if it is built with the _ATL_MIN_CRT symbol defined. The solution is to put the CMainDlg object in an enclosing block:

int nRet;

    CMainDlg dlgMain;
    nRet = dlgMain.DoModal();


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Michael Dunn
Software Developer (Senior) VMware
United States United States
Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

Mike was a VC MVP from 2005 to 2009.

You may also be interested in...

Comments and Discussions

QuestionIs it possible to change the resize property dynamically? Pin
tzleo21-Apr-13 19:37
membertzleo21-Apr-13 19:37 
GeneralMy vote of 5 Pin
wserra11-Oct-12 19:41
memberwserra11-Oct-12 19:41 
GeneralWould not resize until I added WS_THICKFRAME style Pin
jaylb29-Oct-07 9:01
memberjaylb29-Oct-07 9:01 
I built this under VS2005 and WTL80 and the dialog would not resize until I added the WS_THICKFRAME style to the dialog template as specified in ATLFRAME.H:

// CDialogResize - provides support for resizing dialog controls
// (works for any window that has child controls)

// Put CDialogResize in the list of base classes for a dialog (or even plain window),
// then implement DLGRESIZE map by specifying controls and groups of control
// and using DLSZ_* values to specify how are they supposed to be resized.
// Notes:
// - Resizeable border (WS_THICKFRAME style) should be set in the dialog template

GeneralRe: Would not resize until I added WS_THICKFRAME style Pin
Michael Dunn6-Jan-08 8:56
mvpMichael Dunn6-Jan-08 8:56 
GeneralRe: Would not resize until I added WS_THICKFRAME style Pin
Ason14-Feb-08 20:52
memberAson14-Feb-08 20:52 
QuestionDoes new AppWizard fix CDialogResize bug ? Pin
Defenestration2-May-06 13:20
memberDefenestration2-May-06 13:20 
AnswerRe: Does new AppWizard fix CDialogResize bug ? Pin
Michael Dunn2-May-06 15:37
sitebuilderMichael Dunn2-May-06 15:37 
GeneralRe: Does new AppWizard fix CDialogResize bug ? Pin
Defenestration2-May-06 15:52
memberDefenestration2-May-06 15:52 
GeneralVery useful Pin
Anna-Jayne Metcalfe18-Jan-05 4:36
memberAnna-Jayne Metcalfe18-Jan-05 4:36 
GeneralRe: Very useful Pin
Mizan Rahman30-Mar-11 1:22
memberMizan Rahman30-Mar-11 1:22 
GeneralRe: Very useful Pin
Anna-Jayne Metcalfe30-Mar-11 2:16
memberAnna-Jayne Metcalfe30-Mar-11 2:16 
QuestionCentering controls? Pin
Maks Pyatkovskiy9-Sep-04 17:13
sussMaks Pyatkovskiy9-Sep-04 17:13 
AnswerRe: Centering controls? Pin
vtraveller6-Apr-10 23:51
membervtraveller6-Apr-10 23:51 
QuestionDynamically creating controls placing problem? Pin
Daredevil21-Jun-04 7:19
memberDaredevil21-Jun-04 7:19 
AnswerRe: Dynamically creating controls placing problem? Pin
Michael Dunn21-Jun-04 8:49
sitebuilderMichael Dunn21-Jun-04 8:49 
GeneralRe: Dynamically creating controls placing problem? Pin
Daredevil22-Jun-04 2:05
memberDaredevil22-Jun-04 2:05 
QuestionJust Show Scroll Bars? Pin
Rick Pingry3-Apr-04 5:30
memberRick Pingry3-Apr-04 5:30 
GeneralWebBrowser control drawed out of dialog in multithreaded COM apps Pin
Krotow8-Feb-04 23:04
memberKrotow8-Feb-04 23:04 
GeneralRe: WebBrowser control drawed out of dialog in multithreaded COM apps Pin
Krotow16-Feb-04 4:12
memberKrotow16-Feb-04 4:12 
GeneralUpdate Problems Pin
labrador27-Nov-03 4:17
memberlabrador27-Nov-03 4:17 
GeneralRe: Update Problems Pin
Michael Dunn27-Nov-03 7:37
sitebuilderMichael Dunn27-Nov-03 7:37 
GeneralCTabCtrl Pin
Bard26-Sep-03 2:51
memberBard26-Sep-03 2:51 
GeneralRe: CTabCtrl Pin
Dr Bob.11-May-06 5:53
memberDr Bob.11-May-06 5:53 
GeneralRe: CTabCtrl Pin
Fernando A. Gomez F.20-Apr-08 16:55
memberFernando A. Gomez F.20-Apr-08 16:55 
GeneralSetting initial dialog size Pin
Robert Edward Caldecott28-Apr-03 22:58
memberRobert Edward Caldecott28-Apr-03 22:58 
GeneralRe: Setting initial dialog size Pin
Eric Niemisto9-May-03 3:45
sussEric Niemisto9-May-03 3:45 
GeneralStrange XP behaviour Pin
Rodger Bernstein23-Apr-03 4:10
sussRodger Bernstein23-Apr-03 4:10 
GeneralVery good! Pin
Adrian Bacaianu14-Apr-03 3:01
memberAdrian Bacaianu14-Apr-03 3:01 
GeneralRe: Very good! Pin
joyjjjz22-Sep-08 23:27
memberjoyjjjz22-Sep-08 23:27 
GeneralExcellent article Pin
Robert Edward Caldecott3-Mar-03 1:06
memberRobert Edward Caldecott3-Mar-03 1:06 
Generalwon't resize Pin
Stephen Owens27-Feb-03 16:19
memberStephen Owens27-Feb-03 16:19 
GeneralRe: won't resize Pin
pathman1-May-03 8:56
memberpathman1-May-03 8:56 
GeneralRe: won't resize Pin
Stephen Owens1-May-03 9:34
memberStephen Owens1-May-03 9:34 
GeneralRe: won't resize Pin
Serg_swamp13-Jul-06 1:54
memberSerg_swamp13-Jul-06 1:54 
Generalbah Pin
Anonymous4-Oct-02 16:02
sussAnonymous4-Oct-02 16:02 
GeneralRe: bah Pin
peterchen2-Feb-05 8:47
memberpeterchen2-Feb-05 8:47 
GeneralResize Map Tips Pin
John Wilkinson18-Sep-02 9:01
memberJohn Wilkinson18-Sep-02 9:01 
GeneralReason for shift on first resize Pin
Jim Barry15-Jul-02 14:13
memberJim Barry15-Jul-02 14:13 
GeneralRe: Reason for shift on first resize Pin
Michael Dunn15-Jul-02 18:44
sitebuilderMichael Dunn15-Jul-02 18:44 
GeneralRe: Reason for shift on first resize Pin
Jim Barry15-Jul-02 23:47
memberJim Barry15-Jul-02 23:47 
QuestionGroup box backgrounds don't repaint? Pin
Anonymous12-Apr-02 11:01
memberAnonymous12-Apr-02 11:01 
AnswerRe: Group box backgrounds don't repaint? Pin
Anonymous12-Apr-02 11:32
memberAnonymous12-Apr-02 11:32 
AnswerRe: Group box backgrounds don't repaint? Pin
SGarratt4-Nov-03 9:28
memberSGarratt4-Nov-03 9:28 
QuestionHow to sink events in your project? Pin
Huu Quynh23-Dec-01 15:06
memberHuu Quynh23-Dec-01 15:06 
QuestionWhat determines the anchoring point of the controls Pin
Drewpee18-Oct-01 4:54
memberDrewpee18-Oct-01 4:54 
GeneralIs there a way to get the resize to work on a borderless window Pin
Hawkeye31-Jul-01 16:03
memberHawkeye31-Jul-01 16:03 
GeneralRe: Is there a way to get the resize to work on a borderless window Pin
Michael Dunn9-Aug-01 20:10
memberMichael Dunn9-Aug-01 20:10 
General#include <atlframe.h> Pin
Thomas Freudenberg6-Jul-01 2:56
memberThomas Freudenberg6-Jul-01 2:56 
GeneralRe: #include <atlframe.h> Pin
Michael Dunn6-Jul-01 13:27
memberMichael Dunn6-Jul-01 13:27 
Anonymous26-Jun-01 11:04
memberAnonymous26-Jun-01 11:04 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 21 Jul 2001
Article Copyright 2001 by Michael Dunn
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid