The dialog box in MFC does not directly respond to the onchar and onkeydown messages and is blocked by other controls. During the test, it is found that the two functions are not entered. You must re-define the pretranslatemessage () virtual function to correctly
Go to the two message functions. The specific implementation is as follows:
Bool ctestdlg: pretranslatemessage (MSG * PMSG)
{
Sendmessage (PMSG-> message, PMSG-> wparam, PMSG-> lparam );
Return 0;
// Return cdialog: pretranslatemessage (PMSG );
}
In this way, the program can correctly call the onkeydown and onchar functions, and onkeydown () is processed before oncchar. In this way, you can respond to the wm_char and wm_keydowm messages in the dialog box. Note that three messages are actually sent when wm_char is sent.
Wm_char wm_keydowm wm_keyup
Therefore, if you have the following code:
Void cmy1dlg: onchar (uint nchar, uint nrepcnt, uint nflags)
{
// Todo: add your message handler code here and/or call default
If (nchar = 'A ')
MessageBox ("");
CDialog: OnChar (nChar, nRepCnt, nFlags );
}
Two message dialog boxes will pop up, so it is best to process the event separately in WM_KEYDOWM or WM_KEYUP.
PreTranslateMessage is called before a message is sent to the TranslateMessage function. The vast majority of messages in this window are usually used here. When you need to process certain messages before MFC
Add code here. the most distinctive feature of the message control flow of MFC is the virtual function PreTranslateMessage () of the CWnd class. By reloading this function, we can change the message control flow of the MFC, or even make
The new control flow comes out. Only messages passed through the message queue are affected by PreTranslateMessage (). messages sent directly to the window using SendMessage () or other similar methods without passing through the message queue are not
Ignore the existence of PreTranslateMessage.
1. Whether to call TranslateMessage () and DispatchMessage () is determined by the return value of a function named PreTranslateMessage (). If this function returns TRUE, the message is not divided
Send to window function processing.
2. The messages sent to PreTranslateMessage () are untranslated messages that are not processed by TranslateMessage. You can use (pMsg-> wParam = VK_RETURN) in this function to block
The Return key.