当前位置:网站首页>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();
}

边栏推荐
- Software testing and quality learning notes 3 -- white box testing
- [free setup] asp Net online course selection system design and Implementation (source code +lunwen)
- Number game
- QT creator specify editor settings
- AcWing 1298. Solution to Cao Chong's pig raising problem
- What does usart1 mean
- QT creator specifies dependencies
- Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
- Ansible实战系列二 _ Playbook入门
- La table d'exportation Navicat génère un fichier PDM
猜你喜欢

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

Windows下安装MongDB教程、Redis教程

02 staff information management after the actual project

Django running error: error loading mysqldb module solution

引入了junit为什么还是用不了@Test注解

Did you forget to register or load this tag 报错解决方法

Redis的基础使用

AcWing 1298. Solution to Cao Chong's pig raising problem

【博主推荐】asp.net WebService 后台数据API JSON(附源码)

error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
随机推荐
Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
Test objects involved in safety test
Ansible practical Series III_ Task common commands
How to set up voice recognition on the computer with shortcut keys
How to build a new project for keil5mdk (with super detailed drawings)
Project practice - background employee information management (add, delete, modify, check, login and exit)
Summary of numpy installation problems
AcWing 1294.樱花 题解
【博主推荐】C# Winform定时发送邮箱(附源码)
[recommended by bloggers] C WinForm regularly sends email (with source code)
机器学习--人口普查数据分析
L2-007 家庭房产 (25 分)
TCP/IP协议(UDP)
Asp access Shaoxing tourism graduation design website
NPM an error NPM err code enoent NPM err syscall open
虚拟机Ping通主机,主机Ping不通虚拟机
Julia 1.6 1.7 common problem solving
学习问题1:127.0.0.1拒绝了我们的访问
Armv8-a programming guide MMU (2)
Base de données Advanced Learning Notes - - SQL statements