Click here to Skip to main content
Click here to Skip to main content

CResizableDialog

By , 25 Jul 2012
 

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

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

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.

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

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

CResizableDialog::CResizableDialog

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

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)

About the Author

Paolo Messina
Software Developer RoboTech srl
Italy Italy
Member
Paolo began programming at the age of 9 with a glorious 8086 and GW-BASIC, then he played a bit with C, ASM and Pascal. He tought himself MFC and Windows programming, to exploit his studies of C++. Always attracted by low-level programming and Assembly, he's beginning to appreciate the joys of templates and STL. At work he changed his mind about Java, discovered Eclipse IDE, and now think it's cool.
 
He lives in Follonica, Italy.
 
He has been abroad in the U.S. to work on his final thesis before graduating. For seven months he was playing with airplanes and automatic control at the Unversity of Illinois at Urbana-Champaign.
 
He graduated in Computer Science Engineering at the University of Pisa, Italy, in December 2003.
 
Currently working for an edutainment robotics company (www.robotechsrl.com).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberPatrick Harris25 Jul '12 - 18:52 
MFC? Really?
GeneralRe: My vote of 1memberRage21 Sep '12 - 4:28 
When I still was a young programmer (I am talking about 2002/2003), I used this very class in about all the applications I had to code, and I am pretty sure _a lot_ of people did. Shirley, it is somehow outdated now, but you cannot even imagine how useful this code has been over the years.
Paolo, you are one of my earliest heroes.
~RaGE();

I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
Do not feed the troll ! - Common proverb


GeneralRe: My vote of 1memberPatrick Harris21 Sep '12 - 12:00 
Sad there's still a use for MFC. It was bad then, even worse now. Hope I never have to re-visit that place in Hades again.
GeneralRe: My vote of 1 [modified]memberPaolo Messina22 Sep '12 - 7:49 
Thank you, Rage. Maybe "hero" is too much for me, but I appreciate that. I know this code has been used in a few "big" projects, like TortoiseCVS and Emule, but most of all I'm really glad it was useful to somebody. Helping out is priceless to me.
 
And nevermind, I know this code is a bit outdated. I never really had the chance to use it in my everyday job. But if I do, you'll certainly see updates here.
 
Cheers
------
Why spend 2 minutes doing it by hand when you can spend all night plus most of the following day writing a system to do it for you? - (Chris Maunder)


modified 22 Sep '12 - 14:19.

GeneralRe: My vote of 1memberPaolo Messina22 Sep '12 - 8:27 
I feel a bit like I shouldn't do this, I mean reply to this message, if you can call a line with two words a message... but this is a little crazy.
 
Why on earth do you mind voting anything written for MFC with such a low score just because you don't like MFC? Abstain from using it and that's it! So my stuff is crap just because MFC is? What's on your mind dude? I don't get it.
 
But I guess this post will be useless anyway Smile | :)
------
Why spend 2 minutes doing it by hand when you can spend all night plus most of the following day writing a system to do it for you? - (Chris Maunder)

GeneralRe: My vote of 1memberPatrick Harris23 Sep '12 - 13:05 
You're right. I'm a negative jerk sometimes. I'm sure this article will help people who program using MFC. I'm not a fan and never have been but that's no excuse for slamming your article. Please forgive me.
GeneralRe: My vote of 1memberPaolo Messina23 Sep '12 - 23:25 
Well, this comes as a little surprise. A good one. It was not an useless post after all. Smile | :)
Apologies accepted.
------
Why spend 2 minutes doing it by hand when you can spend all night plus most of the following day writing a system to do it for you? - (Chris Maunder)

GeneralI can not download the source filesmemberadorewenzheng4 Mar '12 - 22:04 
Hello,I download your demo project,and found it was wonderful,but i can download your source files,it seems that the server has deleted it.can you send it to me?my email is disheng0320@sina.com,thank you very much!
QuestionWin 7 problem??memberBill Heitler20 Jun '11 - 9:29 
I REALLY like this code, and it is working fine in my project running under Win XP. However, I have a resizable PropertySheet-based wizard, and when I run that on a computer using Win7, the dialog fails to update properly. I can see the (flashing) controls while I'm actually dragging the dialog to resize it, but they then disappear as soon as I stop dragging the dialog.
 
As I say, this only seems to be a problem when running under Win7 - it works OK on my development machine, which is running XP.
 
Any help/suggestions greatly appreciated.
AnswerRe: Win 7 problem??memberPaolo Messina21 Jun '11 - 5:40 
Unfortunately I don't have (yet) a development machine with Windows 7, so I can't see what happens.
 
What version of the library are you using?
 
Did you try the older 1.3? Or the latest code from CVS at SourceForge (newer than 1.4a)?
 
You can get the latest code from: http://resizablelib.cvs.sourceforge.net/viewvc/resizablelib/?view=tar[^]
------
Why spend 2 minutes doing it by hand when you can spend all night plus most of the following day writing a system to do it for you? - (Chris Maunder)

GeneralResizable Textboxmemberumu29 Mar '11 - 16:06 
How to make a resizable textbox?
The text therein should grow/shrink accordingly.
Is this possible?
GeneralRe: Resizable TextboxmemberMizan Rahman4 Apr '11 - 23:24 
You could also see this: MFC/C++ Helper Class for Window Resizing[^]
QuestionAdding logo to top of Property Sheet breaks codemembermasteryoda2118 Oct '10 - 6:56 
Paolo,
 
This code is great, but when I add a logo to the top of a property sheet (as: http://www.codeguru.com/cpp/controls/propertysheet/article.php/c3973[^] see: Adding a Control to the Property Sheet) it breaks your code.
 
Do you have a fix?
AnswerRe: Adding logo to top of Property Sheet breaks codememberPaolo Messina18 Oct '10 - 21:43 
Copy OnInitDialog from CResizableSheet and make m_bLayoutDone as protected in the header file.
 
Then in your sheet call CPropertySheet::OnInitDialog directly at the beginning, skipping the CResizableSheet implementation (that you have copied at the end of your OnInitDialog).
 
That may or may not work. If the layout is much different, do not call PresetLayout and roll your own.
 
Paolo
------
Why spend 2 minutes doing it by hand when you can spend all night plus most of the following day writing a system to do it for you? - (Chris Maunder)

QuestionIs this suitable for dialog with child dialogs?memberRicky.Lee31 Mar '09 - 20:32 
when parant dialog size is changed, child dialog size can change accordingly.
is this class suitable for this case? if so, how to do? thanks
Ricky
QuestionI need to resize the controls in a dialog automatically?memberjohnthecoder25 Aug '08 - 0:16 
Hi,
 
Now now am using CResizableFormView in my application.I need to set the size of the dialog according to the size of the main window.Because its an sdi application different dailogs are displayed at different times in the same window.So the dialog controls sholud be resized automatically.Please help..
 
Regards,
thanks in advance,
 
John
AnswerRe: I need to resize the controls in a dialog automatically?memberPaolo Messina25 Aug '08 - 0:38 
Please see:
http://www.codeproject.com/KB/docview/resizableformview.aspx[^]
 
Paolo
 
------
Why spend 2 minutes doing it by hand when you can spend all night plus most of the following day writing a system to do it for you? - (Chris Maunder)

QuestionWhile compiling I am getting the follwing error - Cannot open include file: 'ResizableDialog.h': No such file or directory?memberjohnthecoder24 Aug '08 - 6:54 
Hi all,
I am using resizablelib in my project.But while compiling it displays the message "Cannot open include file: 'ResizableDialog.h': No such file or directory".Please tell me what may be the reason???
 
regards,
thanks in advance,
john...
AnswerRe: While compiling I am getting the follwing error - Cannot open include file: 'ResizableDialog.h': No such file or directory?memberPaolo Messina24 Aug '08 - 10:47 
Please have a look at:
http://www.codeproject.com/KB/dialog/ResizableLib.asp[^]
 
And check you did the same steps to use the library.
 
Paolo
 
------
Why spend 2 minutes doing it by hand when you can spend all night plus most of the following day writing a system to do it for you? - (Chris Maunder)

QuestionError C2440??memberHeiniBlad22 Jun '06 - 12:07 
when my app is set to use CDialog, all works fine.
when i change CDialog to CResizableDialog, as described i get this error:
 
error C2440: 'type cast' : cannot convert from 'int (__thiscall MyDlg::*)(void)' to 'void (__thiscall CCmdTarget::*)(void)
 
why does it work with CDialog?
Any ideas???
 
Thanks alot...
Heiner
AnswerRe: Error C2440??memberbrwx23 Jul '08 - 2:20 
The function, where the error is pointed at, needs to be return type 'void' not 'int'.
 
Looks like some AFX_MSG ....
 

Hope that helps you.
GeneralModeless DialogsmemberNeilDevlin3 Nov '05 - 6:06 
Hi,
Great class, very useful. I want to create a few modeless dialogs at one time, can anyone see a problem with this?
 
Neil
GeneralWindowsXP Stylememberintarapim19 Feb '05 - 2:22 
I try to use Windows XP Style , but the property pages have another size.
any fix for Windows XP Style ?
 
Robert
Generalwidth changes on minimize maximizesussSanat Tiwari10 Jan '05 - 19:29 
Hi,
Thanks for writing such a good library.
I am using it and it works fine except When we minimize the window and then again maximize it the widht of the window increases by some amount. I am not able to figure it out. Please help!!
 
Sanat Tiwari
GeneralRe: width changes on minimize maximizememberPaolo Messina10 Jan '05 - 21:13 
Hi Sanat,
 
A maximized window occupies all the screen. How can it grow in size?
Or you mean minimize and then restore?
 
In this case, I can't figure out what happens there... Is it the main window size or the size of some controls?
 
Please add some details...
 
Paolo
 
------
Why spend 2 minutes doing it by hand when you can spend all night plus most of the following day writing a system to do it for you? - (Chris Maunder)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 25 Jul 2012
Article Copyright 2000 by Paolo Messina
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid