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

Easy dialog control resizer

Rate me:
Please Sign up or sign in to vote.
4.86/5 (45 votes)
22 Sep 20031 min read 223.4K   7.4K   83   48
Simple implementation of "anchors" and "docking" features implemented in .NET forms

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


Written By
Web Developer
Slovakia Slovakia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
wwputao11-May-11 0:58
wwputao11-May-11 0:58 
GeneralMy vote of 5 Pin
Rolf Kristensen6-Jan-11 4:47
Rolf Kristensen6-Jan-11 4:47 
GeneralCheck this out... Pin
Mizan Rahman6-Dec-10 4:23
Mizan Rahman6-Dec-10 4:23 
GeneralMy vote of 5 Pin
fluke0123-Nov-10 17:03
fluke0123-Nov-10 17:03 
GeneralGreat stuff Pin
Cerberu525-Nov-08 0:52
Cerberu525-Nov-08 0:52 
QuestionHow to keep two compenent equal in sizes when resize? Pin
cgyonly26-Jun-08 4:41
cgyonly26-Jun-08 4:41 
Questionsupplement,my UI like this... Pin
cgyonly26-Jun-08 4:50
cgyonly26-Jun-08 4:50 
GeneralToo sweet! Pin
palanka19-Mar-08 9:33
palanka19-Mar-08 9:33 
GeneralWorks great Pin
Wolferine423-May-07 8:34
Wolferine423-May-07 8:34 
GeneralI can't use in MFC Pin
sms911-Sep-06 19:27
sms911-Sep-06 19:27 
GeneralThanks.. Pin
Kim Togo31-May-06 4:25
professionalKim Togo31-May-06 4:25 
GeneralPicture Control Pin
ScaleOvenStove6-Feb-06 7:14
ScaleOvenStove6-Feb-06 7:14 
GeneralRe: Picture Control Pin
Jenleonard7-May-06 15:33
Jenleonard7-May-06 15:33 
QuestionFit a Dialog into a view? Pin
rgchezhian8-Mar-05 4:16
rgchezhian8-Mar-05 4:16 
GeneralError Resize ActiveX controls (MSFlexGrid) Pin
AlexFilan12-Jul-04 23:29
AlexFilan12-Jul-04 23:29 
GeneralRe: Error Resize ActiveX controls (MSFlexGrid) Pin
Anonymous13-Jul-04 0:50
Anonymous13-Jul-04 0:50 
QuestionHow to Resize CStatusbar Object Pin
Koundinya23-Apr-04 2:54
Koundinya23-Apr-04 2:54 
Questioncan this be restricted to some minimum size ? Pin
Habib Ahmed Qureshi3-Nov-03 17:34
Habib Ahmed Qureshi3-Nov-03 17:34 
AnswerRe: can this be restricted to some minimum size ? Pin
uemaker3-Nov-03 21:38
uemaker3-Nov-03 21:38 
Generalwould you please... Pin
Member 58911322-Oct-03 14:43
Member 58911322-Oct-03 14:43 
QuestionCombatible with CSplitDialog? Pin
iknowindigo18-Sep-03 7:39
iknowindigo18-Sep-03 7:39 
AnswerRe: Combatible with CSplitDialog? Pin
iknowindigo18-Sep-03 9:45
iknowindigo18-Sep-03 9:45 
GeneralMy dialog doesn't size Pin
iknowindigo18-Sep-03 7:03
iknowindigo18-Sep-03 7:03 
GeneralRe: My dialog doesn't size Pin
uemaker18-Sep-03 7:18
uemaker18-Sep-03 7:18 
GeneralRe: My dialog doesn't size Pin
iknowindigo18-Sep-03 7:33
iknowindigo18-Sep-03 7:33 

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.