Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Tips for dialog based applications

0.00/5 (No votes)
25 Aug 2002 1  
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