Creation of MFC program + realization of a simple message map

1. Front

Native environment: Win11, VS2022

First of all, make sure that the MFC extension package has been installed in VS. If it is not installed, you can search and install it in Tools -> Get Tools and Features
insert image description here

Two, MFC program creation

The following is the MFC program creation process

  1. Search for MFC, select the MFC application, and click Next
    insert image description here
  2. Fill in the project name, select the project location, create
    insert image description here
  3. Select a single document, select MFC standard for the project style, and click Finish to create it successfully
    insert image description here
  4. Among the generated classes, it is found that a total of five types will be generated: the App class is equivalent to the entry of the MFC program; the Doc class is used to manage data; the Frame class is a frame class, which can be understood as a window; the View class is a visual class. Just click Finish to create successfully.
    insert image description here

It can be run directly to generate the following window
running result

3. A simple message map

Functional description

Click anywhere in the window, and a new window will pop up, displaying the coordinates of the clicked place

function realization

Analysis function, essentially we want the window to respond to click events and generate a window with custom text. But the frame is the frame class, and the view is the view class, which is the part that actually performs the display. All the things related to the display are written in the View class.

  1. Double-click C View in the class view to jump to the corresponding .h file, and find that the declaration macro has been written in the C View class DECLARE_MESSAGE_MAP(), which means that messages can be mapped to objects belonging to this class
    insert image description here

  2. Then double-click any function under this class to jump to the .cpp file, and find that the boundary macro already exists, and the events/messages we hope to be responded to can be defined between the boundary macros
    insert image description here

  3. Right-click C***View, click Properties, and select Message
    insert image description here

  4. Find OnLButtonDown, click add, the function can be automatically generated, now you only need to fill in the function content
    insert image description here

  5. Implement it in the OnLButtonDown function, the code is as follows

// 注意把类名换成自己的
void CSmallDrawingSystemView::OnLButtonDown(UINT nFlags, CPoint point)
{
    
    
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CView::OnLButtonDown(nFlags, point);

	CString str;
	str.Format(TEXT("x=%d, y=%d"), point.x, point.y);

	MessageBox(str);
}
  1. Click to run, the effect is as follows
    insert image description here

Guess you like

Origin blog.csdn.net/weixin_50497501/article/details/128259716