MFC之CFrameWnd类的探索

来源:互联网 发布:redis 命令查看数据库 编辑:程序博客网 时间:2024/05/17 22:30


CFrameWnd::Create(.....)




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, ATL_RT_MENU);
// 加载菜单
if ((hMenu = ::LoadMenu(hInst, lpszMenuName)) == NULL)
{
TRACE(traceAppMsg, 0, "Warning: failed to load menu for CFrameWnd.\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, "Warning: failed to create CFrameWnd.\n");
if (hMenu != NULL)
DestroyMenu(hMenu);
return FALSE;
}


return TRUE;
}




在Create函数中会调用CreateEx,
而在CreateEx中会调用PreCreateWindow()




CreateEx 的源码如下,并且行下面的源码中可以看出 PreCreateWindow这个虚函数,可以修改窗口类的属性
并且是有作用的.


  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)
{
ASSERT(lpszClassName == NULL || AfxIsValidString(lpszClassName) || 
AfxIsValidAtom(lpszClassName));
ENSURE_ARG(lpszWindowName == NULL || AfxIsValidString(lpszWindowName));


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 的作用是打印一些信息到即时窗口 .
TRACE(traceAppMsg, 0, "Warning: Window creation failed: GetLastError returns 0x%8.8X\n",
GetLastError());
}
#endif


if (!AfxUnhookWindowCreate())
PostNcDestroy();        


if (hWnd == NULL)
return FALSE;
ASSERT(hWnd == m_hWnd);
return TRUE;
}
0 0