"Go" VS2010/MFC programming Getting Started eight (dialog box: Creating a dialog class and adding a control variable)

Source: Internet
Author: User

Original URL: http://www.jizhuomi.com/software/153.html

The first two talk about the chicken peck rice for everyone to explain how to create a dialog resource. When you create a dialog resource, you have to build the dialog box class. Chicken Peck Rice Again, the build dialog class includes the new dialog class, the Add control variable and the control's message handler function, and so on.

Because chickens peck rice to everyone's routines addition is a dialog-based program, so the program automatically creates a dialog template Idd_addition_dialog and automatically generates a dialog class Cadditiondlg, which is derived from the CDialogEx class. You've used vc++6.0. It is possible to remember that the dialog class we define is derived from the CDialog class, but in VS2010, the general dialog class is inherited from the CDialogEx class.

Create a dialog box class

How do you create a dialog class for a new dialog template that you add yourself?

1. First the chicken peck rice just as the sixth Lecture: Create a dialog template and modify the dialog box Properties said, in the resource view of the "Dialog" node right-click, and then select "Insert Dialog" in the right-click menu to create a new dialog box template, the ID will use the default Idd_ DIALOG1.

2. In the middle area, a new dialog template is displayed, then select the dialog template, right-click, and select Add Class from the context menu.

3. After selecting "Add Class", a dialog will pop up and a custom class name can be written in the edit box under "Class name" in the dialog box, for example CMyDialog.

4. The final point "finish" is complete.

Finally you can see the newly generated dialog class CMyDialog in Class View and have the corresponding MyDialog.h header file and MyDialog.cpp source file generated in the Solution Explorer. The CMyDialog class is also derived from the CDialogEx class.

Note that the general class names begin with C, and for example, Ctestdlg.

to add a variable to a control in a dialog box

Added several controls to the dialog box in the previous lecture, including three static text boxes, three edit boxes, and one button control. The Cancel button, which is automatically generated by the program, is retained as an exit button and the OK button is removed.

The static text box is just to illustrate the meaning of the data in the edit box immediately following it, is it addend, addend, and so they don't change, and we don't add variables for them. Button controls are used for manipulation, and no variables are added to them here. The data in the edit box may change frequently, and it is necessary to associate a variable for each of them.

First add a variable for the summand edit box Idc_summand_edit.

1. Right click on the edit box and select "Add Variable" in the right-click menu. The wizard dialog box for adding member variables pops up.

2. We want to add a value variable instead of a control variable for it, so select value in the combo box under Category in the dialog box.

3. The combo box under "Variable type" is now selected by default as "CString", CString is a string class and obviously cannot be additive. We can choose double, float, int, and so on. Here we select Double, which is the edit box to associate a variable of type double.

4. Write the custom variable name in "Variable name". The chicken pecked the rice for its name M_editsummand.

5. Click "Finish" to complete.

Note that the member variable name of a class is typically preceded by m_ to identify that it is a member variable.

Refer to this method, and then add the double type variable m_editaddend to the edit box for Addend, Idd_addend_edit, and the edit box for the double type variable m_editsum.

data Exchange and Validation for dialog classes

In the program run interface, the user tends to change the properties of the control, for example, to enter a string in the edit box, or to change the selection of the combo box, or to change the check box selection status. After the control's properties have changed, MFC modifies the value of the control's associated variable accordingly. This kind of synchronization change is implemented by MFC's member function DoDataExchange (), which is automatically generated by the dialog class, which is also called the data exchange and inspection mechanism of the dialog box.

After we have added variables for three edit boxes, we have three more ddx_text call statements in the function body of the DoDataExchange () function Cadditiondlg in AdditionDlg.cpp. Here is the function body code and the comment added by the Chicken Peck meter.

C + + code
  1. void Cadditiondlg::D odataexchange (cdataexchange* PDX)
  2. {
  3. //Handle MFC's default data exchange
  4. CDialogEx::D odataexchange (PDX);
  5. //Handle data exchange between the control Idc_summand_edit and the variable M_editsummand
  6. DDX_Text (PDX, Idc_summand_edit, M_editsummand);
  7. //Handle data exchange between the control Idc_addend_edit and the variable m_editaddend
  8. DDX_Text (PDX, Idc_addend_edit, m_editaddend);
  9. //Handle data exchange between the control Idc_sum_edit and the variable m_editsum
  10. DDX_Text (PDX, Idc_sum_edit, m_editsum);
  11. }

Chicken Peck Rice and then take the addition procedure as an example simply say the data exchange mechanism. If we enter Summand in the program runtime interface, the input value can be saved to the M_editsummand variable through the caddition's DoDataExchange () function, or vice versa if the value of the variable M_editsummand is modified in the program's run. The caddition DoDataExchange () function can also display the new variable values in the Summand edit box.

But in this data exchange mechanism, DoDataExchange () is not called automatically, but requires us to call the Cdialogex::updatedata () function in the program , by UpdateData () The function then goes to automatically call DoDataExchange ().

The prototype of the Cdialogex::updatedata () function is:

BOOL UpdateData (bool bsaveandvalidate = TRUE);

Parameters: bSaveAndValidate is used to indicate the direction of the data transfer, true to pass from the control to the variable, false to pass from the variable to the control. The default value is True, which is passed from the control to the variable.

Return value: The return value of the Cdialogex::updatedata () function indicates whether the operation was successful, returns true if successful, or false otherwise.

In the next lecture, the Chicken Peck meter will demonstrate how the Cdialogex::updatedata () function is used.

Chicken Peck Rice This section is about creating a new dialog class and adding a control variable, and the message handler function for the control will be described in more detail in the next lecture. Still welcome everyone often back to chicken peck Rice Blog Study and discussion.

"Go" VS2010/MFC programming Getting Started eight (dialog box: Creating a dialog class and adding a control variable)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.