Home arrow C++ arrow Page 3 - Using MFC in C++ Part 4: Controls, DDX and DDV
C++

Using MFC in C++ Part 4: Controls, DDX and DDV


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.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 60
December 17, 2001
TABLE OF CONTENTS:
  1. · Using MFC in C++ Part 4: Controls, DDX and DDV
  2. · Controls explained
  3. · The push button control
  4. · The check box control
  5. · The radio button control
  6. · The list box control
  7. · The group box control
  8. · DDX and DDV explained
  9. · Conclusion

print this article
SEARCH DEVARTICLES

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:

[DEF]PUSHBUTTON [Caption], [Control Id], [X Pos], [Y Pos], [Width], [Height], [Style Options]

To create a button that is selected by default, we would add a control as part of a dialog box in a resource file, like this:

DEFPUSHBUTTON "Show Name", IDB_GETNAME, 80, 18, 50, 15, WS_VISIBLE | WS_CHILD | WS_TABSTOP

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):

Our EDITTEXT and DEFPUSHBUTTON controls in action

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:

afx_msg void CSampleDialog::OnShowName()

{

CEdit* pName = (CEdit*)GetDlgItem(IDE_NAME);

CString theText = "";

char buffer[100];

pName->GetWindowText(theText);

wsprintf(buffer, "You entered: %s", theText);

MessageBox(buffer, NULL, MB_OK | MB_ICONEXCLAMATION);

}


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.

wsprintf(buffer, "You entered: %s", theText);

MessageBox(buffer, NULL, MB_OK | MB_ICONEXCLAMATION);


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:

Displaying the contents of our EDITTEXT control

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.
blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials