C++第一篇:从0开始创建一个MFC程序,就可以知道MFC是怎么回事了
1、创建一个新的Win32项目:


2、添加头文件:stdafx.h
// stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常更改的 // 特定于项目的包含文件 #pragma once #ifndef VC_EXTRALEAN #define VC_EXTRALEAN // 从 Windows 头中排除极少使用的资料 #endif #include "targetver.h" #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // 某些 CString 构造函数将是显式的 // 关闭 MFC 对某些常见但经常可放心忽略的警告消息的隐藏 #define _AFX_ALL_WARNINGS #include <afxwin.h> // MFC 核心组件和标准组件 #include <afxext.h> // MFC 扩展 #include <afxole.h> // MFC OLE 类 #include <afxodlgs.h> // MFC OLE 对话框类 #include <afxdisp.h> // MFC 自动化类 #ifndef _AFX_NO_OLE_SUPPORT #include <afxdtctl.h> // MFC 对 Internet Explorer 4 公共控件的支持 #endif #ifndef _AFX_NO_AFXCMN_SUPPORT #include <afxcmn.h> // MFC 对 Windows 公共控件的支持 #endif // _AFX_NO_AFXCMN_SUPPORT #include <afxcontrolbars.h> // 功能区和控件条的 MFC 支持 #include <afxrich.h> // MFC Rich Edit 类 #ifdef _UNICODE #if defined _M_IX86 #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") #elif defined _M_X64 #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"") #else #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") #endif #endif
3、添加头文件:targetver.h
#pragma once // 包括 SDKDDKVer.h 将定义最高版本的可用 Windows 平台。 // 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将 // WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。 #include <SDKDDKVer.h>
4、类向导:添加一个窗口Frame:

不出意外的话,在类向导里:就能找到MFC类

由于没有添加资源,所以提示了:

做一个添加资源的动作,然后再次添加MFC类:

在MainFrm.cpp里,添加
CMainFrm::CMainFrm()
{
Create(NULL, L"Hello ZhengPengfei.");
}
CMainFrm::~CMainFrm()
{
}
5、类向导:添加一个MFC类:CMyApp:CWinApp,用来启动程序
由于还不知道怎么直接创建Main函数用来启动,暂时只好用这个来启动。

6、把CMyApp的protected改为public

同样的,把CMainFrm里的protected也改为public

7、在MyApp.cpp里:增加 CMyApp theApp; 所有的一切都是从它开始的!

然后它就会开始执行 CMyApp::InitInstance();
接下来,咱们就在
BOOL CMyApp::InitInstance()
{
CMainFrm * MainFrm = new CMainFrm;
if (!MainFrm || !MainFrm->Create(NULL, L"Hello ZhengPengfei.")) //增加了这句后,可以不用在MainFrm.cpp里增加Create语句了。
{
delete MainFrm;
return FALSE;
}
//MainFrm->Create(NULL, L"Hello ZhengPengfei.");
MainFrm->ShowWindow(SW_SHOWNORMAL);
MainFrm->UpdateWindow();
m_pMainWnd = MainFrm;
return TRUE;
}8、把配置里的:配置属性->常规->MFC的使用:改为静态或共享

9、按F5或者点“本地Windows调试器”,恭喜!终于写了第一个可以运行的有窗口的C++ MFC程序!


转载请注明 : 文章转载自不了阁-飞哥 blog.forbs.cn
本站文章除注明转载/出处外,均为本站原创或翻译。若要转载请务必注明出处,尊重他人劳动成果共创和谐网络环境。



