Click here to Skip to main content
Click here to Skip to main content
Go to top

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 -->

Introduction

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>
{
public:
 enum { IDD = IDD_MYDIALOG };

 BEGIN_DLGRESIZE_MAP(CMyDialog)
  ...
 END_DLGRESIZE_MAP()

 BEGIN_MSG_MAP(CMyDialog)
  MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
  ...
  CHAIN_MSG_MAP(CDialogResizeEx<CMyDialog>)
 END_MSG_MAP()

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

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

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

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:

dialog_nnn_cx
dialog_nnn_cy

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;

dlg.m_size.cx = 640;
dlg.m_size.cy = 480;
dlg.DoModal();

CDialogResizeEx

Here is the class:

#pragma once

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

 CDialogResizeEx(void)
  : 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 (m_size.cx != 0 && m_size.cy != 0)
  {
   pT->SetWindowPos(NULL, 0, 0, m_size.cx, m_size.cy, <BR>                   SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
   
   CRect rectClient;
   pT->GetClientRect(&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)
    m_size.cx = dw;
   if (reg.QueryDWORDValue(FormatHeightValueName(), dw) == ERROR_SUCCESS)
    m_size.cy = dw;
#else
   if (reg.QueryValue(dw, FormatWidthValueName()) == ERROR_SUCCESS)
    m_size.cx = dw;
   if (reg.QueryValue(dw, FormatHeightValueName()) == ERROR_SUCCESS)
    m_size.cy = dw;
#endif
  }
 }

 // 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.SetDWORDValue(FormatWidthValueName(), m_size.cx);
   reg.SetDWORDValue(FormatHeightValueName(), m_size.cy);
#else
   reg.SetValue(m_size.cx, FormatWidthValueName());
   reg.SetValue(m_size.cy, FormatHeightValueName());
#endif
  }
 }

 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;
 }

 BEGIN_MSG_MAP(CDialogResizeEx)
  MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
  CHAIN_MSG_MAP(CDialogResize<T>)
 END_MSG_MAP()

 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);
  m_size.cx = rect.Width();
  m_size.cy = rect.Height();

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

  bHandled = FALSE;
  return 0;
 }
};

License

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

Share

About the Author


Comments and Discussions

 
GeneralA Fiver! PinmemberJoão Paulo Figueira17-Feb-06 3:56 
Hands-down. Just included it in my forthcoming product.
 
Regards,
João Paulo Figueira
Embedded MVP
GeneralRe: A Fiver! PinmemberRobert Edward Caldecott17-Feb-06 4:04 
GeneralPut an executable in the demo project zip PinsussAnonymous30-Apr-04 12:18 
GeneralRe: Put an executable in the demo project zip PinmemberRobert Edward Caldecott30-Apr-04 12:29 
GeneralVery useful PinmemberIgor Vigdorchik30-Apr-04 6:42 
GeneralRe: Very useful PinmemberPablo Aliskevicius5-May-04 1:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.140916.1 | Last Updated 30 Apr 2004
Article Copyright 2004 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid