MFC中自訂視窗類別名

來源:互聯網
上載者:User

MFC中封裝很多常用的控制項,把類名也給封裝了,沒有提供明顯的介面出來,用win api寫視窗程序, 第一步就是註冊視窗類別。

此時類名和標題名是一起註冊的,所以能把標題很好地讓使用者來設定,類名也應該是很簡單的,可惜 的是MFC沒有這樣做,原因也許是window name可以不停的改,而類名不能。視窗的類名是有Create來確定 的,要在Create前,給視窗選擇一個已經註冊的視窗類別名,作為參數視窗 Create就ok了,CWnd的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)(UINT_PTR)nID, lpParam);
}
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));
   // 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 = ::AfxCtxCreateWindowEx(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, "Warning: Window creation failed: GetLastError  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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.