Introduction to MFC and basic usage

1. Windows message mechanism

1.1 Window

There is at least one window in a Windows application, called the main window. We draw patterns in the client area.

1.2 Handle

A Windows application contains many components (resources) such as icons and cursors. The system will return their identification numbers after allocating memory for these components (resources), which is the so-called handle. For example, in a Windows application, a window handle is used to identify each window. When operating on a window, the first step is to get the ID of the window.

1.3 messages and message queues

Windows is an event-driven programming. When a Windows application starts to execute, the system will generate a message queue to save the message of this window. eg: When you press the keyboard in a window (event), the system will collect the keyboard press (event), then package it into a message and put it in the message queue of this window (window procedure). Because of the first-in-first-out feature of the queue, the window will read the messages one by one at this time, and then execute the corresponding processing.

1.4WinMain function

The entry in the console DOS is the mian function, and the WinMain function is the entry function of the Windows program. Start calling WinMain function first when starting an application

1.5 Windows programming model

The function of an application is to create a window and then respond to keyboard or mouse messages.

  • Definition of WinMain function
  • Create window
  • Message loop
  • Window procedure

2. Simple use of MFC

2.1 Introduction to MFC

MFC is Microsoft's basic class library, which encapsulates the Windows API in the form of C++ classes and contains an application framework. The class contains a large number of windows handle package classes and many package classes for windows components and built-in controls. MFC wraps the Windows SDK API functions into hundreds of classes. MFC provides object-oriented interfaces to the Windows system, supporting reusability, self-containment and OPP principles.

2.2 MFC program entry

MFC.h

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

//1、应用程序类CWinApp,MyAPP派生类
class MyApp : public CWinApp{
public:
    //父类的虚函数,需要派生类进行重写
    //也是MFC函数的入口函数
    virtual BOOL InitInstance();
};

//2、框架类CFrameWnd,MyFrame派生类
class MyFrame : public CFrameWnd{
public:
    //构造函数
    MyFrame();
}

MFC.cpp

//对应类的头文件
#include "mfc.h"

//有且只有一个的全局应用程序类的对象
MyApp app;

//程序入口
BOOL MyApp::InitInstance(){
    //1、创建类框架
    MyFrame *frame = new MyFrame;
    //2、显示窗口
    frame -> ShowWindow(SW_SHOWNORMAL);
    //3、更新窗口
    frame -> UpdateWindow();
    //4、保存框架类对象指针
    m_pMainWnd = frame;
    return TRUE;
}

//重写构造函数
MyFrame::MyFrame(){
    //创建窗口
    Create(NULL,TEXT("mfc"));
}

2.3 MFC's message mapping

Information mapping is a table that correlates messages and member functions. For example, Windows puts a left-click message in a frame window message queue, and when the window fetches the message, MFC will search the message map of the window. If there is a processing program that handles the left-click message (WM_LBUTTONDOWN) , Just call.

Here is how to write a message map in a class

  • Declare the message macro in the operating class
  • Perform message mapping by placing a macro that identifies the message, and the corresponding class will process the message between calls to BEGIN_MESSAGE_MAP and END_MESSAGE_MAP

mfc.h

class MyFrame : public CFrameWnd{
public:
    MyFrame();

    //声明消息宏
    DECLARE_MESSAGE_MAP()
}

mfc.cpp

//定义消息宏,在类中实现(派生类,父类)
BEGIN_MESSAGE_MAP(MyFrame,CFrameWnd)
    ON_WM_LBUTTONDOWN
END_MESSAGE_MAP()

//重写构造函数
MyFrame::MyFrame(){
    //创建窗口
    Create(NULL,TEXT("mfc"));
}

The corresponding processing functions are declared in the class, and defined outside the class

mfc.h

//框架类CFrameWnd,MyFrame派生类
class MyFrame : public CFrameWnd{
public:
    //构造函数
    MyFrame();
    //声明消息宏
    DECLARE_MESSAGE_MAP()
    //函数声明
    dfx_msg void OnLButtonDown(UINT,CPoint);
}

mfc.cpp

//定义消息宏,在类中实现(派生类,父类)
BEGIN_MESSAGE_MAP(MyFrame,CFrameWnd)
    ON_WM_LBUTTONDOWN
END_MESSAGE_MAP()

//重写构造函数
MyFrame::MyFrame(){
    //创建窗口
    Create(NULL,TEXT("mfc"));
}

//定义具体函数的实现
void OnLButtonDown(UINT,CPoint){
    .......
    .......
}

2.4 Use MFC template when creating a project

After creation, the following classes will be generated

Insert picture description here

Remember that our processing function is written in the view class. It can be understood that the original frame class is used as a base map, and the current view class is a whiteboard area superimposed on it, and it is drawn and rendered on it.

2.5 Use of class wizard in VS2019

ctrl+shift+x can quickly call up the class wizard. The so-called class wizard is to help you quickly generate a message map. You no longer need to write the start and end macros by yourself like the above... It will automatically generate these for you.
Insert picture description here

3. Introduction to MFC drawing

3.1 Basic introduction to MFC drawing

Windows GDI is a graphics device interface provided by Windows, which can draw graphics on the monitor

MFC provides 2 classes to support calling GDI:

  • DC class: used to set drawing attributes and draw graphics
  • Drawing object class: encapsulates various GDI drawing objects, including pens, brushes, fonts...

In MFC, the image device interface is abstracted as the CDC class. The CDC class encapsulates all GDI functions, so the CDC class is used for drawing, and the window operation is in the CWnd class.

There are 4 more sub-categories in the CDC category

  • CClientDC operates the user area, which is used when drawing with the mouse and drawing text
  • CMetaFileDC device environment class, execute BeginPaint during construction, and execute EndPaint during destruction
  • CPaintDC
  • CWindowsDC

3.2 CDC category

GetDC, releaseDC are member functions of the CWnd class
CreateDC, DeleteDC are member functions of the CDC class

The following are three methods of obtaining device environment objects,

void CDrawTestView::Draw(CDC *pDC)
{
  //使用传入CDC的对象指针绘图指针
  pDC -> LineTo(200,100);

  //构造CClientDC设备环境对象指针
  CClientDC dc(this);
  dc.LineTo(200,200)

  //通过GetDC函数获得设备环境对象指针
  CDC* p = this ->GetDC();
  p->LineTo(100,200);
  //利用GetDC函数获得的设备环境对象必须使用ReleaseDC函数释放
  this->ReleaseDC(p);
}

Guess you like

Origin blog.csdn.net/qq_45549336/article/details/109008192