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.

The Sample Application

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

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.

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

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

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

Share

About the Author

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.

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

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)

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

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

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.

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!

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.

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

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