📄 08.4 附录.txt
字号:
附录
本篇文章是笔者在教学过程中解决学员的一个问题后撰写的,在此奉上此文,以飨读者。
如何在对话框程序中让对话框捕获WM KEYDOWN消息
作者:孙鑫日期: 2003-9-4
在对话框程序中,我们经常利用对话框上的子控件进行命令响应来处理一些事件。如果我们想要让对话框(子控件的父窗口)类来响应我们的按键消息,则可以通过 Class Wizard对WM_KEYDOWN消息进行响应,当程序运行后,我们按下键盘上的按键,但对话框不会有任何的反应。这是因为在对话框程序中,某些特定的消息,例女口按键消息,它们被Windows内部的对话框过程处理了(即在基类中完成了处理,有兴趣的读者可以查看MFC的源代码),或者被发送给子拉件进行处理,所以我们在对话框类中就捕获不到按 4建的消息了。
既然我们知道了这个处理的过程,就可以找到底层处理按键消息的函数,然后在子类
中重载它,便能在对话框程序中处理按键消息了。在岛1FC中,是利用BOOLProcessMessage
308 I胁"
Filter(int code, LPMSG lpMsg)这个虚函数来过滤或响应菜单和对话框的特定 Windows消息。下面我们通过程序给大家演示基于对话框的应用程序对 WM_KEYDOWN消息的捕获。
咽
(]J新建一个工程,选择 MFC AppWizard (ex时,工程名为 WinSun,点去 ok,进入下一步,选择Dia10g based,单击【 Finish】按钮。 .在 CWinSunApp类上点击右键,选择 Add Member Vari a1be,增加一个类型为 HWND,变量名 m_hwndDlg的 public的变量。代码如下:
. WinSun.h class CWinSunApp : public CWinApp { public:
HWND m_hwndDlg;
CWinSunApp();
11 Overrides 11 ClassWizard generated virtual function overrides I/{ {AFX_V工 RTUAL(CWinSunApp)
public:
virtual BOOL InitInstance();
II}}AFX_VIRTUAL
11工 mplementation
II{{AFX_MSG(CW工 nSu nApp)
11 NOTE -the ClassWizard will add and remove member functions here. 11 DO NOT EDIT what you see in these blocks of generated code ! II}}AFX_MSG
DECLARE_MESSAGE_MAP()
CIJ在 WinSun.cpp (CWinSunApp类)文件中的 InitlnstanceO函数中添加如下代码 O
WinSun.cpp BOOL CWinSunApp::工 nit工 nstance () {
AfxEnableControlContainer();
11 Standard initialization
11 If you are not using these features and wish to reduce the size
11 of your f工 nal executable, you should remove from the following
11 the specific initialization routines you do not need.
#ifdef AFXDLL
Enable3dControls () ; 11 Call this when using MFC in a shared DLL #else
Enable3dControlsStatic () ; 11 Call th工 s when linking to MFC statically
@建~... 1309
#endif
CWinSunDlg dlg;
ID-pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse ==工 DOK)
{
11 TODO: Place code here to handle when the dialog工 S 11 dismissed with OK
else if (nResponse工 DCANCEL)
{ 11 TODO: Place code here to handle when the dialog is 11 dismissed with Cance工
11 since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
ID_hwndDlg=NULL;
return FALSE;
团在 CWinSunApp类上羊击右键,选择【 Add Virtua1 Function】,在左边一栏里,选择【ProcessMessageFilter],在右边按钮上选择[ Add and Edit 】,然后加入以下代码:
W工 nSun.cpp
BOOL CWinSunApp::processMessageFilter(int code , LPMSG lpMsg)
11 TODO: Add your specìalìzed code here and/or call the base class if(ID_hwndDlg!=NULL)
1/判断消息,如果消息是从对话框发出的或者其子控件发出的,我们就进行处理。 suruin
if((lpMsg->hwnd==m_hwndDlg) II ::工 sChild(m_hwndDlg, lpMsg->hwnd))
1/如果消息是阳_KEYDOWN.我们就弹出一个消息框。 sunxln ìf(lpMsg->message==WM_KEYDOWN)
AfxMessageBox("捕获 WM_KEYDOWN消息成功!");
}
return CWinApp::ProcessMessageFilter(code, lpMsg);
(3J在 WinSunDlg.cpp ( CWinSunDlg类)中的 OnInitia1Dia1og0函数中加入以下代码:
WìnSunDlg.cpp BOOL CWinSunDlg::OnInitDialog() {
310 I ~~...
CDialog: :0口工nitDialog();
// Add "About..." menu item to system menu.
//工DM_ABOUTBOX must be in the system command range.
ASSERT ( (工DM_ABOUTBOX & OxFFFO)工DM_ABOUTBOX) ;
ASSERT(工DM_ABOUTBOX < OxFOOO);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(工DS_ABOUTBOX) ;
if (!strAboutMenu.工sEmpty () )
{
pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING,工DM_ABOUTBOX, strAboutMenu);
/ / Set the icon for this dialog. The framework does this automatically / / when the application' s main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//将对话框的句柄传递到CwinSunApp类中。 sunxin
((CWinSunApp*)AfxGetApp() )->m_hwndDlg=m_hWnd; return TRUE; // return TRUE unless you set the focus to a control
圈在对话框窗口销毁后,将CWinSunApp类中的变量ffi_hwndDlg直为 NULL,为此我们在CWinSunDlg类上羊击右键,选择AddWindows Message Handler,在左边一栏中选择WM_DESTROY,在右边按钮上选择Add and Edit,然后加入以下代码:
WinSunDlg.cpp void CWinSunDlg::OnDestroy() {
CDialog::OnDestroy() ;
// TODO: Add your message handler code. here
((CWinSunApp*)AfxGetApp() )->m_hwndDlg=NULL;
至此,我们的工作就做完了,现在我们可以接Ctrl+自快捷键运行程序,看到我们想要的结果。当然,如果我们想捕获WM_KEYUP或WM CHAR消息,也是类似的,这就交给读者下来自行完成了。
.
乡l
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -