Function prototype: Virtual bool pretranslatemessage (MSG * PMSG)
1. In MFC, pretranslatemessage is a virtual function used to intercept messages. We can reload it to process keyboard and mouse messages. In the SDK, this is different and we must send a response.
Number of lresult callback wndproc (hwnd, uint message, wparam, lparam) processes messages. It is similar to pretranslatemessage, but the MFC encapsulation is better.
2. reload this function to filter window messages before being distributed to the window functions translatemessage () and dispatchmessae (). The default implementation is to complete the translation of the acceleration key.
3. This function indicates the operation performed before Message Processing (such as translatemessge () and dispatchmessage (). If the return value of this function is true, message processing is terminated and no translatemessge () is called () and dispatchmessage () to translate and distribute messages to the corresponding window. If the return value is false, the function of translation and message sending will be called.
4. in Win32 programs, there are two message transmission methods:
A. MFC message, MFC will put all the messages in one afx_msgmap_entry structure to form an array, which stores all the messages and their related parameters. It can also be said that the message is put into the message queue. B. messages sent directly to the window without passing through the Message Queue using sendmessage () or other similar methods. Only messages that pass through the message queue are affected by pretranslatemessage (). messages of the second type do not ignore the existence of pretranslatemessage.
1. Whether to call translatemessage () and dispatchmessage () is determined by the return value of the pretranslatemessage () function. If this function returns true, the message is not distributed to the window function for processing.
2. The message sent to pretranslatemessage () is an untranslated message that has not passed through
Translatemessage () processing. For example, you can use (PMSG-> wparam = vk_return) in this function to intercept the Enter key, where wparam stores the virtual code of the characters on the keyboard.
3. wm_char messages cannot be processed in windowproc. (For the windowproc function, see the message response mechanism of MFC)
4. setwindowtext will send wm_char to the window.
The difference between peekmessage and getmessage:
Getmessage waits for a message when there is no message, and the CPU usage is low.
When peekmessage does not have a message, it is immediately returned, so the CPU usage is high. Because the game cannot be driven by Windows messages, you must use peekmessage ();
Another article:
In a Win32 program, Windows will pass the message to the corresponding window. However, the message is not immediately transmitted to the corresponding window, but is passed to the next level window from the top-level window of the entire program, and then to the next level window until it is passed to the target window. During the entire process, some messages cannot be delivered to the target window by default in some specific circumstances. For example, you can press the Enter key and the cancel key in the edit control. If there is a dialog box before the edit window, the dialog box will process the return message by default (that is, the onok function is returned, and then the dialog box is closed ), then Exit message transmission. Therefore, edit cannot receive the message. To solve this problem, you can reload the pretranslatemessage function in all the dialog boxes before the edit window, and then add:
If (PMSG-> message = wm_keydown & PMSG-> wparam = vk_return) // if the message type is
Wm_keydown // and press ENTER
Return false; // if the message is not translated, the message is directly transmitted. For details, refer to msdn. Note: The returned value here cannot be true. True means to Exit message transmission after the message is translated, so that, although it can also avoid the default processing of the dialog box, the message will exit transmission, in this way, the Edit Control still does not receive messages. In this way, messages can be transmitted without the default processing in the dialog box. Note: Only the dialog box can process the messages by default by pressing the carriage return and cancel. Other control windows do not. Therefore, you do not need to reload the pretranslatemessage function in other windows. Of course, if the function is reloaded, it will not be wrong. Appendix: sample programs for the pretranslatemessage () function:
Bool csearchuserdlg: pretranslatemessage (MSG * PMSG)
{
If (PMSG-> message = wm_keydown) // determines whether a key is pressed
{
Switch (PMSG-> wparam)
{
Case vk_down: // indicates the downward key in the direction key. // Add handle code here
Break;
Case vk_up: // indicates the up key in the direction key. // Add handle code here
Break;
Default:
Break;
}
}
}
**************************************** ******************************
Peekmessage is generally used to reload the message loop to prevent the program from stopping the response. Example:
MSG;
If (: peekmessage (& MSG, null, 0, 0, pm_remove ))
{
If (msg. Message = wm_quit)
{
: Postquitmessage (-1 );
}
If (! Afxgetapp ()-> pretranslatemessage (& MSG ))
{
: Translatemessage (& MSG );
: Dispatchmessage (& MSG );
}
}
**************************************** **************************************
There are many controls on the interface, such as Edit. How do I know which control is the current focus?
1. getfocus () can get the handle of the current control, and you can enumerate the sub-window handle to determine which is in the same position as the obtained handle.
2. cwnd * pctrl = cwnd: getfocus ();
Int ictrlid = pctrl-> getdlgctrlid ();
If (ictrlid = idc_edit1 ){}
Else if (ictrlid = idc_edit2 ){}...
3. getfocus (): determines the control on which the current focus is located. If the function is successfully executed, the window handle of the currently obtained focus control is returned. If an error occurs, the system returns an invalid reference.
Usage: The application uses the isvalid () function to check whether getfocus () returns a valid control reference. You can also use the typeof () function to determine the type of the control.
Functions and usage of pretranslatemessage