Click here to Skip to main content
11,929,675 members (57,267 online)
Click here to Skip to main content
Add your own
alternative version


61 bookmarked

Tips for dialog based applications

, 25 Aug 2002
Rate this:
Please Sign up or sign in to vote.
Beginners tips for dialog based applications


These are a few tips regarding dialog based applications. Its not that the same information cannot be found elsewhere. It can even be found on my site. But, I thought of putting them all together in one place. So that a beginner need not sift through endless pages to find a piece of information he wants.


Its not extensive and its not the complete set of instructions needed for developing dialog based apps. These are just a few answers to some simple but confusing questions.

To put a toolbar in your dialog

Put this in the OnInitDialog function of your dialog class

BOOL CYourDlg::OnInitDialog()

     // Create the toolbar. To understand the meaning of 
     //the styles used, you 
     // can take a look at the MSDN for the Create function
    // of the CToolBar class.
     ToolBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP |

    // I have assumed that you have named your toolbar's 
    // resource as IDR_TOOLBAR1.
    // If you have given it a different name, change the 
    // line below to accomodate 
    // that by changing the parameter for the LoadToolBar function.

    CRect rcClientStart;
    CRect rcClientNow;

    // To reposition and resize the control bar
       AFX_IDW_CONTROLBAR_LAST,0, reposQuery, rcClientNow);
   CPoint ptOffset(rcClientNow.left - rcClientStart.left,;

   CRect rcChild;
   CWnd* pwndChild = GetWindow(GW_CHILD);

   while (pwndChild)
     pwndChild->MoveWindow(rcChild, FALSE);
     pwndChild = pwndChild->GetNextWindow();

   CRect rcWindow;
   rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
   rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
   MoveWindow(rcWindow, FALSE);

   // And position the control bars

   return TRUE;  // return TRUE  unless you set the focus to a control

You have to make CToolBar toolbar as a member variable in your dialog. Then create a toolbar in the resource and set it's ID as IDR_TOOLBAR1. You can give it any name that you want ofcourse, but make sure to change the LoadToolBar statement at the top. ( 3rd line of code ).

To change the shape of your dialog

Sometimes , you might want to change the shape of your dialog. For example,you might want it to look like an ellipse or any other shape. The following is a code snippet which allows you to make your dialog have the shape of a ellipse. For additional shapes, you can take a look at the MSDN for the following functions.

  • CreatePolygonRgn
  • CreateRectRgn
  • CreateRoundRectRgn

CRgn m_rgn;  // Put this in your dialog's header file. 
             // i.e. a member variable

// This Gets the size of the Dialog: This piece of code 
// is to be placed in the
// OnInitDialog Function of your dialog.

CRect rcDialog

// The following code Creates the area and assigns it to your Dialog
m_rgn.CreateEllipticRgn(0, 0, rcDialog.Width(), rcDialogHeight());
SetWindowRgn(GetSafeHwnd(), (HRGN) m_rgn, TRUE);

To move a captionless dialog

You might not want the caption bar to be present in the dialog at some point of time. You can do that easily by changing the dialog's properties in the resource editor. Right click on the dialog and select "Properties" from the menu that you see. In the "Styles" tab of the dialog that you see, you just have to uncheck the checkbox called "Title Bar". Thats easy. Isnt it ? 

But then, you might want to move the dialog box too. Thats not possible without code as you dont have the caption bar which the user can use to drag the dialog. To accomplish just that, Add WM_LBUTTONDOWN and the WM_NCHITTEST handlers and use the following code.

// Handler for WM_LBUTTONDOWN message

void CYourDialog::OnLButtonDown(UINT nFlags, CPoint point)
   CDialog::OnLButtonDown(nFlags, point);
        MAKELPARAM( point.x, point.y));

// Handler for WM_NCHITTEST message

LONG CYourDialog::OnNcHitTest( UINT uParam, LONG lParam )
   int xPos = LOWORD(lParam);
   int yPos = HIWORD(lParam);
   UINT nHitTest = CDialog::OnNcHitTest(CSize(xPos, yPos));
   return (nHitTest == HTCLIENT) ? HTCAPTION : nHitTest;
And now, you are able to move the dialog by clicking anywhere in the client area and dragging it.

To fill the dialog box with a user specified colour

In the Header file of your Dialog,add a CBrush variable ( member variable ). Assuming that you have declared it as such.

CBrush m_brush;
In the OnInitDialog function of your dialog, add this code to create the brush.
Handle the WM_CTLCOLOR message in your dialog and in the OnCtlColor function, add this code.
Or as an alternative to this, you can simply call SetDialogBkColor function in the InitInstance of the application if you want the dialog to have one standard colour. You can use SetDialogBkColor when you want all your child dialogs to have the same color or you can use the above said method to have different colors for different child dialogs that you are going to create.

To maximize a dialog by default

Put this line of code in the OnInitDialog function of your dialog just before the return statement.


To prevent resizing of your dialog after a certain limit

Assuming that you have given the user a flexibility to resize the dialog.But you dont want him/her to resize after a particular size. To do that,handle the WM_SIZING message like this. To add the handler, you can right click on your dialog's class name in the ClassView pane of your Workspace window. Select the Add Windows Message Handler... option and in the window that opens, select WM_SIZING from the messages that you see. And click the "Add and Edit" button. Then, in the handler function, add this code.

void CYourDialog::OnSizing(UINT fwSide, LPRECT pRect)
   if(pRect->right - pRect->left <=200)
      pRect->right = pRect->left + 200;

   if(pRect->bottom - pRect->top <=200)
      pRect->bottom = pRect->top + 200;

   CDialog::OnSizing(fwSide, pRect);

To add a status bar in your dialog

Add a CStatusBar member variable to your dialog. For example, you've declared it like this.

CStatusBar m_StatusBar;
     Assuming that i want only the status of the CAPS lock and the NUMS lock to be shown in the status bar,i've added the following variable.
static UINT BASED_CODE indicators[] =
In the OnInitDialog function of the dialog, add the following code.

// Set the indicators namely caps and nums lock status

CRect rect;





Thats all as of now folks. I'll keep updating this article as and when possible. All Luck and have a great time.


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

India India
No Biography provided

You may also be interested in...

Comments and Discussions

Questiondialog box Pin
gurunbr111-Mar-13 22:29
membergurunbr111-Mar-13 22:29 
GeneralDialog based application Pin
Amit Luniya11-Aug-08 7:25
memberAmit Luniya11-Aug-08 7:25 
Questionyou should check user drag which side of the window? Pin
rock.shell16-Apr-08 2:16
memberrock.shell16-Apr-08 2:16 
GeneralAbout the toolbar in dialog... Pin
AHTOXA5-Jan-08 3:19
memberAHTOXA5-Jan-08 3:19 
QuestionHow to add tool tip control in toolbar Pin
Avadhut_Computer14-Oct-07 23:04
memberAvadhut_Computer14-Oct-07 23:04 
GeneralA thought! Pin
cristitomi22-Mar-07 9:28
membercristitomi22-Mar-07 9:28 
QuestionHow do I place my Toolbar on desiered position on a Dialog?? [modified] Pin
Anup Daware6-Oct-06 0:36
memberAnup Daware6-Oct-06 0:36 
AnswerRe: How do I place my Toolbar on desiered position on a Dialog?? Pin
Anup Daware6-Oct-06 1:27
memberAnup Daware6-Oct-06 1:27 
General"To move a captionless dialog" Pin
Devil for ever23-May-06 5:58
memberDevil for ever23-May-06 5:58 
GeneralRe: "To move a captionless dialog" Pin
mszengg6-Aug-12 21:44
membermszengg6-Aug-12 21:44 
Question32Bit Color at CToolbar? Pin
Anonymous17-Jul-05 8:17
sussAnonymous17-Jul-05 8:17 
AnswerRe: 32Bit Color at CToolbar? Pin
King of Merit31-Oct-06 15:59
memberKing of Merit31-Oct-06 15:59 
GeneralDialog consult Pin
ArielR15-Dec-02 13:44
memberArielR15-Dec-02 13:44 
GeneralMistake in WM_CTLCOLOR Handler Pin
Jim Hawkins30-Oct-02 10:26
memberJim Hawkins30-Oct-02 10:26 
GeneralRe: Mistake in WM_CTLCOLOR Handler Pin
Carlo @ XBS18-Oct-07 3:07
memberCarlo @ XBS18-Oct-07 3:07 
GeneralBig mistake - Use CFrameWnd Pin
armentage29-Aug-02 11:47
memberarmentage29-Aug-02 11:47 
GeneralRe: Big mistake - Use CFrameWnd Pin
igor196029-Aug-02 18:02
sussigor196029-Aug-02 18:02 
GeneralRe: Big mistake - Use CFrameWnd Pin
VGirish29-Aug-02 19:05
memberVGirish29-Aug-02 19:05 
GeneralRe: Big mistake - Use CFrameWnd Pin
Coruscant31-May-08 5:39
memberCoruscant31-May-08 5:39 
General"To put a toolbar in your dialog" Pin
Tomasz Sowinski26-Aug-02 4:20
memberTomasz Sowinski26-Aug-02 4:20 
GeneralRe: "To put a toolbar in your dialog" Pin
VGirish26-Aug-02 15:45
memberVGirish26-Aug-02 15:45 
GeneralRe: "To put a toolbar in your dialog" Pin
Tomasz Sowinski26-Aug-02 23:44
memberTomasz Sowinski26-Aug-02 23:44 
VGirish wrote:
Whats DLGCBR32 ? And where can it be found ? I have no idea abt that at all.

DLGCBR32 is one of the VC6 MFC samples. You can find it on your MSDN CDs, or online at[^]

As you can see, the code in your article and sample code look very similiar, even local variables have identical names. I'd suggest adding a link or information where this code came from.

Tomasz Sowinski --

Free your mind and your ass will follow.

GeneralRe: Shooltzsaver Pin
VGirish26-Aug-02 15:53
memberVGirish26-Aug-02 15:53 
GeneralI like your articles but... Pin
Jason Henderson29-Jun-02 18:30
memberJason Henderson29-Jun-02 18:30 
GeneralRe: I like your articles but... Pin
Christian Graus29-Jun-02 19:46
memberChristian Graus29-Jun-02 19:46 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 26 Aug 2002
Article Copyright 2002 by VGirish
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid