WEB开发网
开发学院软件开发VC MFC程序中的WinMain函数(2) 阅读

MFC程序中的WinMain函数(2)

 2008-11-24 19:59:38 来源:WEB开发网   
核心提示: 在例3-7所示的代码中,AfxWinMain首先调用AfxGetThread函数获得一个CWinThread类型的指针,MFC程序中的WinMain函数(2)(5),接着调用AfxGetApp函数获得一个CWinApp类型的指针,从MFC类库组织结构图(读者可以按照前面介绍的方法在MSDN

在例3-7所示的代码中,AfxWinMain首先调用AfxGetThread函数获得一个CWinThread类型的指针,接着调用AfxGetApp函数获得一个CWinApp类型的指针。从MFC类库组织结构图(读者可以按照前面介绍的方法在MSDN中找到该结构图)中,可以知道CWinApp派生于CWinThread。例3-8是AfxGetThread函数的源代码,位于THRDCORE.CPP文件中。

例3-8

  CWinThread* AFXAPI AfxGetThread()
  {
    // check for current thread in module thread state
    AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
    CWinThread* pThread = pState->m_pCurrentWinThread;
    // if no CWinThread for the module, then use the global app
    if (pThread == NULL)
      pThread = AfxGetApp();
 return pThread;
  }

从例3-8所示代码中可以发现,AfxGetThread函数返回的就是AfxGetApp函数的结果。因此,AfxWinMain函数中的pThread和pApp这两个指针是一致的。

AfxGetApp是一个全局函数,定义于AFXWIN1.INL中:

_AFXWIN_INLINE CWinApp* AFXAPI AfxGetApp()

{ return afxCurrentWinApp; }

而afxCurrentWinApp的定义位于AFXWIN.H文件中,代码如下:

#define afxCurrentWinApp  AfxGetModuleState()->m_pCurrentWinApp

我们返回去看看前面例3-5所示的CWinApp构造函数代码,就可以知道AfxGetApp函数返回的是在CWinApp构造函数中保存的this指针。对Test程序来说,这个this指针实际上指向的是CTestApp的对象:theApp。也就是说,对Test程序来说,pThread和pApp所指向的都是CTestApp类的对象,即theApp全局对象。

上一页  1 2 3 4 5 6  下一页

Tags:MFC 程序 WinMain

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接