如何让Dialog响应WM_CHAR消息

转自:http://blog.sina.com.cn/s/blog_4ce44c3e01008zbu.html

 

 

创建一个MFC Dialog 的Porject 。为了截获键盘击键的值,需要用到WM_CHAR消息。但在Project中添加该消息后会发现,程序无法响应该消息。即击键后程序并没有执行到该消息对应的函数处。参考MSDN对该消息的描述:
This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.
 
关键的意思是要执行WM_CHAR消息,程序焦点必须在主窗口上。但不幸的是,程序运行以后,焦点在按钮上。解决的方式是使用PreTranslateMessage消息,进行处理,将焦点设置到主窗口上。具体代码如下:

BOOL CTTDlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if ( pMsg->message == WM_KEYDOWN ||pMsg->message == WM_CHAR) { pMsg->hwnd = m_hWnd; return FALSE; } return CDialog::PreTranslateMessage(pMsg); }  

此后即可响应WM_CHAR消息了。

 

你可能感兴趣的:(windows,function,application,mfc,dialog,Parameters)