有时候不喜欢系统生成的那三种类型的MFC项目,我们有没有办法构建一个自己的MFC呢?这对于MFC
的初学者可能有点难度,且看下文步步道来:
1.建一个空的Win32 Application Projects.
New->Projects->Win32 Application->Project name(CreateMFC)->An empty Project
2.建立stdafx.h头文件
在FileView里右键Header Files,Add Files to Folder,文件名写上stdafx.h,然后OK
双击生成的stdafx.h再输入内容 #include <afxwin.h>
注:为什么要stdafx.h请参看MFC书,为什么要写afxwin.h请自己去掉研究一下呵!
3.添加Resource
File->New->Files->Resource Script 文件名就叫CreateMFC.
然后右键点在窗口中出现的CreateMFC.rc(一个文件夹图标的),
选择Resource Includes,再OK.
4.建立类信息文件
菜单里View->ClassWizard->选择"是"->OK->OK
5.设置MFC
选中此项目,右键出现Settings->General选项卡里Microsoft Foundation Classes:里选择
Use MFC in a Static Library. ->OK
6.新建窗口类
菜单Insert->New Class->Class type(MFC Class) Name(mfcFrame:可自己随便取) Base
Class(CFrameWnd)->OK
重要:将生成的Frame类的头文件(mfcFrame.h)里的构造函数mfcFrame()权限设置为public.
还有有时有必要注释一些自动包含的头文件如这里的#include "createmfc.h"
7.新建App类
菜单Insert->New Class->Class type(Generic Class) Name(mfcApp:可自取)
设置继承类CWinApp As Public(Base classes里)->是
8.完成基本显示设置
在mfcApp.h里加入成员方法InitInstance() 返回值:BOOL
在mfcApp.cpp里实现InitInstance()方法之前添加声明mfcApp myApp;
声明一个全局的app.为什么要这样?看MFC书.
包含头文件:#include "mfcFrame.h"
在mfcApp::InitInstance()里写上:
m_pMainWnd = new mfcFrame; //#include "mfcFrame.h"
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
建立Windows窗口,在mfcFrame.cpp的构造函数中加入:
Create(NULL,"Zimmerman");
为什么要这步?看MSDN说明:
Construct a CFrameWnd object in two steps. First invoke the constructor, which
constructs the CFrameWnd object, then call Create, which creates the Windows frame
window and attaches it to the CFrameWnd object. Create initializes the window’s
class name and window name and registers default values for its style, parent, and
associated menu.
通过上面步骤,一个干净的MFC便有了,讨厌微软生成的一大堆代码的朋友可以自己动手试一试.
附以上步骤源程序:/Files/zimmerman/CreateMFC.rar
浙公网安备 33010602011771号