The MFC dialog box responds to OnChar ()

Source: Internet
Author: User

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.

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.