孙鑫VC++深入详解:Lesson8 Part3---如何让对话框捕获WM_KEYDOWN消息?


因为对话框程序中,某些特点的消息,如按键消息被Windows内部的对话框过程处理了,即基类完成了或者被发送给子控件处理,所以在对话框类中就不能捕获到按键消息


解决办法:在子类中覆盖基类的特定消息过滤响应函数.

在MFC中,用虚函数ProcessMessageFilter()来过滤或者响应菜单和对话框的特定Windows消息.

在子类中覆盖它,接管消息响应.

本例中,在CWinSunApp类中增加虚函数 ProcessMessageFilter

//覆盖基类的虚函数,接管消息
BOOL CWinSunApp::ProcessMessageFilter(int code, LPMSG lpMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
    
	if(m_hwndDlg!=NULL)
	{
		if(lpMsg->message==WM_KEYDOWN)
			AfxMessageBox("捕获WM_KEYDOWN成功!");
	}
	
	return CWinApp::ProcessMessageFilter(code, lpMsg);
}

WinSunApp.h

// WinSun.h : main header file for the WINSUN application
//

#if !defined(AFX_WINSUN_H__025D80ED_531B_42C3_8C9F_1EB3093C7BC9__INCLUDED_)
#define AFX_WINSUN_H__025D80ED_531B_42C3_8C9F_1EB3093C7BC9__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef __AFXWIN_H__
	#error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h"		// main symbols

/
// CWinSunApp:
// See WinSun.cpp for the implementation of this class
//

class CWinSunApp : public CWinApp
{
public:
	HWND m_hwndDlg;
	CWinSunApp();

// Overrides
	// ClassWizard generated virtual function overrides
	//{
  {AFX_VIRTUAL(CWinSunApp)
	public:
	virtual BOOL InitInstance();
	virtual BOOL ProcessMessageFilter(int code, LPMSG lpMsg);
	//}}AFX_VIRTUAL

// Implementation

	//{
  {AFX_MSG(CWinSunApp)
		// NOTE - the ClassWizard will add and remove member functions here.
		//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG
	DECLARE_MESSAGE
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值