浅析Windows消息循环

一、示例代码

msg.h
#include <afxwin.h>
class MyFrame : public CFrameWnd
{
public:
    MyFrame();
    DECLARE_MESSAGE_MAP()
    afx_msg void OnLButtonDown(UINT, CPoint);
};

class MyApp : public CWinApp
{
public:
    MyApp();
    BOOL InitInstance();
};
msg.cpp
#include "msg.h"
MyApp myapp;
BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
    ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

MyFrame::MyFrame()
{
    this->Create(NULL, _T("I AM A WINDOW"));
}

void MyFrame::OnLButtonDown(UINT, CPoint)
{
    MessageBox(_T("hello, mfc"));
}

BOOL MyApp::InitInstance()
{
    MyFrame* frame = new MyFrame;
    frame->ShowWindow(SW_SHOWNORMAL);
    frame->UpdateWindow();
    m_pMainWnd = frame;
    return TRUE;
}

二、代码分析

这个小程序是为了说明Windows下的简单消息循环而写的代码,代码中InitInstance是MFC应用程序的入口函数;MyFrame 是窗口框架类,MyApp是应用程序类。一般的消息循环需要经过三个步骤:

1.声明消息映射
DECLARE_MESSAGE_MAP()
2.要处理的消息

BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

3.消息函数的声明与实现
//头文件中
afx_msg void OnLButtonDown(UINT, CPoint); 

//.cpp文件中
void MyFrame::OnLButtonDown(UINT, CPoint)
{
    MessageBox(_T("hello, mfc"));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

The_Web3_社区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值