Use the dialog editor and classwizard (note 2)

Source: Internet
Author: User

For most Windows standard controls, we generally use the dialog box editor to add them to the dialog box.

Figure 6. 1 select dialog box in resourceview
Idd_dialogdemo_dialog

Figure 6. 2 Properties dialog box of the control


Figure 6. 3 The controls tool window in the dialog box Editor


In the following process, we add an edit box control to the Main Dialog Box of the dialog box-based MFC framework application created in Chapter 4.

1. In the resourceview option of the workspace window, double-click the idd_dialogdemo_dialog icon under the dialogdemo resources \ dialog node. The operation above.

2. Click "do ......" Static text control. Right-click and select Properties from the context menu to open the dialog box shown in 6.2, and enter the new control text in the caption text box: "Enter some characters in the text box below ", drag the static text control to the upper left corner of the dialog box.

3. from the controls tool window (6.3, if you cannot see the tool window in your resource editor, you can right-click the toolbar and select controls from the context menu) and select the Edit Control icon, draw an edit box control in the dialog box, as shown in Figure 6.4.

In the General tab of the Properties window of the control in the editing box, enter idc_edit as its ID. On the styles tab, tick the multiline check box and remove the check box before the auto hscroll check box.

4. Right-click the edit box control and select the classwizard command from the context menu to open the classwizard dialog box, which looks like 6.5.


Figure 6. 4 Add an edit box control to the dialog box


Figure 6. 5 classwizard dialog box

Click the member variables tab. Make sure that dialogdemo is selected for the project and cdialogdemodlg is selected for the class name. Now we add a data ing entry for the added edit box control idc_edit. Select idc_edit in control IDs and click Add viable on the right. Open the dialog box 6.6.

Link the variable name m_stredit at the membervariable name (here M indicates that this variable is a member variable of the cdialogdemodlg class, STR indicates that its type is string, that is, class cstring ), select value from the category drop-down list (another option is control. The differences between the two options will be described in the following content ), select cstring from the variable type drop-down list (there are many other data types to choose from, but the content in the edit box here is a string, so cstring is the most appropriate choice ). Click OK to close the dialog box.


Figure 6. 6 Add member variables for control ing

5. Check whether the current classwizard dialog box (Figure 6.7) is different from Figure 6.5. In the dialog box shown in Figure 6.7, enter 50 in the maximum characters text box below. You can easily guess the meaning of the string by literal meaning. The maximum size of the possible longest string in idc_edit is 50. Click OK to close the dialog box.


Figure 6. 7 use classwizard to set the data verification scheme

6. Double-click the oninitdialog member function of the cdialogdemodlg class from the classview In the workspace window, and use the following code to replace the statement

Return true;

Note before // todo:

M_stredit = "Hello! Enter some strings here. ";

Updatedata (false );

7. In classview, double-click the initinstance member function of the cdialogdemoapp class and use the following code to find the Parameter

If (nresponse = idok)

/// Todo comment below:

Afxmessagebox (DLG. m_stredit );

Delete (or comment out) the following code lines in the same member function ):

M_pmainwnd = & DLG;

8. Compile and run the application. The dialog box 6.8 is displayed.


Figure 6. 8 running result of the sample program dialogdemo

Enter some characters in the text box shown in Figure 6.8 and click OK ". The message box shown in 6.9 is displayed. The message box retells the user's input in the dialog box shown in Figure 6.8. We also found that in the dialog box shown in Figure 6.8, when the input string reaches a certain length, we cannot enter more characters, this is the result of setting maximum characters to 50.


Figure 6. 9 feed back the input string in the form of a message box

Next, let's take a look at what has been done in the above steps. First, we use the resource editor to add these standard controls to the dialog box template. The concept of this step is clear, so it is not difficult to understand.

Then, the properties dialog box is displayed. Set the Control ID to idc_edit. If the header file resource. H is opened, the macro idc_edit is defined as constant 1001. However, in fact, in many cases, we do not need to care about the specific value of each control's ID, but only need to remember the corresponding mnemonic. For the edit box control, you only need to remember idc_edit, and do not need to care that it is 1001. Next, we set the multiline attribute in the styles tab and clear the auto
The hscroll attribute. The edit box idc_edit supports multiple lines of text. If the length of a text line exceeds the width of the edit box, the row is automatically returned.

The following steps are the most important. We have used classwizard, a powerful tool. First, we associate the edit box with a cstring object, which uses a mechanism called dialog data exchange (DDX. In this mechanism, we first initialize the member variables of the dialog box object in the oninitdialog processing function or the constructor of the dialog box class. Before the dialog box is displayed, the ddx mechanism of the framework passes the value of the member variable to the control in the dialog box. This process occurs when the member function domodal or create is called. By default, the oninitdialog member function in the cdialog class calls the cwnd member function updatedata to initialize controls in the dialog box. At this time, we can see that the previous step 6th still has the following solutions:

1. Set the code line

M_stredit = "Hello! Enter some strings here. ";

Before calling the oninitdialog member function of the base class, that is, before the following code:

Cdialog: oninitdialog ();

2. Change the code

M_stredit = "Hello! Enter some strings here. ";

Move to the constructor class cdialogdemodlg.

For the above two methods, we do not need to call the member function updatedata of the cwnd class compared to the method used in the previous step 6th. This function will be called in oninitdialog, a member function of the cdialog class.

There is no clear distinction between the three methods. In many cases, they apply to different scenarios.

Here we will talk about the member function updatedata. This function has a Boolean parameter. If this parameter is set to false, updatedata transfers the value of the member variable to the variable in the dialog box. If this parameter is set to true, the updatedata function performs the opposite process.

If you click the idok button in the dialog box or call the updatedata function with the true parameter, the DDX Mechanism transfers the value from the control to the member variable, at the same time, the dialog box data validation (DDV) mechanism verifies all data items according to the set verification rules.

During data exchange, the member function updatedata first creates a cdataexchange object, and then calls the dialog box to reload the version of the member function dodataexchange of the class cdialog. The cdataexchange object is used as a parameter of the member function dodataexchange, which defines the context of data exchange.

In dodataexchange, we specify a call to the DDX function for each data member. Each function defines two-way data exchange based on the context determined by the cdataexchange parameter provided by the updatedata member function.

The following code is taken from the implementation file dialogdemo. cpp to define the function dodataexchange:

Void cdialogdemodlg: dodataexchange (cdataexchange * PDX)

{

Cdialog: dodataexchange (PDX );

// {Afx_data_map (cdialogdemodlg)

Ddx_text (PDX, idc_edit, m_stredit );

Ddv_maxchars (PDX, m_stredit, 50 );

//} Afx_data_map

}

The Code section between the two lines of comments/{afx_data_map and //} afx_data_map is called data ing. The ddx_text function uses the cstring object m_stredit to associate it with the edit box control with ID idc_edit. The function ddv_maxchars sets the maximum length of the cstring object m_stredit associated with the idc_edit control in the editing box to 50.

Note that if you click the "cancel" button in the mode dialog box, the domodal function returns the idcancel value. In this case, data exchange does not occur before the dialog box and dialog box objects.

For this reason, if the domodal function returns the value idok, we can use the following code to repeat the value entered in the dialog box:

Afxmessagebox (DLG. m_stredit );

  • Note:
  • There is a confusing process in the previous step, which is why we need to delete the following code from the oninitdialog function:
  • M_pmainwnd = & DLG;

This is based on the following fact:

M_pmainwnd, a data member of the cwinthread class, has a useful feature. If the window referenced by this member is closed, the MFC Library will automatically terminate the thread represented by the cwinthread object. In this way, if we assign the member variable m_pmainwnd to the pointer pointing to the DLG, the main thread of the application will be automatically terminated no matter whether we click confirm or cancel, the subsequent code will certainly not be executed. In this example, we want the program to continue running after the dialog box is closed (that is, a message is displayed to repeat the content entered by the user ), therefore, you should not assign the DLG object pointer to the member variable m_pmainwnd, So that you need to delete the previous code from the oninitdialog function.

 

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.