VS2010/MFC dialog box: Adding a message handler function to a control

Source: Internet
Author: User

To add a message handler function to a control

Creating a dialog class and adding a control variable in the previous lecture, the main content of this lecture is how to add a message handler function to a control.

MFC defines a number of messages for dialog boxes and controls, and we trigger messages when they are manipulated, which are ultimately handled by the message handler function. For example, when we click on the button will generate bn_clicked message, modify the contents of the edit box will produce En_change messages. In general, in order for an operation to be effective, we only need to implement the message handler for a message.

I. Adding a message-handler function

As an example of the previous addition Calculator program, explain how to add a message handler function for the Calculate button control. The Add method lists 4 kinds:

1. Adding a message handler function using the Class Wizard

The use of VC + + 6.0 friends should be familiar with the Class wizard, add classes, message processing functions, etc. often used to it, can be said to be a very core function. But from the beginning of VS2002 to see the Class wizard, most of the functions are integrated into the properties of dialog boxes and controls, and easy to use. To VS2010, the long-lost Class wizard came back. But chicken peck rice has been used to the use of properties in the function, for from VC + + 6.0 direct to VS2010 friends may feel or use the Class Wizard to compare habits.

You should remember that the "Calculate" button has the ID of Idc_add_button, the commands tag in the Oject IDs list has this ID, because we want to implement the message processing function after the click button, so select Bn_clicked message in the Messages list , and then click Add Handler on the top right of the BN_CLICKED message handler function to Onclickedaddbutton. Of course you can also change the name, but the general use of the default can be.

2. Add Message handler function via "Add Event Handler ..."

Right-click on the Calculate button and select menu item "ADD event Handler ..." In the right-click menu to bring up the "Event Handler Wizard" dialog box, such as:

It is visible that the bn_clicked message is selected by default in the "Message type", and the function name and class are given automatically, just click "Add and Edit".

3. Add a message handler function in the Properties view of the button

As stated above, the message handlers are added from the Properties view primarily from the VS2002. We right-click on the "Calculate" button, select "Properties" in the right-click menu, and the button's property view will be displayed in the right panel.

We can point to the Control Events button in the Properties view (similar to the lightning bolt) as in, and the following lists all the messages for the Calculate button. We have to deal with the bn_clicked message, click on its right blank list item, a button with the arrow next to it, click this button will appear "<Add> onbnclickedaddbutton" option, and finally Select this option will automatically add Bn_ Clicked the processing function.

4. Double-click the button to add the message handler function

The most straightforward and straightforward approach is to double-click the Calculate button, and MFC will automatically add a handler function Onbnclickedaddbutton () for the bn_clicked message to it in the Cadditiondlg class.

two. Adding custom features to message handlers

After we have added the message handler function in either method, we can only get an empty function body of the Onbnclickedaddbutton () function, we need to add the custom function code in the function body to implement the function we want.

In the addition Calculator program, we want the "calculate" button to implement the function is to get the values of Summand and Addend, and then calculate their sum and display to the and the edit box. Then, the function Body of Onbnclickedaddbutton () should be modified to:

C + + code
  1. void Cadditiondlg::onbnclickedaddbutton ()
  2. {
  3. //Todo:add your control notification handler code here
  4. //Save the data in each control to the appropriate variable
  5. UpdateData (TRUE);
  6. //Will be added and assigned to M_editsum by Addend and Addend
  7. M_editsum = M_editsummand + m_editaddend;
  8. update the corresponding control according to the values of each variable. And the edit box will display the value of the M_editsum
  9. UpdateData (FALSE);
  10. }

Comments have been added to the code above, and you should be very easy to understand. The description of the UpdateData () function has been described in the previous lecture, and if you forget, you can go back to the previous section to learn more.

Next we run this application. In the Run results interface, enter Summand 5.1, addend 2.3, then click "Calculate":

As you can see, after clicking the "Calculate" button, the correct result is displayed in the edit box: 7.4.

Simple analysis of the running process: Enter Summand and Addend, click the "Calculate" button to generate a hit message, thereby calling the Onbnclickedaddbutton () function. After entering this function, first by the UpdateData (TRUE) function will be Addend value 5.1 and Addend value 2.3 respectively saved to the variable M_editsummand and m_editaddend, and then through the statement m_editsum = M_editsummand + M _editaddend, calculate the Summand and Addend and the 7.4, and assign 7.4 to m_editsum. Finally call UpdateData (FALSE) to update the display values of the three edit boxes based on the values of Summand, Addend, and, and the results are obtained.

In this respect, an addition calculator application with a simple addition function is basically done. If you want to implement additional functionality, you can modify the control resources and message handler functions to practice.

VS2010/MFC dialog box: Adding a message handler function to a control

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.