标签:源文件 call div special 文件 nap init hand void
在对话框程序中,我们经常利用对话框上的子控件进行命令响应来处理一些事件,如果我们想要让对话框类本身(子控件的父窗口)来响应我们的按键消息,须利用ProcessMessageFilter虚函数来捕获对话框的WM_KEYDOWN消息;
virtual BOOL ProcessMessageFilter( int code, LPMSG lpMsg );
1、新建MFC工程:WinSun
2、在CWinSunApp类上右击添加成员变量:HWND m_hwndDlg

3、在CWinSunApp类的源文件中的InitInstance()函数的“return FALSE;”之前添加变量m_hwndDlg的初始化:
m_hwndDlg=NULL;
4、在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->hwnd==m_hwndDlg) || ::IsChild(m_hwndDlg,lpMsg->hwnd)){
//如果消息是WM_KEYDOWN,则弹出消息框
if(lpMsg->message==WM_KEYDOWN){
AfxMessageBox("捕获WM_KEYDOWN消息成功");
}
}
}
return CWinApp::ProcessMessageFilter(code, lpMsg);
}
5、在CWinSunDlg类的OnInitialDialog()函数中的“return TRUE;”之前添加代码:
// TODO: Add extra initialization here //将对话框的句柄传递到CWinSunApp类中 ((CWinSunApp*)AfxGetApp())->m_hwndDlg=m_hWnd;
6、在对话框窗口销毁后,须将CWinSunApp类中的变量m_hwndDlg置为NULL,为此需在CWinSunDlg类上右击添加WM_DESTROY的消息响应函数:
void CWinSunDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
((CWinSunApp*)AfxGetApp())->m_hwndDlg=NULL;
}
7、至此,对话框就可以自己捕获WM_KEYDOWN消息了,要捕获WM_KEYUP和WM_CHAR消息,方法类似;
标签:源文件 call div special 文件 nap init hand void
原文地址:http://www.cnblogs.com/zhouwanqiu/p/6918834.html