Click here to Skip to main content
13,291,084 members (72,198 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 29 Apr 2004

WTL CDialogResize extension

, 29 Apr 2004
Rate this:
Please Sign up or sign in to vote.
Extension to WTL CDialogResize template to support persistent dialog size
<!-- Article image -->

Sample Image - wtl_dialog_resize_ex.jpg

<!-- Add the rest of your HTML here -->


One WTL template I use in many projects is the excellent CDialogResize<T>.  Deriving your dialog class from this template allows you to implement a resizing dialog box very easily indeed.  However, one feature it lacks is the ability to persist the dialog size, and this where my small extension class comes in.

CDialogResizeEx<T> allows the size of the dialog to be stored in the Windows registry at a location of your choice, ensuring that the next time the dialog is displayed, it uses the previous size.

This article doesn't cover the WTL CDialogResize class - for an excellent explanation on how this works, refer to Michael Dunn's article:

Using WTL's Built-in Dialog Resizing Class

Using CDialogResizeEx

To use this template with an existing CDialogResize derived class, take the following steps:

  1. Derive from CDialogResizeEx instead of CDialogResize.
  2. Alter the CHAIN_MSG_MAP call to use CDialogResizeEx.
  3. In your WM_INITDIALOG handler call DlgResize_InitEx instead of DlgResize_Init.
  4. Call to the LoadSize method to specify the root registry parent and key name where the dialog size will be loaded/saved.

For example, you may have a dialog class that looks like this:

class CMyDialog :
 public CDialogImpl<CAnotherDialog>,
 public CDialogResizeEx<CAnotherDialog>
 enum { IDD = IDD_MYDIALOG };



 LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/,<BR>                      BOOL& /*bHandled*/)
  return TRUE;

Next, to display the dialog you might have the following code:

CMyDialog dlg;
dlg.LoadSize(HKEY_CURRENT_USER, _T("Software\\CodeProject\\DialogResizeEx"));

That's pretty much it - should only take a few minutes to implement.

Registry Entries

The class will save the dialog size to the registry using the following value names:


Where nnn is your dialog ID.  This allows you to store the size for as many dialogs in your application as you'd like.

Note that if you don't want to store these values in the registry, you can simple set the m_size member prior to displaying the dialog.  When the dialog is closed, m_size will contain the new dialog size.  For example:

CMyDialog dlg; = 640; = 480;


Here is the class:

#pragma once

template <class T>
class CDialogResizeEx : public CDialogResize<T>
 CSize m_size;
 HKEY m_hKeyParent;
 LPCTSTR m_lpszKeyName;

  : m_size(0, 0)
  , m_hKeyParent(NULL)
  , m_lpszKeyName(NULL)

 void DlgResize_InitEx(bool bAddGripper = true, bool bUseMinTrackSize = true,<BR>                       DWORD dwForceStyle = WS_CLIPCHILDREN)
  DlgResize_Init(bAddGripper, bUseMinTrackSize, dwForceStyle);
  T* pT = static_cast<T*>(this);  
  // Size the dialog and update the control layout
  if ( != 0 && != 0)
   pT->SetWindowPos(NULL, 0, 0,,, <BR>                   SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
   CRect rectClient;
   DlgResize_UpdateLayout(rectClient.Width(), rectClient.Height());
 // Load the dialog size from the registry.  Base the registry
 // value on the dialog ID.
 void LoadSize(HKEY hKeyParent, LPCTSTR lpszKeyName)
  m_hKeyParent = hKeyParent;
  m_lpszKeyName = lpszKeyName;

  ATL::CRegKey reg;
  if (reg.Open(hKeyParent, lpszKeyName, KEY_READ) == ERROR_SUCCESS)
   // Format the value name using the dialog ID
   DWORD dw;
#if (_ATL_VER >= 0x0700)
   if (reg.QueryDWORDValue(FormatWidthValueName(), dw) == ERROR_SUCCESS) = dw;
   if (reg.QueryDWORDValue(FormatHeightValueName(), dw) == ERROR_SUCCESS) = dw;
   if (reg.QueryValue(dw, FormatWidthValueName()) == ERROR_SUCCESS) = dw;
   if (reg.QueryValue(dw, FormatHeightValueName()) == ERROR_SUCCESS) = dw;

 // Save the dialog size to the registry.
 void SaveSize(HKEY hKeyParent, LPCTSTR lpszKeyName) const
  ATL::CRegKey reg;
  if (reg.Create(hKeyParent, lpszKeyName) == ERROR_SUCCESS)
#if (_ATL_VER >= 0x0700)
   reg.SetValue(, FormatWidthValueName());
   reg.SetValue(, FormatHeightValueName());

 CString FormatWidthValueName(void) const
  const T* pT = static_cast<const T*>(this);
  CString strValueName;
  strValueName.Format(_T("dialog_%d_cx"), pT->IDD);
  return strValueName;

 CString FormatHeightValueName(void) const
  const T* pT = static_cast<const T*>(this);
  CString strValueName;
  strValueName.Format(_T("dialog_%d_cy"), pT->IDD);
  return strValueName;


 LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, <BR>                   BOOL& bHandled)
  T* pT = static_cast<T*>(this);

  // Save the window size
  CRect rect;
  pT->GetWindowRect(rect); = rect.Width(); = rect.Height();

  if (m_hKeyParent != NULL && m_lpszKeyName != NULL)
   SaveSize(m_hKeyParent, m_lpszKeyName);

  bHandled = FALSE;
  return 0;


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

You may also be interested in...

Comments and Discussions

GeneralA Fiver! Pin
João Paulo Figueira17-Feb-06 4:56
memberJoão Paulo Figueira17-Feb-06 4:56 
GeneralRe: A Fiver! Pin
Robert Edward Caldecott17-Feb-06 5:04
memberRobert Edward Caldecott17-Feb-06 5:04 
GeneralPut an executable in the demo project zip Pin
Anonymous30-Apr-04 13:18
sussAnonymous30-Apr-04 13:18 
GeneralRe: Put an executable in the demo project zip Pin
Robert Edward Caldecott30-Apr-04 13:29
memberRobert Edward Caldecott30-Apr-04 13:29 
GeneralVery useful Pin
Igor Vigdorchik30-Apr-04 7:42
memberIgor Vigdorchik30-Apr-04 7:42 
GeneralRe: Very useful Pin
Pablo Aliskevicius5-May-04 2:21
memberPablo Aliskevicius5-May-04 2:21 

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 | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 30 Apr 2004
Article Copyright 2004 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid