MFC程序框架剖析(1)

来源:互联网 发布:淘宝怎样营销推广 编辑:程序博客网 时间:2024/04/28 08:24
这两天在看孙鑫老师的《VC++深入详解》 ,看到第3章就晕了…………没有怎么看明白~~只好一边看书一边自己研究。
首先还是按照书中所说,找到了WinMain函数的所在。
它实际对应的宏是_tWinMain这个比较好理解:#define _tWinMain   WinMain
然后我们来看看这个MFC所定义的WinMain函数
extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine, 
int nCmdShow)
{
    
// call shared/exported WinMain
    return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
如果你了解Win32程序的过程你就会发现,实际上AfxWinMain才是真的WinMain,它里面实现了窗体运行的所有过程
而_tWinMain不过是一个壳…………
然后继续在MFC的SRC目录里面搜索文件中的字和词组…………AfxWinMain
然后~我们可以很有趣的发现原来它就在WINMAIN.CPP文件中…………从文件命名我们不难发现~这个文件的实际含义……,好下面我们分析一下里面的代码。
/////////////////////////////////////////////////////////////////////////////
// Standard WinMain implementation【标准的WinMain执行】
//  Can be replaced as long as 'AfxWinInit' is called first【可在AfxWinInit被第一次调用时被取代】
//这里的四个参数和WinMain的四个参数是一样的.分别表示:
//hInstance        当前实例句柄
//hPrevInstance        表示当前实例的前一个实例的句柄
//lpCmdLine        指定传递给应用程序的命令行参数
//nCmdShow        指定程序的窗口应该如何显示
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine, 
int nCmdShow)
{
    
//ASSERT()是一个调试程序时经常使用的宏
    
//如果表达式不为0,则继续执行后面的语句。
    ASSERT(hPrevInstance == NULL);

    
int nReturnCode = -1;
    CWinThread
* pThread = AfxGetThread();
    CWinApp
* pApp = AfxGetApp();

    
// AFX internal initialization 【AFX 内部初始化】
    if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
        
goto InitFailure;

    
// App global initializations (rare)【App全局初始化】
    if (pApp != NULL && !pApp->InitApplication())
        
goto InitFailure;

    
// Perform specific initializations【平台细节初始化】
    if (!pThread->InitInstance())
    
{
        
//如果初始化错误,并且改线程的主窗体成员对象不为空时先销毁主窗体对象
        if (pThread->m_pMainWnd != NULL)
        
{
            TRACE0(
"Warning: Destroying non-NULL m_pMainWnd ");
            pThread
->m_pMainWnd->DestroyWindow();
        }

        
//推出初始话,设置其错误码,跳出If语句
        nReturnCode = pThread->ExitInstance();
        
goto InitFailure;
    }

    
//运行?暂时不清楚这里做了什么
    nReturnCode = pThread->Run();

InitFailure:
//调试,暂时不管它
#ifdef _DEBUG
    
// Check for missing AfxLockTempMap calls
    if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
    
{
        TRACE1(
"Warning: Temp map lock count non-zero (%ld). ",
            AfxGetModuleThreadState()
->m_nTempMapLock);
    }

    AfxLockTempMaps();
    AfxUnlockTempMaps(
-1);
#endif
    
//???消息处理??
    AfxWinTerm();
    
return nReturnCode;
}


/////////////////////////////////////////////////////////////////////////////
原创粉丝点击