代码如下:
WinSun.h
class CWinSunApp : public CWinApp
{
public:
HWND m_hwndDlg;
CWinSunApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWinSunApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CWinSunApp)
// NOTE - the ClassWizard will add and remove member functions here.
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
第三步:在WinSun.cpp(CWinSunApp类)文件中的InitInstance()函数中添加如下代码:
代码如下:
WinSun.cpp
BOOL CWinSunApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CWinSunDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
m_hwndDlg=NULL;
return FALSE;
}
第四步:在CWinSunApp类上点击右键,选择Add Virtual Function,在左边一栏里,选择ProcessMessageFilter,在右边按钮上选择Add and Edit,然后加入以下代码:
代码如下:
WinSun.cpp
BOOL CWinSunApp::ProcessMessageFilter(int code, LPMSG lpMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(m_hwndDlg!=NULL)
{
//判断消息,如果消息是从对话框发出的或者其子控件发出的,我们就进行处理。sunxin
if((lpMsg-hwnd==m_hwndDlg) || ::IsChild(m_hwndDlg,lpMsg-hwnd))
{
//如果消息是WM_KEYDOWN,我们就弹出一个消息框。sunxin
if(lpMsg-message==WM_KEYDOWN)
{
AfxMessageBox("捕获WM_KEYDOWN消息成功!");
}
}
}
return CWinApp::ProcessMessageFilter(code, lpMsg);
}
第五步:在WinSunDlg.cpp(CWinSunDlg类)中的OnInitialDialog()函数中加入以下代码:
代码如下:
WinSunDlg.cpp
BOOL CWinSunDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu-AppendMenu(MF_SEPARATOR);
pSysMenu-AppendMenu(MF_STRING, IDM_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类中的变量m_hwndDlg置为NULL,为此我们在CWinSunDlg类上点击右键,选择Add Windows 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+F5运行程序,看到我们想要的结果。