Click here to Skip to main content
14,981,383 members
Articles / Desktop Programming / WTL
Posted 29 Apr 2004


30 bookmarked

WTL CDialogResize extension

Rate me:
Please Sign up or sign in to vote.
4.98/5 (17 votes)
29 Apr 20042 min read
Extension to WTL CDialogResize template to support persistent dialog size

Sample Image - wtl_dialog_resize_ex.jpg


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

Comments and Discussions

GeneralA Fiver! Pin
João Paulo Figueira17-Feb-06 3:56
professionalJoão Paulo Figueira17-Feb-06 3:56 
GeneralRe: A Fiver! Pin
Rob Caldecott17-Feb-06 4:04
MemberRob Caldecott17-Feb-06 4:04 
GeneralPut an executable in the demo project zip Pin
Anonymous30-Apr-04 12:18
MemberAnonymous30-Apr-04 12:18 
GeneralRe: Put an executable in the demo project zip Pin
Rob Caldecott30-Apr-04 12:29
MemberRob Caldecott30-Apr-04 12:29 
GeneralVery useful Pin
Igor Vigdorchik30-Apr-04 6:42
MemberIgor Vigdorchik30-Apr-04 6:42 
GeneralRe: Very useful Pin
Pablo Aliskevicius5-May-04 1:21
MemberPablo Aliskevicius5-May-04 1: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.