In this article, Mitchell will describe how to add several controls to your dialog windows, including the edit box, group box, check box, list box and radio button. He will also describe what DDX and DDV functions are, and show you how to use them to automatically get and set the values of the controls on a dialog.
Using MFC in C++ Part 4: Controls, DDX and DDV - The push button control (Page 3 of 9 )
There are two types of buttons that we can define in our resource file: The DEFPUSHBUTTON, which is a button that is selected by default, or the PUSHBUTTON, which is just a normal button. As we work through this article, you will notice that most controls have a similar syntax. The syntax to declare a button is shown below:
If we used both the EDITTEXT and DEFPUSHBUTTON controls as part of our dialog definition in a resource file, created a new class for that dialog, and ran our application, we would get a dialog box that looks like this (As mentioned above, the support material for this article contains the complete C++ project, which contains a full application demonstrating these controls):
As with the EDITTEXT control, the PUSHBUTTON and DEFPUSHBUTTON controls also contain functions that can operate on these controls. These functions are declared as normal, and are also added to the dialog boxes message map. Now that we know how to add these controls to a dialog box, I will describe some of the functions that are available to us, allowing us to manipulate these controls.
Some control functions
To respond to windows messages whenever a user clicks on our “Show Name” button, we will create one new function. As with any other function, it is declared in the dialogs header file, as part of its class. It is also defined in the dialogs implementation file. The declaration of a function to respond to the click of the “Show Name” button (which has an Id of IDB_GETNAME), looks like this:
afx_msg void OnShowName();
We would add the actual definition of the OnShowName function to the implementation (.cpp) file, like this:
afx_msg void CSampleDialog::OnShowName()
{
// Code goes here
}
Lastly, we need to notify windows that whenever it receives a message that the “Show Name” button has been clicked, it should call our OnShowName function:
BEGIN_MESSAGE_MAP(CsampleDialog, CDialog)
ON_COMMAND(IDB_GETNAME, OnShowName)
END_MESSAGE_MAP()
In our example above, I have called my dialog class CSampleDialog. The OnShowName() function will be called whenever windows receives a message that the “Show Name” button has been clicked.
It’s no good having an empty OnShowDialog function. Our dialog box contains an EDITTEXT control, and a DEFPUSHBUTTON control. We have created the code to tell windows that it should execute the OnShowName function whenever the “Show Name” function is clicked on.
Let’s make our OnShowDialog function display the contents of our EDIDTEXT control:
Here’s the complete description of what the OnShowName function does:
CEdit* pName = (CEdit*)GetDlgItem(IDE_NAME);
The EDITTEXT control is accessed through MFC using the CEdit class. We are obtaining a pointer to our EDITTEXT control by using the GetDlgItem function. The GetDlgItem function takes the Id of the control to retrieve as its only parameter, and returns a pointer to a CEdit object. We use the old-style C casing method to cast the return value into a pointer to a CEdit object, which is derived from the CWnd class. This gives us access to the control through MFC. All controls have an equivalent MFC class implementation.
CString theText = "";
char buffer[100];
Next, we create two empty variables. One to hold the text in the EDITTEXT control, and the other to act as a buffer.
pName->GetWindowText(theText);
We use the pointer-to-CEdit object, pName, to get the value contained inside of the EDITTEXT control. The GetWindowText function retrieves the contents of the EDITTEXT control into the “theText” CString variable. If we wanted to manually set the value of the EDITTEXT control, we could use the SetWindowText function.
Lastly, we use the wsprintf function to create the caption of our message box and store it in the “buffer” character array. The MessageBox function shows the words “You entered: “, as well as the text obtained from our EDITTEXT control, as shown below:
There are other methods available through the CEdit class. You should explore these methods in detail if you want to come to grips with the EDITTEXT control.