在MFC中添加一个消息处理函数的方法有(比如在CxxxView类中):
1.在CxxxView类上点右键,选择add windows message handler, 选择消息类型,点Add Handler,然后点击Edit Exiting 。
2.在菜单栏,查看,建立类向导,选择项目名,类名,消息类型,点“Add Function”,"Edit code"。
这两种方法会自动生成部分代码:
1.双击CxxxView 会发现在xxxView.h文件中
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CMyTestSingleView)
// 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_MAP()
变为了:
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CDrawView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
2.双击CxxxView()函数,在xxxView.cpp文件中
BEGIN_MESSAGE_MAP(CMyTestSingleView, CView)
//{{AFX_MSG_MAP(CMyTestSingleView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
变为:
BEGIN_MESSAGE_MAP(CDrawView, CView)
//{{AFX_MSG_MAP(CDrawView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
3.消息函数的实现部分,同样在xxxView.cpp中
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnLButtonDown(nFlags, point);
}