通过VC++ 新建一个MFC单文档应用程序,其工程名为mfcproject.新建后通过类视图可以看到一共有5个类:
CAboutDlg:对话框类,如关于对话框
CMainFrame:应用程序框架类,包括工具栏菜单等
CMfcprojectApp:应用程序类
CMfcprojectDoc:文档类
CMfcprojectView:也是和文档有关的视图类
1、mfcproject.cpp文件中定义了全局类 CMfcprojectApp theApp;在这个地方设置断点,调试运行可以发现整个程序首先执行到这个地方。
2、全局变量定义后将会调用CMfcprojectApp 类的构造函数
3、从mfcproject.h中可以发现 CMfcprojectApp继承自CWinApp类(在APPCORE.cpp中),根据继承性原理,程序将会首先调用CWinApp类的构造函数,如果细心地话会发现CWinApp的构造函数有参数,但CMfcprojectApp类构造函数并没有带参数,这是怎么回事呢?再仔细看下CWinApp类的构造函数:CWinApp(LPCTSTR lpszAppName = NULL); 看见了没这里的参数是默认值,这也就是为什么CMfcprojectApp构造函数没有带参数的缘故了
4、定义好全局变量以后,就要找主函数入口了,MFC程序的主函数入口为
![](http://img.e-com-net.com/image/info8/7da3525d42b249f380f90d23d1eaafba.gif)
2
![](http://img.e-com-net.com/image/info8/7da3525d42b249f380f90d23d1eaafba.gif)
3
![](http://img.e-com-net.com/image/info8/7da3525d42b249f380f90d23d1eaafba.gif)
4
![ExpandedBlockStart.gif](http://img.e-com-net.com/image/info8/a851f6ebab08498cbe1d2f2c2b23a0c9.gif)
![ContractedBlock.gif](http://img.e-com-net.com/image/info8/70c152915daf47829a33744d52567c74.gif)
![](http://img.e-com-net.com/image/info8/9bb15f9e61964d8f8ba53621bef8c92f.png)
5
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
6
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
7
![](http://img.e-com-net.com/image/info8/2c9100fab8554b7caad7b3144808b323.gif)
该函数在APPMODUL.cpp中,从这个函数可以看出真正的入口函数应该为AfxWinMain(Winmain.cpp中)
2 LPTSTR lpCmdLine, int nCmdShow)
3 {
4 ASSERT(hPrevInstance == NULL);
5
6 int nReturnCode = - 1 ;
7 CWinThread * pThread = AfxGetThread();
8 CWinApp * pApp = AfxGetApp(); //获取子类
9
10 // AFX internal initialization
11 if ( ! AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
12 goto InitFailure;
13
14 // App global initializations (rare)
15 if (pApp != NULL && ! pApp -> InitApplication())
16 goto InitFailure;
17
18 // Perform specific initializations
19 if ( ! pThread -> InitInstance()) //初始化注册窗口
20 {
21 if (pThread -> m_pMainWnd != NULL)
22 {
23 TRACE0( " Warning: Destroying non-NULL m_pMainWnd\n " );
24 pThread -> m_pMainWnd -> DestroyWindow();
25 }
26 nReturnCode = pThread -> ExitInstance();
27 goto InitFailure;
28 }
29 nReturnCode = pThread -> Run();
30
31 InitFailure:
32 #ifdef _DEBUG
33 // Check for missing AfxLockTempMap calls
34 if (AfxGetModuleThreadState() -> m_nTempMapLock != 0 )
35 {
36 TRACE1( " Warning: Temp map lock count non-zero (%ld).\n " ,
37 AfxGetModuleThreadState() -> m_nTempMapLock);
38 }
39 AfxLockTempMaps();
40 AfxUnlockTempMaps( - 1 );
41 #endif
42
43 AfxWinTerm();
44 return nReturnCode;
45 }
46
关键的语句是line8获取子类实例,line19初始化子类,完成窗口注册,line20可以在这些行上设置断点
5、 由于pThred当前指向子类,InitInstance函数是虚函数,因此 pThread->InitInstance()将调用子类的CMfcprojectApp::InitInstance()
![](http://img.e-com-net.com/image/info8/7da3525d42b249f380f90d23d1eaafba.gif)
2
![ExpandedBlockStart.gif](http://img.e-com-net.com/image/info8/a851f6ebab08498cbe1d2f2c2b23a0c9.gif)
![ContractedBlock.gif](http://img.e-com-net.com/image/info8/70c152915daf47829a33744d52567c74.gif)
![](http://img.e-com-net.com/image/info8/9bb15f9e61964d8f8ba53621bef8c92f.png)
3
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
4
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
5
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
6
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
7
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
8
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
9
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
10
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
11
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
12
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
13
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
14
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
15
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
16
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
17
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
18
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
19
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
20
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
21
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
22
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
23
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
24
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
25
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
26
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
27
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
28
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
29
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
30
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
31
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
32
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
33
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
34
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
35
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
36
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
37
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
38
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
39
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
40
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
41
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
42
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
43
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
44
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
45
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
46
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
47
![](http://img.e-com-net.com/image/info8/2c9100fab8554b7caad7b3144808b323.gif)
![](http://img.e-com-net.com/image/info8/7da3525d42b249f380f90d23d1eaafba.gif)
上述代码中红色部分是比较重要的地方,可以在这些地方设置断点。
6、窗口注册会调用函数BOOL AFXAPI AfxEndDeferRegisterClass(LONG fToRegister)在文件WINCORE.CPP中
7、紧接着调用BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs),创建窗口
![](http://img.e-com-net.com/image/info8/7da3525d42b249f380f90d23d1eaafba.gif)
2
![ExpandedBlockStart.gif](http://img.e-com-net.com/image/info8/a851f6ebab08498cbe1d2f2c2b23a0c9.gif)
![ContractedBlock.gif](http://img.e-com-net.com/image/info8/70c152915daf47829a33744d52567c74.gif)
![](http://img.e-com-net.com/image/info8/9bb15f9e61964d8f8ba53621bef8c92f.png)
3
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
4
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
5
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
6
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
7
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
8
![](http://img.e-com-net.com/image/info8/631f42b035dc4497b25c141371b40678.gif)
9
![](http://img.e-com-net.com/image/info8/2c9100fab8554b7caad7b3144808b323.gif)
8、接下来调用line3父类的precreatewindow函数
9、由于窗口包括frame框架类,因此需要调用cfrmwnd的create函数(WINFRM.cpp中),这个函数又会调用Cfrmwnd的父类CWnd::CreateEx函数
10、注册完窗口后就会执行消息响应,消息响应是通过 AfxWinMain中nReturnCode = pThread->Run()实现的
11、用一个dowhile循环实现消息循环 PumpMessage()
大概的MFC实现就写到这,可能有很多不对的地方,等熟悉以后再来更新!