mfc--12. Response handling for keyboard messages in a dialog box && how to respond to WM_CHAR messages

Source: Internet
Author: User
Tags message queue
This article is reproduced article, reprint address: http://blog.sina.com.cn/s/blog_a9fa057b0101gz1p.html
Today in the writing of a small program, found in the dialog box corresponding to the class to add the keyboard character message, wrote the message response function, but did not respond, found this article solved the problem.
Creates a dialog-based program that results in messages that do not respond directly to keyboard keys. Originally, in MFC, the dialog box program after the completion of the program initialization, in the main thread of the program, call the Cwinthread::run function. In this function, the API function PeekMessage is called first, and the function PeekMessage checks the thread message queue, and if the message exists, the message is placed in the specified MSG structure, and subsequent message processing will target the MSG structure object. After the message is captured, the function pre-processes the captured message and then passes the message to the appropriate window-handler function.

Keyboard messages are intercepted without a normal response, and the key is to preprocess the message by the run function. In the run function, the function CWinThread is called::P umpmessage, using this function, MFC implements a diversion of the message, allowing the message to flow along the lines specified by MFC for various messages until it is properly responded to.

function PumpMessage calls the function CWinThread::P retranslatemessage to process the message, if the function does not process the message, Call the API function TranslateMessage function to convert the virtual key message to a character message and call DispatchMessage to distribute the message to the window handler. In the dialog box, the program uses the CWinThread::P retranslatemessage function to process the keyboard message, so whether the dialog box program responds to the keyboard message will be entirely determined by the CWinThread::P retranslatemessage function.

member functions in CWnd and its derived classes the PreTranslateMessage function is a virtual function that can be overloaded to change its processing. By default, this function is not overloaded.

As an example, find the appropriate dialog class in VC6 's Class View right-click and select Add Virtual fuction in the context menu ... Item, and then find the PreTranslateMessage virtual function to load.

BOOL Ckeyintstdlg::P retranslatemessage (msg* pMsg)    //ckeyintstdlg I created for myself dialog box class
{
 //Todo:add your Specialized code here and/or call the base class
	if (Pmsg->message = = Wm_keydown) 
	{ 
		MessageBox (L "has key pressed");
	}
 	 Return CDialog::P retranslatemessage (PMSG);
}

How to respond to WM_CHAR messages create an MFC Dialog Porject, in order to intercept the value of the keyboard keystroke, you need to use the WM_CHAR message. However, when you add the message in project, you find that the program cannot respond to the message. The program does not execute to the function that corresponds to the message after the keystroke. Refer to MSDN's description of the message: This member function was called by the framework of the your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message is received. If the Base-class implementation of this function, that implementation would use the parameters originally passed   With the message and isn't the parameters you supply to the function. The key point is that to execute the WM_CHAR message, the program focus must be on the main window. Unfortunately, after the program runs, the focus is on the button.

BOOL Ckeyintstdlg::P retranslatemessage (msg* pMsg)    //ckeyintstdlg I created for myself dialog box class
{
 //Todo:add your Specialized code here and/or call the base class
method is to use the PreTranslateMessage message, which is processed to set the focus to the main window. The specific code is as follows:
if (pmsg->message = = Wm_char)  
 {  
  pmsg->hwnd = m_hwnd; 
  return FALSE;
 }  
 Return CDialog::P retranslatemessage (PMSG);
}
Then add the corresponding message in OnChar (NChar, nrepcnt, nflags)
void Ctest4dlg::onchar (UINT NChar, uint nrepcnt, uint nflags)//nchar is the ASC code value of the corresponding key
{
      if (nchar==0xd)
{
Dd= "";
Cal SetWindowText (dd);
}
CString SS;
Ss. Format (L "%c", NChar);
DD=DD+SS;
Cal SetWindowText (DD);
Cdialog::onchar (NChar, nrepcnt, nflags);
}


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.