MFC简单框架启动流程(CWinApp,CFrameWnd)

本文详细解析了MFC应用程序的启动流程,从全局对象theApp的创建到WinMain的调用,再到AfxWinMain函数,以及AfxWinInit、InitApplication、InitInstance和Run等关键步骤。在过程中,探讨了窗口类的注册、CWinApp和CFrameWnd对象的初始化,以及窗口的创建和消息循环的运行机制。通过对PreCreateWindow函数的分析,展示了如何自定义窗口样式。最后,提到了消息处理和消息映射的基本原理。
摘要由CSDN通过智能技术生成

先看下例子代码

#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()-
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值