CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的區別

MFC(VC6.0)的CWnd及其子類中,有以下三個函數:javascript

 

Cpp代碼 
  1. class CWnd : public CCmdTarget  
  2. {  
  3. public:      
  4.     virtual BOOL PreCreateWindow(CREATESTRUCT& cs);  
  5.     virtual void PreSubclassWindow();  
  6.     BOOL SubclassWindow(HWND hWnd);      
  7. };  
 

 

讓人很不容易區分,不知道它們究竟幹了些什麼,在什麼狀況下要改寫哪一個函數?java

想知道改寫函數?讓我先告訴你哪一個不能改寫,那就是SubclassWindow。Scott Meyers的傑做<<Effective C++>>的第36條是這樣的Differentiate between inheritance of interface and inheritance of implementation. 看了後你立刻就知道,父類中的非虛擬函數是設計成不被子類改寫的。根據有無virtual關鍵字,咱們在排除了SubclassWindow後,也就知道PreCreateWindow和PreSubClassWindow是被設計成可改寫的。接着的問題即是該在何時該寫了。要知道何時該寫,必須知道函數是在何時被調用,還有執行函數的想要達到的目的。咱們先看看對這三個函數,MSDN給的解釋:windows

PreCreateWindow:app

Called by the framework before the creation of the Windows window ide

attached to this CWnd object.函數

(譯:在窗口被建立並attach到this指針所指的CWnd對象以前,被framework調用)this

PreSubclassWindow:spa

This member function is called by the framework to allow other necessary 設計

subclassing to occur before the window is subclassed.指針

(譯:在window被subclassed以前被framework調用,用來容許其它必要的subclassing發生)

雖然我已有譯文,但仍是讓我對CWnd的attach和窗口的subclass做簡單的解釋吧!要理解attach,咱們必需要知道一個C++的CWnd對象和窗口(window)的區別:window就是實在的窗口,而CWnd就是MFC用類對window所進行C++封裝。attach,就是把窗口附加到CWnd對象上操做。附加(attach)完成後,CWnd對象才和窗口發生了聯繫。窗口的subclass是指修改窗口過程的操做,而不是面向對象中的派生子類。

好了,PreCreateWindow由framework在窗口建立前被調用,函數名也說明了這一點,Pre應該是previous的縮寫,PreSubclassWindow由framework在subclass窗口前調用。 這段話說了等於沒說,你可能仍是不知道,何時該改寫哪一個函數。羅羅嗦嗦的做者,仍是用代碼說話吧!源碼以前,了無祕密(候捷語)。咱們就看看MFC中的這三個函數都是這樣實現的吧!

 

Cpp代碼 
  1. BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,  
  2.                     LPCTSTR lpszWindowName, DWORD dwStyle,  
  3.                     int x, int y, int nWidth, int nHeight,  
  4.                     HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam)  
  5.                     {  
  6.     // allow modification of several common create parameters  
  7.     CREATESTRUCT cs;  
  8.     cs.dwExStyle = dwExStyle;  
  9.     cs.lpszClass = lpszClassName;  
  10.     cs.lpszName = lpszWindowName;  
  11.     cs.style = dwStyle;  
  12.     cs.x = x;  
  13.     cs.y = y;  
  14.     cs.cx = nWidth;  
  15.     cs.cy = nHeight;  
  16.     cs.hwndParent = hWndParent;  
  17.     cs.hMenu = nIDorHMenu;  
  18.     cs.hInstance = AfxGetInstanceHandle();  
  19.     cs.lpCreateParams = lpParam;  
  20.       
  21.     if (!PreCreateWindow(cs))  
  22.         {  
  23.         PostNcDestroy();  
  24.         return FALSE;  
  25.     }  
  26.       
  27.     AfxHookWindowCreate(this);  
  28.     HWND hWnd = ::CreateWindowEx(cs.dwExStyle, cs.lpszClass,  
  29.         cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,  
  30.         cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);  
  31.       
  32.       
  33.         return TRUE;  
  34. }  
  35.   
  36. // for child windows  
  37. BOOL CWnd::PreCreateWindow(CREATESTRUCT& cs)  
  38. {  
  39.     if (cs.lpszClass == NULL)  
  40.         {  
  41.         // make sure the default window class is registered  
  42.         VERIFY(AfxDeferRegisterClass(AFX_WND_REG));  
  43.           
  44.         // no WNDCLASS provided - use child window default  
  45.         ASSERT(cs.style & WS_CHILD);  
  46.         cs.lpszClass = _afxWnd;  
  47.     }  
  48.     return TRUE;  
  49. }  
 

 

CWnd::CreateEx先設定cs(CREATESTRUCT),在調用真正的窗口建立函數::CreateWindowEx以前,調用了CWnd::PreCreateWindow函數,並把參數cs以引用的方式傳遞了進去。而CWnd的PreCreateWindow函數也只是給cs.lpszClass賦值而已。畢竟,窗口建立函數CWnd::CreateEx的諸多參數中,並無哪一個指定了所要建立窗口的窗口類,而這又是不可缺乏的(請參考<<windows程序設計>>第三章)。因此當你須要修改窗口的大小、風格、窗口所屬的窗口類等cs成員變量時,要改寫PreCreateWindow函數。

 

Cpp代碼 
  1. // From VS Install PathVC98MFCSRCWINCORE.CPP  
  2. BOOL CWnd::SubclassWindow(HWND hWnd)  
  3. {  
  4.     if (!Attach(hWnd))  
  5.         return FALSE;  
  6.       
  7.     // allow any other subclassing to occur  
  8.     PreSubclassWindow();  
  9.       
  10.     // now hook into the AFX WndProc  
  11.     WNDPROC* lplpfn = GetSuperWndProcAddr();  
  12.     WNDPROC oldWndProc = (WNDPROC)::SetWindowLong(hWnd, GWL_WNDPROC,  
  13.         (DWORD)AfxGetAfxWndProc());  
  14.     ASSERT(oldWndProc != (WNDPROC)AfxGetAfxWndProc());  
  15.       
  16.     if (*lplpfn == NULL)  
  17.         *lplpfn = oldWndProc;   // the first control of that type created  
  18. #ifdef _DEBUG  
  19.     else if (*lplpfn != oldWndProc)  
  20.         {  
  21.           
  22.             ::SetWindowLong(hWnd, GWL_WNDPROC, (DWORD)oldWndProc);  
  23.     }  
  24. #endif  
  25.       
  26.     return TRUE;  
  27. }  
  28.   
  29. void CWnd::PreSubclassWindow()  
  30. {  
  31.     // no default processing  
  32. }  
 

 

CWnd::SubclassWindow先調用函數Attach(hWnd)讓CWnd對象和hWnd所指的窗口發生關聯。接着在用::SetWindowLong修改窗口過程(subclass)前,調用了PreSubclassWindow。CWnd::PreSubclassWindow則是什麼都沒有作。

在CWnd的實現中,除了CWnd::SubclassWindow會調用PreSubclassWindow外,還有一處。上面所列函數CreateEx的代碼,其中調用了一個AfxHookWindowCreate函數,見下面代碼:

 

Cpp代碼 
  1. // From VS Install PathVC98MFCSRCWINCORE.CPP  
  2. BOOL CWnd::CreateEx()  
  3. {  
  4.     // allow modification of several common create parameters  
  5.       
  6.           
  7.         if (!PreCreateWindow(cs))  
  8.             {  
  9.             PostNcDestroy();  
  10.             return FALSE;  
  11.         }  
  12.           
  13.         AfxHookWindowCreate(this);   
  14.         HWND hWnd = ::CreateWindowEx(cs.dwExStyle, cs.lpszClass,  
  15.             cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,  
  16.             cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);  
相關文章
相關標籤/搜索