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
黑色天使 阅读(586)
评论(0) 编辑 收藏 引用 所属分类:
VC&MFC