当前位置:网站首页>vs2019 第一个MFC应用程序
vs2019 第一个MFC应用程序
2022-07-06 09:14:00 【imxlw00】
创建项目
项目的创建和之前一样,只是此次的源文件后缀为.cpp,因为MFC是由C++编写的,编写MFC程序需要包含**#include <afxwin.h>**头文件。
编写代码

mfc.h
#pragma once
#include <afxwin.h>
class MyApp :public CWinApp//CWinApp应用程序类
{
public:
//程序入口
virtual BOOL InitInstance();
};
class MyFrame :public CFrameWnd
{
public :
MyFrame();
};
mfc.cpp
#include "mfc.h"
MyApp app;//全局应用程序对象,有且仅有一个
BOOL MyApp::InitInstance()
{
//创建窗口
MyFrame* frame = new MyFrame();
//显示和更新
frame->ShowWindow(SW_NORMAL);
frame->UpdateWindow();
//保存指向应用程序的主窗口的指针
m_pMainWnd = frame;
//返回正常初始化
return TRUE;
}
MyFrame::MyFrame()
{
Create(NULL, TEXT("mfc"));
}
运行程序
配置环境
右击项目属性

显示了一个空空的界面
程序执行流程
① 程序开始时,先实例化应用程序对象(有且只有一个)
② 执行程序的入口函数InitInstance()
③ 给框架类MyFrame对象动态分配空间(自动调用它的构造函数),在其构造函数内部,通过CWnd::Create创建窗口
④ 框架类对象显示窗口CWnd::ShowWindow
⑤ 框架类对象更新窗口CWnd::UpdateWindow
⑥ 保存框架类对象指针CWinThread::m_pMainWnd
消息映射
消息映射是一个将消息和成员函数相互关联的表。比如,框架窗口接收到一个鼠标左击消息,MFC将搜索该窗口的消息映射,如果存在一个处理WM_LBUTTONDOWN消息的处理程序,然后就调用OnLButtonDown。
下面是是将消息映射添加到一个类中所做的全部工作:
1)所操作类中,声明消息映射宏。
2)通过放置标识消息的宏来执行消息映射,相应的类将在对BEGIN_MESSAGE_MAP和END_MESSAGE_MAP的调用之间处理消息。
3)对应消息处理函数分别在类中声明,类外定义:
增加消息处理
让程序动起来吧!!!
//声明宏 提供消息映射机制
DECLARE_MESSAGE_MAP()
afx_msg void OnLButtonDown(UINT, CPoint);
afx_msg void OnChar(UINT, UINT, UINT);
afx_msg void OnPaint();
//分界宏
BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
ON_WM_LBUTTONDOWN() //鼠标左键按下
ON_WM_CHAR() //键盘
ON_WM_PAINT() //绘图宏
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中的字符串 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("按下了%c 键"), key);
MessageBox(str);
}
void MyFrame::OnPaint()
{
CPaintDC dc(this); //CDC里找其他的能画的图形
dc.TextOutW(100, 100, TEXT("为了部落"));
//画椭圆
dc.Ellipse(10, 10, 100, 100);
//多字节转为 宽字节
//TEXT是由自适应编码的转换
// TCHER 自适应编码的转换
//MessageBox(L"aaa");
//统计字符串长度
int num = 0;
char * p = "aaaa";
num = strlen(p);
//统计宽字节的字符串长度
wchar_t * p2 = L"bbbb";
num = wcslen(p2);
//char * 与 CString之间的转换 C++ string .c_str();
//char* -> CString
char * p3 = "ccc";
CString str = CString(p3);
//CString -> char *
CStringA tmp;
tmp = str;
char * pp = tmp.GetBuffer();
}

边栏推荐
- MySQL的一些随笔记录
- 打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站
- QT creator runs the Valgrind tool on external applications
- 解决安装Failed building wheel for pillow
- 安装numpy问题总结
- 图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
- LeetCode #461 汉明距离
- [recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
- Did you forget to register or load this tag
- AcWing 1298. Solution to Cao Chong's pig raising problem
猜你喜欢

QT creator test

【博主推荐】C#MVC列表实现增删改查导入导出曲线功能(附源码)

Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'

机器学习--人口普查数据分析

QT creator runs the Valgrind tool on external applications
![[number theory] divisor](/img/ec/036d7e76cc566c08d336444f2898e1.jpg)
[number theory] divisor

Summary of numpy installation problems

02 staff information management after the actual project

Learning question 1:127.0.0.1 refused our visit

Install mysql5.5 and mysql8.0 under windows at the same time
随机推荐
AcWing 1298. Solution to Cao Chong's pig raising problem
机器学习--人口普查数据分析
[AGC009D]Uninity
JDBC原理
Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
Use dapr to shorten software development cycle and improve production efficiency
Install mongdb tutorial and redis tutorial under Windows
Attention apply personal understanding to images
Rhcsa certification exam exercise (configured on the first host)
[Thesis Writing] how to write function description of jsp online examination system
数数字游戏
记某公司面试算法题:查找一个有序数组某个数字出现的次数
QT creator test
Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
[蓝桥杯2017初赛]包子凑数
QT creator custom build process
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
Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
Ubuntu 20.04 安装 MySQL
Learning question 1:127.0.0.1 refused our visit