090917(星期四):MFC消息路由2 , Frame8代码分析

一、Frame8源码分析

1,以main函数为线索

void main()

{

       CWinApp* pApp = AfxGetApp();

 

       pApp->InitApplication();

       pApp->InitInstance();

       pApp->Run();

 

       CMyDoc* pMyDoc = new CMyDoc;

       CMyView* pMyView = new CMyView;

       CFrameWnd* pMyFrame = (CFrameWnd*)pApp->m_pMainWnd; //此处的m_pMainWnd = new CMyFrameWnd

       pMyFrame->m_pViewActive = pMyView;

       pMyView->m_pDocument = pMyDoc;

 

       // test Message Routing

       cout << endl << "pMyFrame received a WM_CREATE, routing path :" << endl;

       AfxWndProc(0, WM_CREATE, 0, 0, pMyFrame);

 

       cout << endl << "pMyView received a WM_PAINT, routing path :" << endl;

       AfxWndProc(0, WM_PAINT, 0, 0, pMyView);

 

       cout << endl << "pMyView received a WM_COMMAND, routing path :" << endl;

       AfxWndProc(0, WM_COMMAND, 0, 0, pMyView);

 

       cout << endl << "pMyFrame received a WM_COMMAND, routing path :" << endl;

       AfxWndProc(0, WM_COMMAND, 0, 0, pMyFrame);

}

 

2,第一行

       CWinApp* pApp = AfxGetApp();

全局函数AfxGetApp

CWinApp* AfxGetApp()

{

       return theApp.m_pCurrentWinApp;

}

theApp是一个全局变量:

extern CMyWinApp theApp;

类型CMyWinApp,成员m_pCurrentWinApp是从基类CWinApp继承而来:

class CMyWinApp : public CWinApp

{

public:

       CMyWinApp::CMyWinApp() {

       }

       CMyWinApp::~CMyWinApp() {

       }

       virtual BOOL InitInstance();

       DECLARE_MESSAGE_MAP()

};

 

3,类CWinApp

CObject

----CCmdTarget

--------CWinThread

------------CWinApp

下面是msdn中的描述,认真读一下你不会后悔的,让认识加深不少。

The CWinApp class is the base class from which you derive a Windows application object. An application object provides member functions for initializing your application (and each instance of it) and for running the application.

Each application that uses the Microsoft Foundation classes can only contain one object derived from CWinApp. This object is constructed when other C++ global objects are constructed and is already available早于主函数开始 when Windows calls the WinMain function, which is supplied by the Microsoft Foundation Class Library. Declare your derived CWinApp object at the global level.

When you derive an application class from CWinApp, override the InitInstance member function to create your application's main window object.

In addition to the CWinApp member functions, the Microsoft Foundation Class Library provides the following global functions to access your CWinApp object and other global information:

  • AfxGetApp   Obtains a pointer to the CWinApp object.
  • AfxGetInstanceHandle   Obtains a handle to the current application instance.
  • AfxGetResourceHandle   Obtains a handle to the application's resources.
  • AfxGetAppName   Obtains a pointer to a string containing the application's name. Alternately, if you have a pointer to the CWinApp object, use m_pszExeName to get the application's name.

See CWinApp: The Application Class for more on the CWinApp class, including an overview of the following:

CWinApp: The Application Class

The main application class in MFC encapsulates the initialization, running, and termination of an application for the Windows operating system. An application built on the framework must have one and only one object of a class derived from CWinApp. This object is constructed before windows are created.

CWinApp is derived from CWinThread, which represents the main thread of execution for your application, which might have one or more threads. In recent versions of MFC, the InitInstance, Run, ExitInstance, and OnIdle member functions are actually in class CWinThread. These functions are discussed here as if they were CWinApp members instead, because the discussion concerns the object's role as application object rather than as primary thread.

Like any program for the Windows operating system, your framework application has a WinMain function. In a framework application, however, you do not write WinMain. It is supplied by the class library and is called when the application starts up. WinMain performs standard services such as registering window classes. It then calls member functions of the application object to initialize and run the application. (You can customize WinMain by overriding the CWinApp member functions that WinMain calls.)

To initialize the application, WinMain calls your application object's InitApplication and InitInstance member functions. To run the application's message loop, WinMain calls the Run member function. On termination, WinMain calls the application object's ExitInstance member function. The following figure shows the sequence of execution in a framework applicati

Msdn2006版比前几版丰富了不少内容,难得的好文档。

 

4Frame8中模拟的CWinApp类:

class CWinApp : public CWinThread

{

public:

       CWinApp* m_pCurrentWinApp;

       CWnd* m_pMainWnd;   //为什么要定义这个新的变量

       CWinApp::CWinApp() {

              m_pCurrentWinApp = this;

       }

}

回到主函数的第一行,

CWinApp* pApp = AfxGetApp();

意思就是把全局变量CMyWinApp theApp; // global object

的指针给pApp。相当于是CWinApp* pApp = &theApp;可以调用动态的虚函数。

详细的过程:

CMyWinApp theApp; // global object,定义的时候调用构造函数

       CMyWinApp::CMyWinApp() {

       }

执行的过程是:

       CWinApp::CWinApp() {

              m_pCurrentWinApp = this;

       }中的内容

 

m_pCurrentWinApp = this;

相当于是CMyWinApp:: m_pCurrentWinApp = CMyWinApp::this

 

 

 

5main函数第2-4

       pApp->InitApplication();

       pApp->InitInstance();

       pApp->Run();

知道pApp所指的真实类型,这几个函数就清楚了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值