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
Two, MFC program creation
The following is the MFC program creation process
- Search for MFC, select the MFC application, and click Next
- Fill in the project name, select the project location, create
- Select a single document, select MFC standard for the project style, and click Finish to create it successfully
- 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.
It can be run directly to generate the following window
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.
-
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
-
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
-
Right-click C***View, click Properties, and select Message
-
Find OnLButtonDown, click add, the function can be automatically generated, now you only need to fill in the function content
-
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);
}
- Click to run, the effect is as follows