1. Command Message (WM_COMMAND)
All classes derived from c1_target are eligible to accept WM_COMMAND.
2. Window message (WM_xxx)
All classes derived from CWnd are eligible to accept WM_xxx.
3. Control Message (WM_NOTIFY)
The widget sends a notification to its parent window.
Message Processing
1. WM_xxx Message Processing
Window class (itself) Processing → base class processing → CWnd includefwindowproc () processing;
The corresponding macro is like adding ON _ before the message WM _.
2. Command Message Processing
The command message comes from the WM_COMMAND message sent from the command User Interface object (menu, acceleration key, or toolbar button;
(I) WM_COMMAND message
The types and corresponding macros are as follows:
① ON_COMMAND (ID, pfn)
Standard Command Message;
② ON_COMMAND_EX (ID, pfn)
Multiple command IDS provide the same processing. Each ID must use an ON_COMMAND_EX independently. IDS can be discontinuous and multiple objects can process the same command ID;
The function prototype is as follows:
Afx_msg BOOL pfn (UINT nID)
Note:
If the return value is TRUE, it indicates that the command has been processed. You do not need to process the command in the message processing chain. If the return value is FALSE, it indicates that the command will continue to be processed in the message processing chain.
Note:
1. This macro must be used in multi-object processing;
Second, the returned value of pfn (UINT nID) (message processing function) changes its void type to BOOL, and must be FALSE;
Third, the process of processing multiple objects is from the top to the bottom: View class → main framework window class → application class;
③ ON_COMMAND_RANGE (nID, nLastID, pfn)
Multiple command IDS provide the same processing, and multiple objects process the same command ID;
Note:
1. Ensure that the nID and nLastID values are continuous in Resource. h.
Second, we generally add a parameter to the function pfn (UINT nID) to determine which button to click.
(Ii), CN_UPDATE_COMMAND_UI message
A message corresponding to a menu item, toolbar button, or other [command User Interface object] to update its status. The type and macro contained in the message are as follows:
① ON_UPDATE_COMMAND_UI (ID, pfn)
The function prototype is as follows:
Afx_msg void pfn (CCmdUI * pCmdUI)
② ON_UPDATE_COMMAND_UI_RANGE (nID, nLastID, pfn)
This function can process the appearance of a group of [command user interface objects;
The function prototype is as follows:
Afx_msg void pfn (CCmdUI * pCmdUI)
Important:
The m_nID member in CCmdUI represents different IDs, so it can be used for differentiated processing.
3. Notification messages of controls
WM_COMMAND notification message sent from the control and subwindow to the parent window (that is, the notification code of the control is added to the sending command message ).
Note: In the new Window9x control, WM_COMMAND notification messages are not sent, but WM_NOTIFY messages are sent. To ensure compatibility, the old control still sends WM_COMMAND messages.
For example:
The CEdit control sends the WM_COMMAND message of the EN_CHANGE notification code to the parent window.
Note: The Framework sends notification messages like other WM _ messages, but with one exception, the BN_CLICKED notification message sent by the [button] control is specially processed as a command message.
(I) The macros corresponding to WM_COMMAND are as follows:
① ON_CONTROL (Notification code, nID, fn)
② ON_CONTROL_RANGE (Notification code, nFirstID, nEndID, fn)
Note:
The applications of these two macros are the same as those of ON_COMMAND and ON_COMMAND_RANGE. The difference is that the [Notification Code] is added before the macro.
Note: You can derive a specific macro Based ON the [Notification Code] of different controls. The derived macro is generally added with ON _ before the [Notification Code _.
(Ii). The macros corresponding to WM_NOTIFY are as follows:
① ON_NOTIFY (Notification code, nID, fn)
The function prototype is as follows:
Afx_msg void fn (NMHDR * pNotifyStruct, LRESULT * result)
Structure:
Typedef struct tagNMHDR {
HWND hwndFrom; // handle of the control that sends the notification message;
UINT idFrom; // the ID of the control that sends the notification message;
UINT code; // The Notification code;
} NMHDR;
② On_policy_ex (Notification code, nID, fn)
Indicates that a message is processed in the member functions of multiple objects.
The function prototype is as follows:
Afx_msg BOOL fn (UINT nID, NMHDR * pNotifyStruct, LRESULT * result)
Note:
It must return a value of the BOOL type, which is the same as ON_COMMAND_EX.
③ On_policy_range (Notification code, nFirstID, nEnd, fn)
Indicates that the notification messages of multiple controls are processed in the same function.
The function prototype is as follows:
Afx_msg void fn (UINT nID, NMHDR * pNotifyStruct, LRESULT * result)
Note:
It is the same as ON_COMMAND_RANGE.
4. reflected Message Processing
When the parent window processes the WM_CTLCOLOR, WM_COMMAND, and WM_NOTIFY notification messages in the control window, the parent window converts the message to a reflection message and transfers it to the control subwindow for processing, the parent window can be processed only when the control subform does not process the message.
Note: You can check the reflected message on the message page in the "class properties" dialog box (with the "=" sign before)
① WM_CTLCOLOR_REFLECT reflection message
The corresponding macro is as follows:
ON_WM_CTLCOLOR_REFLECT ()
Prototype of the reflected message function:
HBRUSH class_name ∷ CtlColor (CDC * pDC, UINT nCtlColor)
{
Return NULL;
}
This function is used to reset the color of the Control. Note: it is valid only when return CBrush is used.
5. Custom window messages
Message logos of custom window messages are greater than WM_USER (at least WM_USER + 100, because many controls use WM_USER messages in this range)
Custom messages are divided into two steps:
① Define message tag in Resource. h
# Define WM_MYMSG (wm_user+ 1000)
② Add a message ing macro to the Message ing table
BEGIN_MESSAGE_MAP ()
ON_MESSAGE (WM_MYMSG, fn)
END_MESSAGE_MAP ()
Note:
The corresponding macro is on_message (), and its member function prototype is:
Afx_msg lresult FN (wparam, lparam)
6. register the message
① Register and obtain the message mark of a registration message in the system
Uint registerwindowmessage (lpctstr)
Note:
You can use the API function to register the message tag. The message tag is an arbitrary string of the user. For example:
Uint wm_mymsg = registerwindowmessage ("mymsg ");
Wm_mymsg indicates the message tag of a custom unsigned integer.
② Add a message ing macro to the Message ing table
Begin_message_map ()
On_registered_message (wm_mymsg, FN)
End_message_map ()
Note:
The corresponding macro is on_registered_message (), and its member function prototype is:
Afx_msg lresult FN (wparam, lparam)
Note: Message registration can achieve cross-process data communication.
7. Thread message
Only the cwinthread class can process thread messages.
1. Define the message mark of the thread
There are two methods:
(1) Use a custom message tag, namely, WM_USER;
(2) Use the registered message tag: RegisterWindowMessage;
② Add macros to the Message ing table of the CWinThread inheritance class
ON_THREAD_MESSAGE (message tag, fn) // custom message;
ON_REGISTERED_THREAD_MESSAGE (message tag, fn) // registered // message
③ Its function prototype is as follows:
Afx_msg void fn (WPARAM wPARAM, LPARAM lParam)
④ Thread messages
Thread messages must be triggered by calling the PostThreadMessage of the CWinThread class to deliver messages to the thread message queue.
Note: You can use the AfxGetApp () function to obtain a global application object.
PostThreadMessage (UINT, WPARAM, LPARAM)
8. WM_COPYDATA
The operating system maintains a piece of memory to manage WM_COPYDATA messages. This message is mainly used to transmit data across processes, and the amount of transferred data reaches 232.
① Define a COPYDATASTRUCT Data Structure
Typedef struct tagCOPYDATASTRUCT {
DWORD dwData; // custom special data;
DWORD cbData; // The lpData size in bytes;
PVOID lpData; // pointer of the transmitted data memory block;
} COPYDATASTRUCT;
② The corresponding macro
ON_WM_COPYDATA ()
③ Prototype of the corresponding function
Afx_msg BOOL OnCopyData (CWnd *, COPYDATASTRUCT *)
Note:
CWnd *: pointer to the window for sending the message;
9. Deliver And send messages
By posting or sending messages to a form, you can indirectly drive the form message process.
Post message: place the message in the message queue of the thread, and then return it directly to the caller without waiting for the thread to process the message.
SendMessage: When a thread sends a message to the target thread, the thread waits until the target thread processes the message.
1. deliver messages
BOOL CWnd receive PostMessage (UINT, WPARAM = 0, LPARAM = 0)
Note:
CWnd: target window;
This function puts a message into the message queue of the application, and then directly returns the message without waiting for window processing.
② Send messages
LRESULT CWnd receive SendMessage (UINT, WPARAM = 0, LPARAM = 0)
Note:
CWnd: target window;
This function puts a message into the message queue of the application. It is returned only after the window is processed.
To prevent threads from waiting permanently, use SendMessageTimeout instead of SendMessage:
LRESULT SendMessageTimeout (HWND, UINT, WPARAM, LPARAM, UINT, UINT, PDWORD_PTR)
Note:
HWND: Window handle;
UINT: Specifies the message sending option. If it is SMTO_BLOCK, it can prevent the thread from waiting without limits, that is, it is returned based on a certain timeout value.
UINT: timeout, in milliseconds;
PDWORD_PTR: return value;
Note: CWnd does not wrap the function.
③ Deliver and send messages
BOOL CWnd receive sendpolicymessage (UINT, WPARAM, LPARAM)
Note:
CWnd: target window;
The message has two functions: SendMessage and PostMessage:
When the target window and the sending window are in the same thread, it is equivalent to the SendMessage function; otherwise, it is PostMessage function when not the same thread.
6-1. Deliver And send WM_XXX messages
When sending a standard WINDOW message, you only need to place the ID, wParam, and lParam parameters of the message in the corresponding positions of the SendMessage () and PostMessage () functions.
6-2. Deliver And send command messages and control notification messages
When a command message is delivered and sent, the Message ID is wm_commadn. For different menu items, acceleration keys, and controls, the values of wparam and lparam are different.
Wparam is divided into two parts: low and high. The lower part is the menu item, acceleration key, and Control ID. For the upper part, set menu item: 0; acceleration key: 1; Control: Notification code.
Lparam: when the control is used, it is the control handle; otherwise, it is null.
You can use custom macros for wparam parameters:
Wparam makewparam (word wlow, word whigh) sendmessage (wm_command, makewparam (getdlgctrlid (), bn_clicked), (lparam) m_hwnd );
6-3. Deliver And send custom window messages
When a custom window message is delivered and sent, wparam and lparam parameters have no special meaning, and only transmit data like the common function parameters.
Note:
Postmessage and sendmessage are different. The former is returned after delivery, and the latter must wait until the message is processed. Therefore, when the parameter is [partial] or [temporary, using the postmessage function will cause an error (unless the parameter uses a pointer, you can avoid the error). Instead, you must use the sendmessage function.
6-4. Deliver And send registered window messages
It is basically the same as 6-3, but it should pay special attention to the problem that, during cross-process message processing, if the message is postmessage and sendmessage to a process, the window class name of process A must be obtained in process B, the window class name must be used to obtain the window pointer, and then the postmessage and sendmessage functions must be called Based on the pointer.
Note: You can obtain the window pointer Based on the window class name or window title.
6-5. Deliver And send WM_COPYDATA messages
SendMessage (message tag, WPARAM, LPARAM)
Where:
Message Tag: WM_COPYDATA;
WPARAM: The Window handle for sending the message;
LPARAM: a pointer to the COPYDATASTRUCT structure. It is converted through (LPVOID) and then through (LPARAM) in the following form:
(LPARAM) (LPVOID) & cds from http://blog.csdn.net/na_he/archive/2008/05/07/2410240.aspx