MFC文档视图结构框架分析(4)

2019-03-09 22:08

}

(2)pMainFrame->LoadFrame(IDR_MAINFRAME)所引发的一系列

BOOL CMDIFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle, CWnd* pParentWnd, CCreateContext* pContext) {

if (!CFrameWnd::LoadFrame(nIDResource, dwDefaultStyle, pParentWnd, pContext)) return FALSE;

// save menu to use when no active MDI child window is present ASSERT(m_hWnd != NULL);

m_hMenuDefault = ::GetMenu(m_hWnd); if (m_hMenuDefault == NULL)

TRACE(traceAppMsg, 0, \\\n\

return TRUE; }

BOOL CFrameWnd::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle, CWnd* pParentWnd, CCreateContext* pContext) {

// only do this once

ASSERT_VALID_IDR(nIDResource);

ASSERT(m_nIDHelp == 0 || m_nIDHelp == nIDResource);

m_nIDHelp = nIDResource; // ID for help context (+HID_BASE_RESOURCE)

CString strFullString;

if (strFullString.LoadString(nIDResource))

AfxExtractSubString(m_strTitle, strFullString, 0); // first sub-string

VERIFY(AfxDeferRegisterClass(AFX_WNDFRAMEORVIEW_REG)); // attempt to create the window

LPCTSTR lpszClass = GetIconWndClass(dwDefaultStyle, nIDResource); CString strTitle = m_strTitle;

if (!Create(lpszClass, strTitle, dwDefaultStyle, rectDefault, pParentWnd, MAKEINTRESOURCE(nIDResource), 0L, pContext)) {

return FALSE; // will self destruct on failure normally }

// save the default menu handle ASSERT(m_hWnd != NULL);

m_hMenuDefault = ::GetMenu(m_hWnd); // load accelerator resource

LoadAccelTable(MAKEINTRESOURCE(nIDResource)); if (pContext == NULL) // send initial update

SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, TRUE, TRUE); return TRUE; }

BOOL CFrameWnd::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, LPCTSTR lpszMenuName, DWORD dwExStyle,

CCreateContext* pContext) {

HMENU hMenu = NULL;

if (lpszMenuName != NULL) {

// load in a menu that will get destroyed when window gets destroyed

HINSTANCE hInst = AfxFindResourceHandle(lpszMenuName, RT_MENU); if ((hMenu = ::LoadMenu(hInst, lpszMenuName)) == NULL) {

TRACE(traceAppMsg, 0, \nd.\\n\

PostNcDestroy(); // perhaps delete the C++ object return FALSE; } }

m_strTitle = lpszWindowName; // save title for later

if (!CreateEx(dwExStyle, lpszClassName, lpszWindowName, dwStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,

pParentWnd->GetSafeHwnd(), hMenu, (LPVOID)pContext)) {

TRACE(traceAppMsg, 0, \ if (hMenu != NULL) DestroyMenu(hMenu); return FALSE; }

return TRUE; }

因为CFrameWnd没有重新写CreateEX,所以是调用的基类的CreateEx的函数: BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight,

HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam) {

// allow modification of several common create parameters CREATESTRUCT cs;

cs.dwExStyle = dwExStyle; cs.lpszClass = lpszClassName; cs.lpszName = lpszWindowName; cs.style = dwStyle; cs.x = x; cs.y = y; cs.cx = nWidth; cs.cy = nHeight;

cs.hwndParent = hWndParent; cs.hMenu = nIDorHMenu;

cs.hInstance = AfxGetInstanceHandle(); cs.lpCreateParams = lpParam;

if (!PreCreateWindow(cs)) {

PostNcDestroy(); return FALSE; }

AfxHookWindowCreate(this);

HWND hWnd = ::CreateWindowEx(cs.dwExStyle, cs.lpszClass, cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,

cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);

#ifdef _DEBUG if (hWnd == NULL) {

TRACE(traceAppMsg, 0, \r returns 0x%8.8X\\n\

GetLastError()); } #endif

if (!AfxUnhookWindowCreate())

PostNcDestroy(); // cleanup if CreateWindowEx fails too soon

if (hWnd == NULL) return FALSE;

ASSERT(hWnd == m_hWnd); // should have been set in send msg hook return TRUE; }

可以看到,::CreateWindowEx是一个全局的函数,在其中触发WM_CREATE消息,进而调用我们自己定义的CMainFrame::OnCreate()

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {

if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1) return -1;

if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP

| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))


MFC文档视图结构框架分析(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2017会计硕士考研备考研习:英语词汇强化练习题(四)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: