MFC中的消息映射机制
声明宏 写道 .h 中
分界宏 写道 .cpp 中 BEGIN_MESSAGE_MAP(子类,父类) …END_MESSAGE_MAP()
找消息宏 写到分界宏中间
把函数原型声明写到 .h 中,函数实现写到 .cpp
鼠标,键盘的实现
代码注释如下
mfc.h
#include <afxwin.h>
#include <intsafe.h>
class MyApp:public CWinApp
{
public:
virtual BOOL
InitInstance();
};
class MyFrame :public CFrameWnd
{
public:
MyFrame();
DECLARE_MESSAGE_MAP()
afx_msg
void OnLButtonDown(UINT
, CPoint
);
afx_msg
void OnLButtonUp(UINT
, CPoint
);
afx_msg
void OnChar(UINT
, UINT
, UINT
);
afx_msg
void OnPaint();
};
mfc.cpp
#include "mfc.h"
MyApp app
;
BOOL MyApp
::InitInstance()
{
MyFrame
* frame
= new MyFrame
;
frame
->ShowWindow(SW_SHOWNORMAL
);
frame
->UpdateData();
m_pMainWnd
= frame
;
return TRUE
;
}
BEGIN_MESSAGE_MAP(MyFrame
,CFrameWnd
)
ON_WM_LBUTTONDOWN()
ON_WM_CHAR()
ON_WM_PAINT()
END_MESSAGE_MAP()
MyFrame
::MyFrame()
{
Create(NULL, TEXT("湖南科技大学"));
}
void MyFrame
::OnLButtonDown(UINT
, CPoint point
)
{
TCHAR buf
[1024];
wsprintf(buf
, TEXT("x = %d,y = %d"),point
.x
,point
.y
);
MessageBox(buf
);
}
void MyFrame
::OnLButtonUp(UINT
, CPoint
)
{
}
void MyFrame
::OnChar(UINT key
,UINT
,UINT
)
{
CString str
;
str
.Format(TEXT("按下了 %c 键"),key
);
MessageBox(str
);
}
void MyFrame
::OnPaint()
{
CPaintDC
dc(this);
dc
.Ellipse(10, 10, 100, 100);
}
mfc创建窗口完结!!
转载请注明原文地址:https://blackberry.8miu.com/read-42463.html