第一个MFC程序

版权声明:转载请注明出处: https://blog.csdn.net/qq_33757398/article/details/82263714

1.MFC使用C++语言把Windows SDK API函数包装成了几百个类

2.最重要的两个MFC类

1)CwinApp

2)CFrameWnd

3.两种方法

1)一个继承

2)两个继承

4.具体实现

4.0VS设置

1)正常创建控制台程序

2)设置项目属性

在常规中:

a.将MFC的使用改为-》在静态库中使用MFC(动态也行)

b.将字符集改为-》使用Unicode字符集

4.1使用一个继承

MyApp.h文件:

#pragma once
//#define _WIN32_WINNT 0x0502
#include <afxwin.h>

class MyApp :public CWinApp
{
public:
	BOOL InitInstance()//程序入口点
	{
		//创建框架窗口对象
		CFrameWnd *f = new CFrameWnd();

		this->m_pMainWnd = f;

		//创建窗口
		f->Create(NULL, TEXT("Hello,this is my first MFC"));
		f->ShowWindow(SW_SHOW);

		return true;
	}
};

helloMFC.app文件:

#include "MyApp.h"

MyApp app;

运行结果:

一个空白窗口

但是VS2017中有一个警告:

_WIN32_WINNT not defined. Defaulting to _WIN32_WINNT_MAXVER (see WinSDKVer.h)

解决办法,就是在文件中添加:

#define _WIN32_WINNT 0x0502

https://blog.csdn.net/whatday/article/details/38063889

https://blog.csdn.net/xiaolongwang2010/article/details/7550505

4.2使用两个继承

MyApp.h文件:

#pragma once
#define _WIN32_WINNT 0x0502
#include <afxwin.h>

class MyApp :public CWinApp
{
public:
	virtual BOOL InitInstance();
};

class MyMainWindow :public CFrameWnd
{
public:
	MyMainWindow();
};

MyApp.cpp文件:

#include "MyApp.h"

BOOL MyApp::InitInstance()
{
	this->m_pMainWnd = new MyMainWindow();
	this->m_pMainWnd->ShowWindow(this->m_nCmdShow);
	this->m_pMainWnd->UpdateWindow();

	return true;
}

MyMainWindow::MyMainWindow()
{
	Create(NULL, TEXT("hello,this is my second MFC"));
}

helloMFC.cpp文件:

#include "MyApp.h"

MyApp app;

运行结果与上面一样

猜你喜欢

转载自blog.csdn.net/qq_33757398/article/details/82263714