admin管理员组文章数量:1621657
先看下例子代码
#include <afxwin.h>
class CMyFrameWnd : public CFrameWnd
{
public:
CMyFrameWnd();
virtual ~CMyFrameWnd();
//
DECLARE_DYNCREATE(CMyFrameWnd)
DECLARE_MESSAGE_MAP()
protected:
afx_msg void OnPaint();
};
class CMyApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMyFrameWnd;
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
CMyApp theApp;
//
IMPLEMENT_DYNCREATE(CMyFrameWnd,CFrameWnd)
BEGIN_MESSAGE_MAP(CMyFrameWnd,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
CMyFrameWnd::CMyFrameWnd()
{
Create(NULL,"hello");
}
CMyFrameWnd::~CMyFrameWnd()
{
}
void CMyFrameWnd::OnPaint()
{
CPaintDC dc(this);
CRect rc;
GetClientRect(rc);
dc.DrawText("Hello",-1,&rc,DT_VCENTER | DT_CENTER | DT_SINGLELINE);
}
theApp全局对像产生,调用CWinApp构造函数
CWinApp::CWinApp(LPCTSTR lpszAppName)
{
if (lpszAppName != NULL)
m_pszAppName = _tcsdup(lpszAppName);
else
m_pszAppName = NULL;
// initialize CWinThread state
AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE();
AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread;
ASSERT(AfxGetThread() == NULL);
pThreadState->m_pCurrentWinThread = this;//
ASSERT(AfxGetThread() == this);
m_hThread = ::GetCurrentThread();
m_nThreadID = ::GetCurrentThreadId();
// initialize CWinApp state
ASSERT(afxCurrentWinApp == NULL); // only one CWinApp object please
pModuleState->m_pCurrentWinApp = this;//实际上指向CMyApp
ASSERT(AfxGetApp() == this);
// in non-running state until WinMain
m_hInstance = NULL;
m_pszHelpFilePath = NULL;
m_pszProfileName = NULL;
m_pszRegistryKey = NULL;
m_pszExeName = NULL;
m_pRecentFileList = NULL;
m_pDocManager = NULL;
m_atomApp = m_atomSystemTopic = NULL;
m_lpCmdLine = NULL;
m_pCmdInfo = NULL;
// initialize wait cursor state
m_nWaitCursorCount = 0;
m_hcurWaitCursorRestore = NULL;
// initialize current printer state
m_hDevMode = NULL;
m_hDevNames = NULL;
m_nNumPreviewPages = 0; // not specified (defaults to 1)
// initialize DAO state
m_lpfnDaoTerm = NULL; // will be set if AfxDaoInit called
// other initialization
m_bHelpMode = FALSE;
m_nSafetyPoolSize = 512; // default size
}
在这期间完成一些CWinApp对像的初始化赋值,
完成这一过程后.WinMain调用,这个WinMain,是MFC早已准备好并由连接器直接加到应用程序中的
原始代码如下:
_tWinMain代码如下
extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
// call shared/exported WinMain
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
_tWinMain之后调用AfxWinMain
AfxWinMain函数代码如下:
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
ASSERT(hPrevInstance == NULL);
int nReturnCode = -1;
CWinThread* pThread = AfxGetThread();//这里也就是CWinApp构造函数中设定的值
CWinApp* pApp = AfxGetApp();//这里也就是theApp对像
// AFX internal initialization
if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))//1
goto InitFailure;
// App global initializations (rare)
if (pApp != NULL && !pApp->InitApplication())//2
goto InitFailure;
// Perform specific initializations
if (!pThread->InitInstance())//3
{
if (pThread->m_pMainWnd != NULL)
{
TRACE0("Warning: Destroying non-NULL m_pMainWnd\n");
pThread->m_pMainWnd->DestroyWindow();
}
nReturnCode = pThread->ExitInstance();
goto InitFailure;
}
nReturnCode = pThread->Run();//4
InitFailure:
#ifdef _DEBUG
// Check for missing AfxLockTempMap calls
if (AfxGetModuleThreadState()-
版权声明:本文标题:MFC简单框架启动流程(CWinApp,CFrameWnd) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1728851440a1176723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论