Dialog响应WM_CHAR消息

创建一个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消息,进行处理,将焦点设置到主窗口上。具体代码如下:

[cpp]  view plain  copy
  1. BOOL CTTDlg::PreTranslateMessage(MSG* pMsg)  
  2. {  
  3.  // TODO: Add your specialized code here and/or call the base class  
  4.  if (pMsg->message == WM_CHAR)  
  5.  {  
  6.    pMsg->hwnd = m_hWnd;  
  7.       return FALSE;  
  8.   }  
  9.  return CDialog::PreTranslateMessage(pMsg);  
  10. }  
  11.    
 

此后即可响应WM_CHAR消息了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值