What is afx_msg in MFC and what does afx_msg void function () mean?

Source: Internet
Author: User

 The message map function generated by the application framework 
for example: afx_msg void OnBnClickedButton1 (); Where afx_msg is the message flag, It declares to the system that there is a message mapped to the function implementation body, whereas in the map macro definition there is a specific message and a mapping definition for this function (either custom or automatic)
AFX is the application framework
If you define an edit change handler function:
One, add in the header file (*.h) of the class:
//{{afx_msg (cdialogdemo)
afx_msg void OnChangeEdit1 ();
{{afx_msg
II, added in the class's implementation file (*.cpp):

1. Message definition (on_en_change):
Begin_message_map (Cdialogdemo, CDialog)
{{Afx_msg_map ()
On_en_change (idc_edit1, OnChangeEdit1)
//}}afx_msg_map
End_message_map ()
2. Execute function: Br>void cdialogdemo::onchangeedit1 ()
{
//Todo:add your control notification handler code here
...
}

Explanation of afx_msg in AFXWIN.h:
#ifndef afx_msg
#define AFX_MSG//Intentional Placeholder
#endif
doesn't mean anything. It just defines the symbol. This is the equivalent of nothing for the compiler, and for people, we can see symbols like this.
for the Class wizard. This symbol is meaningful. It is a prefix for a message handler function. The message functions generated by the Class Wizard, the distribution functions, and the event response functions are prefixed with this. If it is removed, the wizard will not recognize
How to add a message response function for 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

Chicken Peck Rice still takes the procedure of the previous addition calculator as an example of 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. }

Chicken Peck Rice In the above code has been added comments, you should be 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.

Chicken Peck Rice Simple analysis of the running process: Enter Summand and Addend, click "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.

==============

If you use the Class Wizard, you will automatically add

Automatically add message function mappings

What is afx_msg in MFC and what does afx_msg void function () mean?

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.