消息映射机制

2020-04-21  本文已影响0人  温柔倾怀

MFC.h

#pragma once
#include <afxwin.h> //mfc头文件

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 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->UpdateWindow();
    //m_pMainWnd 保存指向应用程序的主窗口的指针 
    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("mfc"));
}
afx_msg void MyFrame::OnLButtonDown(UINT, CPoint point)
{
    CString str;
    str.Format(TEXT("x=%d  y=%d"), point.x, point.y);
    MessageBox(str);
}
afx_msg void MyFrame::OnChar(UINT key, UINT, UINT)
{
    CString str;
    str.Format(TEXT("按下了 %c 键"), key);
    MessageBox(str);
}

afx_msg void MyFrame::OnPaint()
{
    CPaintDC dc(this);
    dc.TextOutW(100, 100, TEXT("为了部落"));
    //画椭圆
    dc.Ellipse(10, 10, 100, 100);
}

上一篇 下一篇

猜你喜欢

热点阅读