Click here to Skip to main content
13,557,288 members
Click here to Skip to main content
Add your own
alternative version


61 bookmarked
Posted 28 Jun 2002

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

General"To move a captionless dialog" Pin
Devil for ever23-May-06 4:58
memberDevil for ever23-May-06 4:58 
GeneralRe: "To move a captionless dialog" Pin
mszengg6-Aug-12 20:44
membermszengg6-Aug-12 20:44 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180515.1 | Last Updated 26 Aug 2002
Article Copyright 2002 by VGirish
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid