天天看点

Create和CreateEx 区别

先来看CreateEx函数:

BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,

  LPCTSTR lpszWindowName, DWORD dwStyle,

  const RECT& rect, CWnd* pParentWnd, UINT nID,

  LPVOID lpParam /* = NULL */)

{

 return CreateEx(dwExStyle, lpszClassName, lpszWindowName, dwStyle,

  rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,

  pParentWnd->GetSafeHwnd(), (HMENU)nID, lpParam);

}

 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)

  TRACE1("Warning: Window creation failed: GetLastError returns 0x%8.8X/n",

   GetLastError());

#endif

 if (!AfxUnhookWindowCreate())

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

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

 return TRUE;

可以看到,这两个重载函数的实现原理是让第一个函数调用了第二个函数。在第二个CreateEx中实际是通过调用Win32 SDK平台的ateWindowEx(CWnd没有此函数)来创建窗口的。

再来看Create函数:

BOOL CWnd::Create(LPCTSTR lpszClassName,

 const RECT& rect,

 CWnd* pParentWnd, UINT nID,

 CCreateContext* pContext)

 // can't use for desktop or pop-up windows (use CreateEx instead)

 ASSERT(pParentWnd != NULL);

 ASSERT((dwStyle & WS_POPUP) == 0);

 return CreateEx(0, lpszClassName, lpszWindowName,

  dwStyle | WS_CHILD,

  rect.left, rect.top,

  rect.right - rect.left, rect.bottom - rect.top,

  pParentWnd->GetSafeHwnd(), (HMENU)nID, (LPVOID)pContext);

注意这两行:

 ASSERT((dwStyle & WS_POPUP) == 0);  // 该行说明Create函数不允许窗口风格为WS_POPUP

可以看到,在Create函数之中又调用了CreateEx的第二个重载版本。

总结:

归根到底,MFC是通过CreateEx函数调用CreateWindowEx函数来创建窗口的。

注意一点:这个新创建的窗口是如何与我们的窗口对象进行绑定的呢?看第二个CreateEx的return上面的那一行:

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

在这里这只是进行判断。看它的注释:// should have been set in send msg hook

奥,它说在

if (!AfxUnhookWindowCreate())

处设置了m_hWnd的值。