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

Easy dialog control resizer

By , 22 Sep 2003
 

dialog window before resizing

dialog window after resizing

Introduction

Yesterday when I wrote a simple Win32 dialog application, I missed one handy feature that is implemented in .NET forms; anchors and docking for controls. I took a look at the CodeProject, but I did not find exactly what I was looking for, so I wrote a simple solution for myself. I put it here with hope that it will be useful for some other programmers. This solution has two advantages. First is that, you can use this simple class in MFC and non-MFC applications too. Second is that you can use add/remove for dynamically created controls during running application.

Using the code

First of all, if you want to use this class in your application, you must include this header file:

#include "anchor.h"
  
CDlgAnchor dlgAnchor;
CDlgMan dlgMan;     

In WM_INITDIALOG handler routine or OnInitDialog (MFC applications), you must initialize this class:

static BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, 
                                   WPARAM wParam, LPARAM lParam)
{
    switch (msg) {
      case WM_INITDIALOG:
        // load pos/sizes of dialog window and all child windows
        dlgMan.Load(hwndDlg, "Software\\CodeProject\\AnchorExample");
        dlgAnchor.Init(hwndDlg);
        ...

Now you can set anchors and/or docking for the controls you want from the list in the header file. You can use add/remove controls during the life of the dialog window as you will need, e.g. you can use Add function for dynamically created controls.

dlgAnchor.Add(IDOK, ANCHOR_BOTTOM);
dlgAnchor.Add(IDCANCEL, ANCHOR_BOTTOM);
dlgAnchor.Add(IDC_LIST1, ANCHOR_TOPLEFT | ANCHOR_BOTTOM);
dlgAnchor.Add(IDC_LIST2, ANCHOR_BOTTOMLEFT | ANCHOR_RIGHT);
dlgAnchor.Add(IDC_EDIT1, ANCHOR_ALL);
...

Lastly you will need to add to the WM_SIZE handler routine, code that will execute the OnSize class routine, and add to the WM_DESTROY handler routine, code that will save window position and size.

case WM_SIZE:
      dlgAnchor.OnSize();
      ...
case WM_DESTROY:
      dlgMan.Save();
      ...

That's all. I hope, you find this class useful.

History

  • 19 September 2003
    • Added Update and UpdateAll functions that you may use if some other dialog handler (like Splitter) will change size of controls.
    • Added helper class CDlgMan (saving/restoring window position and position of all child windows).
  • 11 September 2003
    • First release.

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

About the Author

uemaker
Web Developer
Slovakia Slovakia
Member
No Biography provided

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 5memberwwputao11 May '11 - 0:58 
useful and good
GeneralMy vote of 5memberRolf Kristensen6 Jan '11 - 4:47 
Very easy to use, and works as expected. Really like there are no nasty message hooks and it is just one simple class.
GeneralCheck this out...memberMizan Rahman6 Dec '10 - 4:23 
Hi,
 
Here is a new one: MFC/C++ Helper Class for Window Resizing[^]
 
BR
Mizan
GeneralMy vote of 5memberfluke0123 Nov '10 - 17:03 
I have been searching for days for something simple to make my dialog-based app resizable. This was exactly what I was looking for. Thank you VERY MUCH!!!
GeneralGreat stuffmemberCerberu525 Nov '08 - 0:52 
Thanks for this it works great. Credit where its due. Well done.
 
Matt
QuestionHow to keep two compenent equal in sizes when resize?membercgyonly26 Jun '08 - 4:41 
I have two CListCtrl equal in sizes in the middle of window,
How can I mantain them equal in sizes when the window resize? Thank you!
Questionsupplement,my UI like this...membercgyonly26 Jun '08 - 4:50 
------------—_[]X
 
|------| |------|
| | | |
| | | |
|------| |------|
 
-----------------
GeneralToo sweet!memberpalanka19 Mar '08 - 9:33 
I have this resizing an Ultimate Grid control. Took about 5 minutes.
 
Thanks loads,
Tim
GeneralWorks greatmemberWolferine423 May '07 - 8:34 
I was just about to write such a thing myself and I found this - it saved me a lot of coding... Thanks
 
Wolferine4
GeneralI can't use in MFCmembersms911 Sep '06 - 19:27 
I use it in this way,But the dialog can't see now.why?OMG | :OMG:
class CMyKuGooDlg : public CDialog
{
// Construction
public:
//
CDlgAnchor dlgAnchor;
CDlgMan dlgMana;
//...
}
GeneralThanks..memberKim A31 May '06 - 4:25 
for the good resize class. I like Smile | :)

GeneralPicture ControlmemberScaleOvenStove6 Feb '06 - 7:14 
Picture controls dont seem to work. when you use the maximize button it is fine, but if you resize the window, then it leaves lines all over the dialog like it isnt getting repainted or something. Has anyone else seen this behanvior?
GeneralRe: Picture ControlmemberJenleonard7 May '06 - 15:33 
Did you ever have any luck solving this problem? I am having a similar problem with a DSOFramer control in a MFC Dialog. I can't get it to redraw although it appears to resize.
 
Thanks!
QuestionFit a Dialog into a view?memberrgchezhian8 Mar '05 - 4:16 
Is it possible to fit a dialog into a view?.
or if there is any other way to procede it?
GeneralError Resize ActiveX controls (MSFlexGrid)memberAlexFilan12 Jul '04 - 23:29 
Resize ActiveX controls. Error "Invalid window Handle". Frown | :(
GeneralRe: Error Resize ActiveX controls (MSFlexGrid)sussAnonymous13 Jul '04 - 0:50 
there are two versions of "Add" function. one with a control ID as a parameter and one with a window handle as a parameter. use second, for example:
dlgAnchor.Add(m_cFlexiGrid.m_hWnd, ...)

QuestionHow to Resize CStatusbar ObjectmemberKoundinya23 Apr '04 - 2:54 
Hi,
 
First I should say many thanks for such a good code.
 
I used your code in my dialog based application. As all dialog items have resource ids, i am able to resize controls with your code, But for Status bar I am creating it directly using CStatusBar class. So my quesstion is how can I bind the statusbar object to yor code.
 
Thanks
Sudhakar Koundinya
Questioncan this be restricted to some minimum size ?memberHabib Ahmed Qurashi3 Nov '03 - 17:34 
hi
very nice code. very kewl.
 
i need to get info that, if this is possible to restrice the dialog to some minimum width and hiegt from ur code..
or i jsut call api .. to restric min size of dlgbox...
or handke someway myself in wm_size before anchor.onsize();
 
suggestion and a lil code here plz.

 
World without walls neither need gates nor windows.
AnswerRe: can this be restricted to some minimum size ?memberuemaker3 Nov '03 - 21:38 
case WM_SIZING:
{
int min_x = 200;
int min_y = 300;
int max_x = 400;
int max_y = 500;
 
LPRECT r = (LPRECT) lParam;
int ux = -1, uy = -1, wx = r->right-r->left, wy = r->bottom-r->top;
if (wy < min_y) uy = min_y;
else if (wy > max_y) uy = max_y;
if (wx < min_x) ux = min_x;
else if (wx > max_x) ux = max_x;

if (uy != -1) {
if (wParam == WMSZ_TOP || wParam == WMSZ_TOPLEFT || wParam == WMSZ_TOPRIGHT) {
r->top = r->bottom - uy;
}
else {
r->bottom = r->top + uy;
}
}
if (ux != -1) {
if (wParam == WMSZ_LEFT || wParam == WMSZ_TOPLEFT || wParam == WMSZ_BOTTOMLEFT) {
r->left = r->right - ux;
}
else {
r->right = r->left + ux;
}
}
}
return TRUE;

Generalwould you please...memberhongseman22 Oct '03 - 14:43 
would you please show me a resizer dialog with only one CEdit control directly without class.I wanna know how to resize and move a control in dialog .thanks !
QuestionCombatible with CSplitDialog?memberiknowindigo18 Sep '03 - 7:39 
I know you wrote the code for both adding a splitter to a dialog, and resizing controls in the dialog.
 
I'm trying to get both to work.
 
Any hint on how to set it up?
 
First I got the splitter to work nicely. I made two panes, with a different control in each pane.
 
Now, I try to resize two controls, but they unhook the splitter, which immediately becomes inactive.
 
Suggestions?
AnswerRe: Combatible with CSplitDialog?memberiknowindigo18 Sep '03 - 9:45 
my bad Frown | :(
 
The dialog splitter code I was talking about was written by Georges Kanaan in a really great chunk of code I got from CodeGuru.
 
I would like to get them to play nicely together. Working on that now...
GeneralMy dialog doesn't sizememberiknowindigo18 Sep '03 - 7:03 
Nice code. I hooked it up, however, I have a small problem.
 
My dialog doesn't know how to resize. Is there a flag I need to set?
 
Also, I'm not seeing the grabber bottom right. I assume that comes with the ability to resize?
 
(I did put the WM_SIZE handler in, but once the dialog is up, resizing doesn't seem enabled
 
Thanks in advance for any help!
GeneralRe: My dialog doesn't sizememberuemaker18 Sep '03 - 7:18 
WS_THICKFRAME style must be set.
GeneralRe: My dialog doesn't sizememberiknowindigo18 Sep '03 - 7:33 
Oh thank you! I didn't get the gripper, but now I can resize!
 
btw, it's a small world! I'm basing this dialog on CSplitDialog, which u did. That was a very nice job!

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 23 Sep 2003
Article Copyright 2003 by uemaker
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid