Click here to Skip to main content
Click here to Skip to main content
Go to top

Tips for dialog based applications

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

Introduction

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.

Disclaimer

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()
  {
       CDialog::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 | 
           CBRS_TOOLTIPS |CBRS_FLYBY | CBRS_BORDER_BOTTOM);

      // 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.
      ToolBar.LoadToolBar(IDR_TOOLBAR1);
  
      CRect rcClientStart;
      CRect rcClientNow;
      GetClientRect(rcClientStart);

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

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

     while (pwndChild)
     {
       pwndChild->GetWindowRect(rcChild);
       ScreenToClient(rcChild);
       rcChild.OffsetRect(ptOffset);
       pwndChild->MoveWindow(rcChild, FALSE);
       pwndChild = pwndChild->GetNextWindow();
     }

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

     // And position the control bars
     RepositionBars(AFX_IDW_CONTROLBAR_FIRST, 
         AFX_IDW_CONTROLBAR_LAST, 0);
  
     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
  GetClientRect(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);
     PostMessage( WM_NCLBUTTONDOWN, HTCAPTION, 
          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.
     m_brush.CreateSolidBrush(RGB(150,50,100));
Handle the WM_CTLCOLOR message in your dialog and in the OnCtlColor function, add this code.
     m_brush.CreateSolidBrush(RGB(150,50,100));
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.

  SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, NULL);

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[] =
  {
     ID_INDICATOR_CAPS,
     ID_INDICATOR_NUM
  };
In the OnInitDialog function of the dialog, add the following code.
  m_StatusBar.CreateEx(this,SBT_TOOLTIPS,
    WS_CHILD|WS_VISIBLE|CBRS_BOTTOM,AFX_IDW_STATUS_BAR);
 
  // Set the indicators namely caps and nums lock status
  m_StatusBar.SetIndicators(indicators,
     sizeof(indicators)/sizeof(UINT));

  CRect rect;
  GetClientRect(&rect);
		
  m_StatusBar.SetPaneInfo(0,ID_INDICATOR_CAPS,SBPS_NORMAL,
     rect.Width()/2);
  m_StatusBar.SetPaneInfo(1,ID_INDICATOR_NUM,SBPS_STRETCH ,
     rect.Width()/2);

  RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,
       ID_INDICATOR_NUM);

  m_StatusBar.GetStatusBarCtrl().SetBkColor(RGB(180,180,180));

Conclusion

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

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

Share

About the Author

VGirish
Founder
India India
No Biography provided

Comments and Discussions

 
Questiondialog box Pinmembergurunbr111-Mar-13 21:29 
GeneralDialog based application PinmemberAmit Luniya11-Aug-08 6:25 
Questionyou should check user drag which side of the window? Pinmemberrock.shell16-Apr-08 1:16 
GeneralAbout the toolbar in dialog... PinmemberAHTOXA5-Jan-08 2:19 
QuestionHow to add tool tip control in toolbar PinmemberAvadhut_Computer14-Oct-07 22:04 
GeneralA thought! Pinmembercristitomi22-Mar-07 8:28 
Hi!
About changing the shape of the dialog, I want to make a remark.
You must go to the resource editor, Dialog Properties->Styles tab and uncheck the Title Bar so that the dialog will not have a title bar and surrounding bar. Otherwise, when you make your dialog an ellipse, for example it will look pretty weird.
Best regards,
Christian.

 
I am in love with VC++
QuestionHow do I place my Toolbar on desiered position on a Dialog?? [modified] PinmemberAnup Daware5-Oct-06 23:36 
AnswerRe: How do I place my Toolbar on desiered position on a Dialog?? PinmemberAnup Daware6-Oct-06 0:27 
General"To move a captionless dialog" PinmemberDevil for ever23-May-06 4:58 
GeneralRe: "To move a captionless dialog" Pinmembermszengg6-Aug-12 20:44 
Question32Bit Color at CToolbar? PinsussAnonymous17-Jul-05 7:17 
AnswerRe: 32Bit Color at CToolbar? PinmemberKing of Merit31-Oct-06 14:59 
GeneralDialog consult PinmemberArielR15-Dec-02 12:44 
GeneralMistake in WM_CTLCOLOR Handler PinmemberJim Hawkins30-Oct-02 9:26 
GeneralRe: Mistake in WM_CTLCOLOR Handler PinmemberCarlo @ XBS18-Oct-07 2:07 
GeneralBig mistake - Use CFrameWnd Pinmemberarmentage29-Aug-02 10:47 
GeneralRe: Big mistake - Use CFrameWnd Pinsussigor196029-Aug-02 17:02 
GeneralRe: Big mistake - Use CFrameWnd PinmemberVGirish29-Aug-02 18:05 
GeneralRe: Big mistake - Use CFrameWnd PinmemberCoruscant31-May-08 4:39 
General"To put a toolbar in your dialog" PinmemberTomasz Sowinski26-Aug-02 3:20 
GeneralRe: "To put a toolbar in your dialog" PinmemberVGirish26-Aug-02 14:45 
GeneralRe: "To put a toolbar in your dialog" PinmemberTomasz Sowinski26-Aug-02 22:44 
GeneralRe: Shooltzsaver PinmemberVGirish26-Aug-02 14:53 
GeneralI like your articles but... PinmemberJason Henderson29-Jun-02 17:30 
GeneralRe: I like your articles but... PinmemberChristian Graus29-Jun-02 18:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 26 Aug 2002
Article Copyright 2002 by VGirish
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid