因为对话框程序中,某些特点的消息,如按键消息被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