MFC-消息映射机制

MFC-消息映射机制

#include <afxwin.h>

class MyFrameWnd :public CFrameWnd {
public:
	virtual LRESULT WindowProc(UINT msgID,WPARAM wParam,LPARAM IParam);
};

LRESULT MyFrameWnd::WindowProc(UINT msgID, WPARAM wParam, LPARAM IParam) {
	switch (msgID)
	{
     // 窗口被创建时触发
	 case WM_CREATE:
		AfxMessageBox("Hi Major");
		break;
	}
	return CFrameWnd::WindowProc(msgID,wParam,IParam);
}

class MyWinApp :public CWinApp {
public:
	MyWinApp() {

	}
	virtual BOOL InitInstance() {

	MyFrameWnd* pFrame	= new MyFrameWnd;
	pFrame->Create(NULL,"Test");
	m_pMainWnd = pFrame;
	pFrame->ShowWindow(SW_SHOW);
	pFrame->UpdateWindow();
	return TRUE;
	}
};

MyWinApp mainApp;

在这里插入图片描述

#include <afxwin.h>

class MyFrameWnd :public CFrameWnd {
	// 类内声明宏
	DECLARE_MESSAGE_MAP()
public:
	LRESULT OnCreate(WPARAM wParam,LPARAM IParam);
};

// 类外实现宏
BEGIN_MESSAGE_MAP(MyFrameWnd,CFrameWnd)
	ON_MESSAGE(WM_CREATE,OnCreate)
END_MESSAGE_MAP()

LRESULT MyFrameWnd::OnCreate(WPARAM wParam, LPARAM IParam) {
	AfxMessageBox("Hi Major!");
	return 0;
}

class MyWinApp :public CWinApp {
public:
	MyWinApp() {

	}
	virtual BOOL InitInstance() {

	MyFrameWnd* pFrame	= new MyFrameWnd;
	pFrame->Create(NULL,"Test");
	m_pMainWnd = pFrame;
	pFrame->ShowWindow(SW_SHOW);
	pFrame->UpdateWindow();
	return TRUE;
	}
};

MyWinApp mainApp;

在这里插入图片描述

#include <afxwin.h>

class MyFrameWnd :public CFrameWnd {
	// 类内声明宏
	DECLARE_MESSAGE_MAP()
public:
	int OnCreate(LPCREATESTRUCT pcs);
};

// 类外实现宏
BEGIN_MESSAGE_MAP(MyFrameWnd,CFrameWnd)
	ON_WM_CREATE()
END_MESSAGE_MAP()

int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
	AfxMessageBox("Hi Major!");
	return CFrameWnd::OnCreate(pcs);
}

class MyWinApp :public CWinApp {
public:
	MyWinApp() {

	}
	virtual BOOL InitInstance() {

	MyFrameWnd* pFrame	= new MyFrameWnd;
	pFrame->Create(NULL,"Test");
	m_pMainWnd = pFrame;
	pFrame->ShowWindow(SW_SHOW);
	pFrame->UpdateWindow();
	return TRUE;
	}
};

MyWinApp mainApp;

在这里插入图片描述

#include <afxwin.h>

class MyFrameWnd :public CFrameWnd {
	// 类内声明宏
	DECLARE_MESSAGE_MAP()
public:
	int OnCreate(LPCREATESTRUCT pcs);
	void OnPaint();
};

// 类外实现宏
BEGIN_MESSAGE_MAP(MyFrameWnd,CFrameWnd)
	ON_WM_CREATE()
	ON_WM_PAINT()
END_MESSAGE_MAP()

int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
	AfxMessageBox("Hi Major!");
	return CFrameWnd::OnCreate(pcs);
}

void MyFrameWnd::OnPaint() {
	PAINTSTRUCT ps = { 0 };
	HDC hdc = ::BeginPaint(this->m_hWnd,&ps);
	::TextOutA(hdc,100,100,"Hi Major!",9);
	::EndPaint(m_hWnd,&ps);
}



class MyWinApp :public CWinApp {
public:
	MyWinApp() {

	}
	virtual BOOL InitInstance() {

	MyFrameWnd* pFrame	= new MyFrameWnd;
	pFrame->Create(NULL,"Test");
	m_pMainWnd = pFrame;
	pFrame->ShowWindow(SW_SHOW);
	pFrame->UpdateWindow();
	return TRUE;
	}
};

MyWinApp mainApp;

在这里插入图片描述

#include <afxwin.h>

class MyFrameWnd :public CFrameWnd {
	// 类内声明宏
	DECLARE_MESSAGE_MAP()
public:
	int OnCreate(LPCREATESTRUCT pcs);
	void OnPaint();
	void OnMouseMove(UINT nKey,CPoint pt);
	int m_x;
	int m_y;
};

// 类外实现宏
BEGIN_MESSAGE_MAP(MyFrameWnd,CFrameWnd)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
	AfxMessageBox("Hi Major!");
	return CFrameWnd::OnCreate(pcs);
}

void MyFrameWnd::OnPaint() {
	PAINTSTRUCT ps = { 0 };
	HDC hdc = ::BeginPaint(this->m_hWnd,&ps);
	::TextOutA(hdc, m_x, m_y,"Hi Major!",9);
	::EndPaint(m_hWnd,&ps);
}

void MyFrameWnd::OnMouseMove(UINT nKey, CPoint pt) {
	m_x = pt.x;
	m_y = pt.y;
	::InvalidateRect(this->m_hWnd,NULL,TRUE);
}

class MyWinApp :public CWinApp {
public:
	MyWinApp() {

	}
	virtual BOOL InitInstance() {

	MyFrameWnd* pFrame	= new MyFrameWnd;
	pFrame->Create(NULL,"Test");
	m_pMainWnd = pFrame;
	pFrame->ShowWindow(SW_SHOW);
	pFrame->UpdateWindow();
	return TRUE;
	}
};

MyWinApp mainApp;

在这里插入图片描述

#include <afxwin.h>

// 自定义消息
#define WM_MYMESSAGE WM_USER+001

class MyFrameWnd :public CFrameWnd {
	// 类内声明宏
	DECLARE_MESSAGE_MAP()
public:
	int OnCreate(LPCREATESTRUCT pcs);
	void OnPaint();
	void OnMouseMove(UINT nKey,CPoint pt);
	LRESULT OnMyMessage(WPARAM wParam,LPARAM IParam);
	int m_x;
	int m_y;
};

// 类外实现宏
BEGIN_MESSAGE_MAP(MyFrameWnd,CFrameWnd)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_MOUSEMOVE()
	ON_MESSAGE(WM_MYMESSAGE,OnMyMessage)
END_MESSAGE_MAP()

int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
	AfxMessageBox("Hi Major!");
	// 发送消息
	::PostMessage(this->m_hWnd, WM_MYMESSAGE,1,2);
	return CFrameWnd::OnCreate(pcs);
}

// 对应函数
LRESULT MyFrameWnd::OnMyMessage(WPARAM wParam, LPARAM IParam) {
	CString str;
	str.Format("wParam=%d,IParam=%d",wParam,IParam);
	AfxMessageBox(str);
	return 0;
}

void MyFrameWnd::OnPaint() {
	PAINTSTRUCT ps = { 0 };
	HDC hdc = ::BeginPaint(this->m_hWnd,&ps);
	::TextOutA(hdc, m_x, m_y,"Hi Major!",9);
	::EndPaint(m_hWnd,&ps);
}

void MyFrameWnd::OnMouseMove(UINT nKey, CPoint pt) {
	m_x = pt.x;
	m_y = pt.y;
	::InvalidateRect(this->m_hWnd,NULL,TRUE);
}

class MyWinApp :public CWinApp {
public:
	MyWinApp() {

	}
	virtual BOOL InitInstance() {

	MyFrameWnd* pFrame	= new MyFrameWnd;
	pFrame->Create(NULL,"Test");
	m_pMainWnd = pFrame;
	pFrame->ShowWindow(SW_SHOW);
	pFrame->UpdateWindow();
	return TRUE;
	}
};

MyWinApp mainApp;

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值