Click here to Skip to main content
13,558,603 members
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 16 Nov 2000

Adding controls to a dialog

, 23 Mar 2001
Rate this:
Please Sign up or sign in to vote.
An entry level tutorial on using one of the Windows Common Controls in a dialog.


This tutorial will show how to add a Common control to a very basic dialog, using a dialog based application. It will cover the following points:

This tutorial assumes that you are comfortable with creating a dialog based application using the VC++ 6 Class Wizard. If you require information on this topic, consult the article: A Beginners Guide to Dialog Base Applications - Part 1.

Placing a control on a dialog

When your dialog-based application is generated, go to the Resources in the Workspace window. Select the dialog IDD_USINGCTRLSINDIALOGS_DIALOG in the Dialog section. For this example, use the Button control, select it from the Control palette. The cross shaped cursor indicates where the centre of the Button will be placed.

There are a number of common controls you use shown below with their tool tips. These controls are discussed in more detail in other tutorials on this site.

Sizing the control on the dialog

As the cursor moves over the 'widened border', it will change to a double headed arrow indicating the directions in which you can resize the control. You will see the position and the dimensions of the control on the status bar at the bottom right. I must point out that these are in dialog units and not pixels. Care must be taken therefore if an image is to be placed on the control.

Dialog unit (DLU)

A unit of horizontal or vertical distance within a dialog box. A horizontal DLU is the average width of the current dialog box font divided by 4. A vertical DLU is the average height of the current dialog-box font divided by 8.

As can be seen from the above definition, DLUs depend on the font and assume that font's aspect ratio is 2:1 (height:width). This is why some dialogs will look good on the system on which they were designed but not so good on others. It is extremely important with multilingual applications, because not only can the length of the text vary in another language but also the style of the font.

Aligning controls

The layout of a dialog is important if it is to be user friendly, so don't take this demo as a good example. The controls should be in a logical sequence and neatly layout. To assist with the latter, the resource editor can place a grid on the dialog for you and the controls will snap to the grid when it is turned on. The size of the grid can be adjusted by going to Layout >> Grid Setting. Also, there is a toolbar of alignment tools that are easy to use.

These tools allow selected controls to be left or right aligned, cantered same sized etc.

Above, the buttons have been placed with approximate vertical spacing, then press Space Down (Alt+UpArrow) which will keep the top and bottom buttons in place and evenly space the other in-between. Press Align Left (Ctrl+LeftArrow) and the button will be left aligned with Button 1 because it is highlighted. Had Button 2 been highlighted, Button 1 would have been moved to the right in line with Button 2.

Changing control Styles and properties

To change the style and properties of a control, select it, then select the Properties menu under View, or right click on the control and select Properties.

The Properties dialog has three tabs: General, Styles and Extended Styles. For most controls, the General and Extended Styles tabs are the same and provide for entering the control's ID and text, default enable/visible states, and if it is to be part of the tab order. Extended Style include border types and reading order for Hebrew and Arabic applications.

The Styles tab has the styles that are specific to the control type as shown for the list box above. The effect of these styles are discussed in other articles on this site in more detail.

Setting the tab order

The tab order is the order in which you move focus through a series of controls using the TAB key. By default, the tab order is the order in which the controls are placed on the dialog. Once the placement is complete, it is worth checking that the tab order follows a logical flow in which the controls are best accessed.

To do this, press CTRL+D, Layout menu Tab Order. The dialog will then be displayed (as below) with the numbers indicating the present tab order. Click each control in the order in which the controls are to be tabbed. If a control is already in the right sequence, still click it so that it maintains its position in the order. When the order is complete, press ENTER or click where there is no control.

If only a few controls are to be re-ordered, hold down the CTRL key and click on the control prior to the one that is to be changed, then release the CTRL key and proceed as before. If the control is double clicked, it will become the first in the tab order.

Linking variables to a control

To link a variable to a control such as pointer that will give access to control's class methods:

  1. select View >> Class Wizard
  2. select the Member Variables tab
  3. select the control's ID
  4. click on Add Variable button. To pop up the Add Member Variable dialog,
  5. enter the variable name, select the category (there can be one variable for each category if needed), and variable type

Class Wizard will have made the following entry in the dialog's header file:

// Dialog Data
    CEdit    m_Edit;
    CString    m_strEdit;

the next in the dialog's constructor:

m_strEdit = _T("");

and the following entries in the DoDataExchange() method.

DDX_Control(pDX, IDC_EDIT1, m_Edit);
DDX_Text(pDX, IDC_EDIT1, m_strEdit);

The number of variables allowed does vary depending on the control.

Linking Notification handlers

To link a notification handler to a control:

  1. select View >> Class Wizard.
  2. select the Message Maps tab.
  3. select the class name of the dialog, CUsingCtrlsInDialogsDlg, to display the relevant object IDs for the class.
  4. select the object ID, in this case IDC_BUTTON1. This will display the allowable Messages.
  5. select the Message to be handled and click on the Add Function button.

Class Wizard will have made the following entry in the dialog's header file:

    // Generated message map functions
    afx_msg void OnButton();

and in the CPP file's Message Map:


and added the following function to the bottom of the file:

void CUsingCtrlsInDialogsDlg::OnButton() 
    // TODO: Add your control notification handler code here

You will notice that Class Wizard will name the function OnButton1(). This was changed because the intention was to use the same handler for all four buttons. So the Message Map entry is moved outside the Class Wizard reserved section in both the header and code file.

The prototype is also modified to take the button ID as an input.

afx_msg void OnButton(UINT nID);

The Message Map entry is modified to handle a range of button controls. For this to work, it is vital the IDs are in consecutive order.


Add contents of function to display the button number in the Static control.

void CUsingCtrlsInDialogsDlg::OnButton(UINT nID) 
    m_strReport.Format("%d", nID-IDC_BUTTON1+1);


I have shown on the basics here in a rather badly laid out dialog. It should give a starting point, however. More complete and advanced information can be found in other articles on this site.

Happy programming!


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

Wolfram Steinke
Engineer MedTech Global
Australia Australia
Started working with computers when I/O was with punched paper tape, using Algol. Then learned Fortran, Basic, various Assemblers, Forth and Postscript. Built a robot in the early '80s. When I finally got a PC I learned C, C++ and more recently worked on a variety of .NET and PHP projects. Senior Software Engineer at MedTech Global in Melbourne, Australia.

You may also be interested in...


Comments and Discussions

QuestionVC++ 6 Class Wizard Pin
KarstenK23-Jan-07 1:19
memberKarstenK23-Jan-07 1:19 

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 24 Mar 2001
Article Copyright 2000 by Wolfram Steinke
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid