windows默認的入口函數WinMain直接調用MFC函數AfxWinMain
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)


{
// call shared/exported WinMain
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)


{
CWinThread* pThread = AfxGetThread();
CWinApp* pApp = AfxGetApp();
// 主要進行內部初始化和調用了AfxInitThread()
if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
// 相當于調用CWinApp的InitApplication(),與Document Template 和CDocManager相關初始化
if (pApp != NULL && !pApp->InitApplication())
AfxInitThread()
// 相當于調用CMyWinApp中的InitInstance()
if (!pThread->InitInstance())

{}
// 相當于調用CWinApp中的函數
nReturnCode = pThread->Run();

return nReturnCode;
}
BOOL CMYApp::InitInstance()


{
//對話框程序的初始化
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

CMYDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal(); //產生模態對話框
if (nResponse == IDOK)

{
}
else if (nResponse == IDCANCEL)

{
}
return FALSE; //不同之處,對話框初始化程序返回的是FALSE,這樣,RUN就不會被調用
}
int CDialog::DoModal()


{
// can be constructed with a resource template or InitModalIndirect
ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
m_lpDialogTemplate != NULL);

// 載入需要的資源文件
LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
HGLOBAL hDialogTemplate = m_hDialogTemplate;
HINSTANCE hInst = AfxGetResourceHandle();
if (m_lpszTemplateName != NULL)

{
hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
hDialogTemplate = LoadResource(hInst, hResource);
}
if (hDialogTemplate != NULL)
lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);

// return -1 in case of failure to load the dialog template resource
if (lpDialogTemplate == NULL)
return -1;

// 在創建窗口前,隱藏父窗口
HWND hWndParent = PreModal();
AfxUnhookWindowCreate();
BOOL bEnableParent = FALSE;
if (hWndParent != NULL && ::IsWindowEnabled(hWndParent))

{
::EnableWindow(hWndParent, FALSE);
bEnableParent = TRUE;
}

TRY

{
//調用CreateDlgIndirect創建對話框
AfxHookWindowCreate(this);
if (CreateDlgIndirect(lpDialogTemplate,
CWnd::FromHandle(hWndParent), hInst))

{
if (m_nFlags & WF_CONTINUEMODAL)

{
//RunModalLoop中開始消息循環
DWORD dwFlags = MLF_SHOWONIDLE;
if (GetStyle() & DS_NOIDLEMSG)
dwFlags |= MLF_NOIDLEMSG;
VERIFY(RunModalLoop(dwFlags) == m_nModalResult);
}

// 在父窗口出現前,先隱藏窗口
if (m_hWnd != NULL)
SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW|
SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
}
}
CATCH_ALL(e)

{
DELETE_EXCEPTION(e);
m_nModalResult = -1;
}
END_CATCH_ALL

if (bEnableParent)
::EnableWindow(hWndParent, TRUE);
if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
::SetActiveWindow(hWndParent);

// 銷毀狂口
DestroyWindow();
PostModal();

// 釋放資源
if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL)
UnlockResource(hDialogTemplate);
if (m_lpszTemplateName != NULL)
FreeResource(hDialogTemplate);

return m_nModalResult;
}
// 文檔程序的初始化
BOOL CMy4App::InitInstance()


{
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_MYTYPE,
RUNTIME_CLASS(CMyDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CMyView));
AddDocTemplate(pDocTemplate);

// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;

pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();

return TRUE;
}
一開始NEW了一個CFrameWnd對象,pMainFrame->LoadFrame()引發連鎖反映-CFrameWnd::Create()-CWnd::CreateEx()-::CreateWindowEx()觸發WM_CREATE其中調用宏AfxDeferRegisterClass注冊窗口類,默認的窗口類對應為下表
CWnd |
CFameWnd |
CMDIFrameWnd |
CMDIChildWnd |
CView |
_afxWnd |
_afxWndFrameOrView |
_afxWndMDIFrame |
_afxWndFrameOrView |
_afxWndFrameOrView |
posted on 2008-08-31 01:03
黑色天使 閱讀(602)
評論(0) 編輯 收藏 引用 所屬分類:
VC&MFC