当前位置:网站首页>Vs2019 first MFC Application
Vs2019 first MFC Application
2022-07-06 11:24:00 【imxlw00】
Create project
The creation of the project is the same as before , Only the suffix of the source file this time is .cpp, because MFC By C++ Compiling , To write MFC The program needs to contain **#include <afxwin.h>** The header file .
Write code

mfc.h
#pragma once
#include <afxwin.h>
class MyApp :public CWinApp//CWinApp Application class
{
public:
// Program entrance
virtual BOOL InitInstance();
};
class MyFrame :public CFrameWnd
{
public :
MyFrame();
};
mfc.cpp
#include "mfc.h"
MyApp app;// Global application objects , There is and only one
BOOL MyApp::InitInstance()
{
// create a window
MyFrame* frame = new MyFrame();
// Display and update
frame->ShowWindow(SW_NORMAL);
frame->UpdateWindow();
// Save a pointer to the main window of the application
m_pMainWnd = frame;
// Return to normal initialization
return TRUE;
}
MyFrame::MyFrame()
{
Create(NULL, TEXT("mfc"));
}
Run the program
Configuration environment
Right-click the project properties 

It shows an empty interface 
Program execution flow
① When the program starts , Instantiate the application object first ( There is one and only one )
② The entry function of the execution program InitInstance()
③ To frame class MyFrame Object dynamically allocates space ( Automatically call its constructor ), Inside its constructor , adopt CWnd::Create create a window
④ Frame class object display window CWnd::ShowWindow
⑤ Frame class object update window CWnd::UpdateWindow
⑥ Save frame class object pointer CWinThread::m_pMainWnd
Message mapping
A message map is a table that associates messages with member functions . such as , The frame window receives a left mouse click message ,MFC The message map for this window will be searched , If there is a process WM_LBUTTONDOWN Message handler , Then call OnLButtonDown.
Here's all you've done to add a message map to a class :
1) In the operated class , Declare the message mapping macro .
2) Perform message mapping by placing a macro that identifies the message , The corresponding class will BEGIN_MESSAGE_MAP and END_MESSAGE_MAP Processing messages between calls to .
3) The corresponding message processing functions are declared in the class , Off class definition :
Add message processing
Let the program move !!!
// Declare macros Provide message mapping mechanism
DECLARE_MESSAGE_MAP()
afx_msg void OnLButtonDown(UINT, CPoint);
afx_msg void OnChar(UINT, UINT, UINT);
afx_msg void OnPaint();
// Boundary macro
BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
ON_WM_LBUTTONDOWN() // Press the left mouse button
ON_WM_CHAR() // keyboard
ON_WM_PAINT() // Drawing macro
END_MESSAGE_MAP()
void MyFrame::OnLButtonDown(UINT, CPoint point)
{
/*TCHAR buf[1024]; wsprintf(buf, TEXT("x = %d, y =%d"), point.x, point.y); MessageBox(buf);*/
//mfc String in CString
CString str;
str.Format(TEXT("x = %d ,,,, y = %d "), point.x, point.y);
MessageBox(str);
}
void MyFrame::OnChar(UINT key, UINT, UINT)
{
CString str;
str.Format(TEXT(" Press %c key "), key);
MessageBox(str);
}
void MyFrame::OnPaint()
{
CPaintDC dc(this); //CDC Look for other graphics that you can draw
dc.TextOutW(100, 100, TEXT(" For the tribe "));
// Drawing ellipse
dc.Ellipse(10, 10, 100, 100);
// Multi byte to Wide bytes
//TEXT Is the conversion by adaptive coding
// TCHER Conversion of adaptive coding
//MessageBox(L"aaa");
// Statistical string length
int num = 0;
char * p = "aaaa";
num = strlen(p);
// Count the string length of wide bytes
wchar_t * p2 = L"bbbb";
num = wcslen(p2);
//char * And CString Conversion between C++ string .c_str();
//char* -> CString
char * p3 = "ccc";
CString str = CString(p3);
//CString -> char *
CStringA tmp;
tmp = str;
char * pp = tmp.GetBuffer();
}

边栏推荐
- What does usart1 mean
- Antlr4 uses keywords as identifiers
- SSM integrated notes easy to understand version
- Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
- 數據庫高級學習筆記--SQL語句
- 软件测试-面试题分享
- Learn winpwn (3) -- sEH from scratch
- Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
- 01 project demand analysis (ordering system)
- L2-004 这是二叉搜索树吗? (25 分)
猜你喜欢

Kept VRRP script, preemptive delay, VIP unicast details

Machine learning notes week02 convolutional neural network

连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
![[free setup] asp Net online course selection system design and Implementation (source code +lunwen)](/img/ac/b518796a92d00615cd374c0c835f38.jpg)
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)

Rhcsa certification exam exercise (configured on the first host)
![[蓝桥杯2017初赛]方格分割](/img/e9/e49556d0867840148a60ff4906f78e.png)
[蓝桥杯2017初赛]方格分割

Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.

Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes

QT creator support platform

Request object and response object analysis
随机推荐
AcWing 179.阶乘分解 题解
Antlr4 uses keywords as identifiers
Kept VRRP script, preemptive delay, VIP unicast details
LeetCode #461 汉明距离
MySQL的一些随笔记录
Copie maître - esclave MySQL, séparation lecture - écriture
基于apache-jena的知识问答
QT creator specifies dependencies
Install MySQL for Ubuntu 20.04
MySQL completely uninstalled (windows, MAC, Linux)
连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
02 staff information management after the actual project
UDS learning notes on fault codes (0x19 and 0x14 services)
Codeforces Round #753 (Div. 3)
引入了junit为什么还是用不了@Test注解
MySQL主从复制、读写分离
人脸识别 face_recognition
Learn winpwn (3) -- sEH from scratch
打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站
Base de données Advanced Learning Notes - - SQL statements