Click here to Skip to main content
15,860,859 members
Articles / Desktop Programming / MFC

CResizableDialog

Rate me:
Please Sign up or sign in to vote.
4.85/5 (53 votes)
25 Jul 2012CPOL 804K   15.1K   217   227
A CDialog derived class to implement resizable dialogs with MFC

CResizableDialog - What Is It & Why?

I wrote my own class to implement resizable dialogs mostly as an exercise of MFC and Windows programming. Later, I discovered that other people had written similar classes, but I didn't find one I really like that was as simple as my little projects.

Before you go any further, I have to warn you that I'm not a "guru". Surely something could be done better, but I think this class is at least easy to use.

The user will have the ability to resize the dialog, with consequent rearrangement of child windows, and you can control the minimum and maximum size allowed, as well as the maximized size and position of the dialog. The size grip is displayed by default, but you may turn it off. Automatic Save/Restore the dialog's size and position is also supported.

Conversion of a previously existent dialog should be very simple, as well as creation of a new resizable dialog.

Now implemented with ResizableLib (see article).

The Sample Application

This is a view of the sample dialogs:

Default min size is the one in the dialog template (and grip is visible)

Support for colored or bitmap background with a triangular size grip

This is a composite view, where you can see the maximized position is not the default:

A desktop snapshot with the dialog maximized and normal size overimpressed

You may see how to do this in the next section.  

Usage - Step by Step

Add the ResizableLib to your project's workspace, as explained in the relative article.

Create a dialog resource and associate it with a CDialog derived class, for example using Class Wizard, or take a dialog you have already made which you want to be resizable.

You no longer need to change the window style to have the dialog resizing.

Include 'ResizableDialog.h' in the associated header file.

Search and replace all CDialog occurrences with CResizableDialog in both your .cpp and .h files, just as if your dialog class was derived from CResizableDialog instead of CDialog. I think there's no way to let the Class Wizard do this for you. Let me know if I'm wrong, please.

Your header file should appear like this:

C++
// MyDialog.h : header file
//
// ( Class Wizard stuff )
/////////////////////////////////////////////////////////////////////////////
// CMyDialog dialog

#include "ResizableDialog.h"
class CMyDialog : public CResizableDialog
{
// Construction
public:
    CMyDialog(CWnd* pParent = NULL);    // standard constructor

// ( other stuff )
// ...
}

In your OnInitDialog override, add an anchor for each control you want the size and/or position to be changed when the user resizes the dialog.

C++
BOOL CMyDialog::OnInitDialog()
{
    CResizableDialog::OnInitDialog();

    // Set the icon for this dialog. The framework does this
    // automatically when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);     // Set big icon
    SetIcon(m_hIcon, FALSE);    // Set small icon

    // preset layout
    AddAnchor(IDOK, BOTTOM_RIGHT);
    AddAnchor(IDCANCEL, BOTTOM_RIGHT);
    
    AddAnchor(IDC_SPIN1, TOP_RIGHT);

    AddAnchor(IDC_LABEL1, TOP_LEFT);
    AddAnchor(IDC_EDIT1, TOP_LEFT, BOTTOM_RIGHT);

    AddAnchor(IDC_RADIO1, BOTTOM_LEFT);
    AddAnchor(IDC_RADIO2, BOTTOM_LEFT);
    AddAnchor(IDC_GROUP1, BOTTOM_LEFT, BOTTOM_RIGHT);
    
    // other initializations
    // ...

Here, you may also set a maximum size for your dialog (default is the workspace area), a minimum size (default is the one you set in the resource editor) and also a rectangle that the dialog occupies when maximized.

C++
// get desktop size
CRect rc;
GetDesktopWindow()->GetClientRect(&rc);

// set max tracking size to half a screen
SetMaxTrackSize(CSize(rc.Width(), rc.Height()/2));

// maximized position and size on top of the screen
rc.bottom = 100;
SetMaximizedRect(rc);

After all this settings, you may wish the dialog's size and position to be automatically saved and restored, as well as its maximized or minimized state. Just provide a Section and an Entry name in your application's profile, in which to save dialog's status.

C++
// save/restore
// (for dialog based app, default is a .INI file with
// the application's name in the Windows directory)
EnableSaveRestore(_T("DemoDlg"));

You are ready to rebuild your project and you will have a resizable dialog just as you wanted.

For further details, see the next section.

Class Reference

This class inherits from CResizableGrip, CResizableLayout, CResizableMinMax, CResizableState, and obviously from CDialog.

Image 4

CResizableDialog::CResizableDialog

C++
CResizableDialog()
CResizableDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL)
CResizableDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL)

The first is the default constructor.

The second and the third forms are needed to reproduce the construction scheme of a CDialog derived class. Since the dialog resource template is needed by the CDialog constructor, you have to call one of these forms of the CResizableDialog constructor. This is the reason why replacing one class with the other will work.

CResizableDialog::EnableSaveRestore

C++
void EnableSaveRestore(LPCTSTR pszSection, BOOL bRectOnly = FALSE)

Enables automatic save/restore on dialog's open/close operations. The arguments are the same as in CWinApp::WriteProfileString. If bRectOnly is TRUE, the minimized/maximized state is not saved/restored. Should be called after all the layout settings.

If you want details on how this information is stored by your application, look at CWinApp::SetRegistryKey, CWinApp::m_pszProfileName, CWinApp::m_pszRegistryKey on MFC documentation.

CResizable???::???

Implemented in the various base classes, see ResizableLib article.

Conclusion

I would like to make this class more "integrated" with Class Wizard, but I don't even know if it's possible. I hope this class can be useful to other programmers that just want to have resizable dialogs with the minimum effort.

I implemented a sort of percentage resizing. However, currently available anchor types do not permit a high level of complexity, but should be enough in many applications. If you want more flexibility or if your dialogs are very big and full of controls, you may search CodeProject for another solution in this same Section (see the top of the article).

The CVS tree is now on Source Forge.

Updates

25 May 2000

  • Initial public release

31 May 2000

  • Some unneeded code has been removed
  • Flickering has been reduced for Button, Static and ListBox controls
  • Save/Restore support has been added
  • Fixed a nasty bug that only came out with modeless dialogs

9 Jun 2000

  • Now compiles cleanly at warning level 4
  • Memory leak fixed (I completely forgot to free memory)
  • New type of anchorage now allowing more complex layouts (compatible with previous code)
  • Fixed a bug with the size grip when hidden and when the dialog is maximized
  • Better handling of controls that need repainting
  • Demo project updated to show new features

11 Jul 2000

  • Fixed a minor bug in save/restore functions. (standard maximized dialog wasn't restored correctly)

27 Oct 2000

  • Changed layout implementation (from CPtrList to CArray)
  • Fixed a bug with controls that need refresh

23 Nov 2000

  • Fixed a bug with multiple calls to DoModal()
  • Removed doc from zip files (you simply save this page if you need it)

13 Mar 2001

  • Fixed a bug with radio buttons and group boxes (thanks to Matt Philmon)
  • Changed copyright note

11 Jun 2001

  • New implementation and integration with ResizableLib
  • Automatic resizing style (thanks to John Simmons)
  • Added anti-flickering support

15 Jul 2001

  • Updated to new ResizableLib release
  • Demo project shows new grip implementation

28 Oct 2001

  • Version 1.1 (CVS Tag: SF_1_1)
  • Added static build configurations to all projects

License

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


Written By
Technical Lead RoboTech srl
Italy Italy
Paolo began programming at the age of 9 with a glorious Olivetti M24 (i8086) and GW-BASIC, then he played a bit with Turbo C, Turbo Pascal and Assembly (using the MS-DOS Debug). Quick BASIC and Visual Basic shortly followed, until he learned C++ in College. He tought himself MFC and Windows programming, along with some DHTML and Javascript.

Always attracted by low-level programming and Assembly, he started to appreciate the joys of templates and STL while working for his Master Thesis. For seven months he was playing with airplanes and automatic control at the Unversity of Illinois at Urbana-Champaign, where he first met QNX and embedded systems.

In his job experience he learned Java to develop user interfaces and graphical editors, and re-discovered the Eclipse IDE that he had used in its early versions with the QNX SDK. He also deepened his knowledge of Linux and embedded systems, microcontrollers firmware and embedded voice recognition, while also practicing electronics design.

He graduated in Computer Engineering (Ingegneria informatica) at the University of Pisa, Italy, in December 2003. Currently working for an electronics and robotics company (www.robotechsrl.com).

He lives in Pisa and in Follonica (GR), Italy.

Comments and Discussions

 
Question64 bit ? Pin
freddykatz26-Jan-23 2:07
freddykatz26-Jan-23 2:07 
QuestionMaximize the dialog windows Pin
bioan19-Sep-17 0:53
professionalbioan19-Sep-17 0:53 
AnswerRe: Maximize the dialog windows Pin
Paolo Messina30-Nov-17 22:48
professionalPaolo Messina30-Nov-17 22:48 
GeneralRe: Maximize the dialog windows Pin
bioan11-Dec-17 10:04
professionalbioan11-Dec-17 10:04 
QuestionGreat work and a small enhancement Pin
Rick York15-Jun-16 5:04
mveRick York15-Jun-16 5:04 
AnswerRe: Great work and a small enhancement Pin
Paolo Messina15-Jun-16 5:19
professionalPaolo Messina15-Jun-16 5:19 
GeneralMy vote of 5 Pin
Gordon Smith12-Oct-15 23:20
Gordon Smith12-Oct-15 23:20 
QuestionUpdated July 2012? Pin
Iain Clarke, Warrior Programmer24-Sep-13 4:38
Iain Clarke, Warrior Programmer24-Sep-13 4:38 
AnswerRe: Updated July 2012? Pin
Paolo Messina24-Sep-13 6:33
professionalPaolo Messina24-Sep-13 6:33 
GeneralMy vote of 1 Pin
Patrick Harris25-Jul-12 18:52
Patrick Harris25-Jul-12 18:52 
GeneralRe: My vote of 1 Pin
Rage21-Sep-12 4:28
professionalRage21-Sep-12 4:28 
GeneralRe: My vote of 1 Pin
Patrick Harris21-Sep-12 12:00
Patrick Harris21-Sep-12 12:00 
GeneralRe: My vote of 1 Pin
Paolo Messina22-Sep-12 7:49
professionalPaolo Messina22-Sep-12 7:49 
GeneralRe: My vote of 1 Pin
Paolo Messina22-Sep-12 8:27
professionalPaolo Messina22-Sep-12 8:27 
GeneralRe: My vote of 1 Pin
Patrick Harris23-Sep-12 13:05
Patrick Harris23-Sep-12 13:05 
GeneralRe: My vote of 1 Pin
Paolo Messina23-Sep-12 23:25
professionalPaolo Messina23-Sep-12 23:25 
GeneralI can not download the source files Pin
adorewenzheng4-Mar-12 22:04
adorewenzheng4-Mar-12 22:04 
QuestionWin 7 problem?? Pin
Bill Heitler20-Jun-11 9:29
Bill Heitler20-Jun-11 9:29 
AnswerRe: Win 7 problem?? Pin
Paolo Messina21-Jun-11 5:40
professionalPaolo Messina21-Jun-11 5:40 
GeneralResizable Textbox Pin
umu29-Mar-11 16:06
umu29-Mar-11 16:06 
GeneralRe: Resizable Textbox Pin
Mizan Rahman4-Apr-11 23:24
Mizan Rahman4-Apr-11 23:24 
QuestionAdding logo to top of Property Sheet breaks code Pin
masteryoda2118-Oct-10 6:56
masteryoda2118-Oct-10 6:56 
AnswerRe: Adding logo to top of Property Sheet breaks code Pin
Paolo Messina18-Oct-10 21:43
professionalPaolo Messina18-Oct-10 21:43 
QuestionIs this suitable for dialog with child dialogs? Pin
Ricky.Lee31-Mar-09 20:32
Ricky.Lee31-Mar-09 20:32 
QuestionI need to resize the controls in a dialog automatically? Pin
johnthecoder25-Aug-08 0:16
johnthecoder25-Aug-08 0:16 

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.