MFC message processing and SDK Message Processing

Source: Internet
Author: User

In the MFC implementation, message ing is used to respond to messages, thus changing the loop structure in the SDK, making the message flow more concealed.
The SDK consists of three parts:

Wndclass WND;
WND. lpfnwndproc = wndproc; // set the callback function first.

SDK loop structure for message loop
While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Disptachmessage (& MSG );
}
The callback function is implemented in another place:
Lpresult callback wndproc (hwnd, uint MSG, wparam, lparam)
{
Switch (MSG)
{
Case wm_create:... break;
Case wm_paint:... break;
Case wm_lbuttondown:... break;
Case wm_lbuttonup:... break;
Case wm_mousemove:... break;
Case wm_char:... break;
Case wm_timer:... break;
Case wm_close:... break;
Case wm_destroys:... break;
Case wm_quit:... break;
Case wm_command:
Switch (loword (wparam ))
{
Case id_xxx: break;
Default: break;
}
Break;
Default: break;
}
Defwindowproc (hwnd, MSG, wparam, lparam );
}

The message ing is:
In the MFC class definition structure, declare a statement:
Declare_message_map ()
Then message ing is performed using the following statement in the global space implementation:
Begin_declare
On_wm_lbuttondowm ()
On_wm_lbuttonup ()
On_wm_mousemove ()
On_wm_paint ()

On_command (ID, lpfunc)
End_decalre

In the implementation of class member functions, the above message ing functions are implemented. Common commands have fixed message names:
On_wm_lbuttondown () ing function: onlbuttondown () function, callback ing function: lbuttonup () function, on_wm_mousemove () ing function: onmousemove () function, on_wm_paint () ing Function () the ing function in on_command is the lpfunc function.

In this case, the message ing declaration is modified in classwizard instead of being manually modified. This allows you to avoid missing a part or some minor bugs during input.

The message flow of MFC is from the current type of APP to the cwndapp, and then to the cwndthread class. When the message flow to the csf-target class is not consistent, match the message when it is transferred to cwndthread.

Although the MFC message is hard to understand and the message is relatively complex, the emergence of the MFC message function makes the implementation of each message very independent, it is much clearer than a series of message judgments in the SDK.

In simple terms, set the message ing in MFC to such a structure:
Struct afx_msgmap
{
Afx_msgmap * pbasemessagemap;
Afx_msgmap_entry * lpentries;
};
Struct afx_msgmap_entry // MFC 4.0 format
{
Uint nmessage; // WINDOWS Message
Uint ncode; // control code or wm_policy code
Uint NID; // Control ID (or 0 for Windows messages)
Uint nlastid; // used for entries specifying a range of Control ID's
Uint nsig; // signature type (action) or pointer to message #
Afx_pmsg PFN; // routine to call (or special value)
};
Typedef void (csf-target: * afx_pmsg) (void );
# Define declare_message_map ()\
Static afx_msgmap_entry _ messageentries []; \
Static afx_msgmap messagemap ;\
Virtual afx_msgmap * getmessagemap () const;
Afx_msgmap_entry is another data structure:
Afx_pmsg is defined as a function pointer:
Then we define a macro:
# Define declare_message_map ()\
Static afx_msgmap_entry _ messageentries []; \
Static afx_msgmap messagemap ;\
The data structure is filled with three macros:
# Define begin_message_map (theclass, baseclass )\
Afx_msgmap * theclass: getmessagemap () const \
{Return & theclass: messagemap ;}\
Afx_msgmap theclass: messagemap = \
{& (Baseclass: messagemap ),\
(Afx_msgmap_entry *) & (theclass: _ messageentries )};\
Afx_msgmap_entry theclass: _ messageentries [] = \
{
# Define on_command (ID, memberfxn )\
{Wm_command, 0, (Word) ID, (Word) ID, afxsig_vv, (afx_pmsg) memberfxn },
# Define end_message_map ()\
{0, 0, 0, 0, afxsig_end, (afx_pmsg) 0 }\
Rtual afx_msgmap * getmessagemap () const;
};

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.