Windows Systems Programming: Spring 2002

[ Home | Syllabus | Course Notes | Assignments | Search]


 

Basic Windows Controls (chapter 7)


Controls

Controls are special windows which take care of:


Six Basic Controls

Control Type WNDCLASS MFC Class
Buttons "BUTTON" CButton
List Boxes "LISTBOX" CListBox
Edit controls "EDIT" CEdit
Combo Boxes "COMBOBOX" CComboBox
Scroll bars "SCROLLBAR" CScrollBar
Static controls "STATIC" CStatic

CButton Class

Comes in several flavors (see figure 7-2)

Let's build 7-2. 

First define a new application:

  1. New/Project /MFC appwizard(exe) (I called it figure72)

  2. dialog based

This creates an application object (defined in files figure72.h/.cpp) and a dialog object (defined in files figure72Dlg.h/.cpp) which is the main window of this application.

We will build the first button (the push button) by hand so we can see all the pieces. We need

  1. A object of type "CButton"

  2. A Command message that will be sent from this object (when the button is pushed) to ??

  3. Message handler for that command message

Add a new data member to the Dialog object by adding the following lines into figure72Dlg.h

// In figure 72Dlg.h 
//just before the class definition, add a symbolic name for the button command message
const int IDC_PUSH_ME = 500; // hope that 500 is not used by any other command message

// after the wizard generated message handler prototypes add
afx_msg void OnPushMe();

// After m_hIcon object add
CButton m_pushMe;

// then in figure72Dlg.cpp, add the following lines in function "OnInitDialog"

m_pushMe.Create("Push Button", // Label for this button
   WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
   // style is visible child window 
   CRect(CPoint(10,10),CSize(100,50)),
   // located at position 10,10 of size 100x50
   this,	// parent is "this" window
   IDC_PUSH_ME); // ID is IDC_PUSH_ME (a unique constant)

// add the following in the MessageMap after the wizard comments

ON_COMMAND(IDC_PUSH_ME, OnPushMe)

 // then define a message handler

void CFigure72Dlg::OnPushMe() 
{
   MessageBox("Button Pushed","Command Message Received");
}
You can compile and run this program (source code here )

What has been learned


Other Button Styles added

We will add the rest of the buttons from figure 7.2 using the resource editor

  1. Select the resource editor tab/open resources/dialog, select IDD_FIGURE72_DIALOG
    This is the main window of a dialog based application, you should see

  2. We don't see the push button because that is generated programmatically. From the Controls toolbar, select the checkbox control (the icon showing a box with an "X" in it.

  3. First, we will get rid of the "TODO: ..." string by clicking on it and deleting

  4. Then click on the dialog window to place the control

  5. Right click on new control to get properties/ change caption to "Check Box"

  6. Double click on this new control, this will bring up the Add Member Function dialog shown here

  7. Select OK, then add following code to the message handler "OnCheck1"

    MessageBox("Button Pushed","Command Message Received");

  8. The Radio buttons are more complicated since typically several radio buttons are grouped together so that only one button can be selected at a time (they are mutually exclusive). A group of related radio buttons must be defined in order, with the first member of the group having the "WS_GROUP" property set and the rest not having this property set. If there is a control after the last member of the group, it should have the "WS_GROUP" property set to denote that it NOT a member of the previous radio button group. The first radio button should have the following property box
    :
    The rest of this group will not have the "Group" attribute checked (which controls the WS_GROUP property).

  9. By default the radio buttons correctly handle the mutually exclusive setting of own their members. So we could at this point compile and execute the program.

  10. If we don't define a message handler for radio buttons, we can still examine the value checked in some other message handler (e.g. in OnOK (if we overrode it), we could use the values checked and do something), alternatively, we could write a message handler for the radio button and take action immediately when changed. In the resource editor, double click on the radio button you will see the following dialog

    I have edited the member function name because I want to use the same message handler for all radio button messages. (however as a first step, just add a messagebox to say you got there).

  11. You can map the other radio buttons to their own or the same message handler (called member function above). I have mapped them all to the same handler.

  12. Use the resource editor to add a group box (which is only a visual aid for grouping - does not change the program logic).

Some experiments:

The source code


Copyright chris wild 1999-2002.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: February 05, 2002.