MFC(VC6.0)的CWnd及其子類中,有如下三個函數(shù):
class CWnd : public CCmdTarget



{

public:

virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual void PreSubclassWindow();
BOOL SubclassWindow(HWND hWnd);

};
讓人很不容易區(qū)分,不知道它們究竟干了些什么,在什么情況下要改寫哪個函數(shù)?
想知道改寫函數(shù)?讓我先告訴你哪個不能改寫,那就是SubclassWindow。Scott Meyers的杰作<<Effective C++>>的第36條是這樣的Differentiate between inheritance of interface and inheritance of implementation. 看了后你馬上就知道,父類中的非虛擬函數(shù)是設(shè)計成不被子類改寫的。根據(jù)有無virtual關(guān)鍵字,我們在排除了SubclassWindow后,也就知道PreCreateWindow和PreSubClassWindow是被設(shè)計成可改寫的。接著的問題便是該在什么時候該寫了。要知道什么時候該寫,必須知道函數(shù)是在什么時候被調(diào)用,還有執(zhí)行函數(shù)的想要達到的目的。我們先看看對這三個函數(shù),MSDN給的解釋:
PreCreateWindow:
Called by the framework before the creation of the Windows window
attached to this CWnd object.
(譯:在窗口被創(chuàng)建并attach到this指針所指的CWnd對象之前,被framework調(diào)用)
PreSubclassWindow:
This member function is called by the framework to allow other necessary
subclassing to occur before the window is subclassed.
(譯:在window被subclassed之前被framework調(diào)用,用來允許其它必要的subclassing發(fā)生)
雖然我已有譯文,但還是讓我對CWnd的attach和窗口的subclass作簡單的解釋吧!要理解attach,我們必須要知道一個C++的CWnd對象和窗口(window)的區(qū)別:window就是實在的窗口,而CWnd就是MFC用類對window所進行C++封裝。attach,就是把窗口附加到CWnd對象上操作。附加(attach)完成后,CWnd對象才和窗口發(fā)生了聯(lián)系。窗口的subclass是指修改窗口過程的操作,而不是面向?qū)ο笾械呐缮宇悺?br> 好了,PreCreateWindow由framework在窗口創(chuàng)建前被調(diào)用,函數(shù)名也說明了這一點,Pre應(yīng)該是previous的縮寫,PreSubclassWindow由framework在subclass窗口前調(diào)用。 這段話說了等于沒說,你可能還是不知道,什么時候該改寫哪個函數(shù)。羅羅嗦嗦的作者,還是用代碼說話吧!源碼之前,了無秘密(候捷語)。我們就看看MFC中的這三個函數(shù)都是這樣實現(xiàn)的吧!
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);

return TRUE;
}

// for child windows
BOOL CWnd::PreCreateWindow(CREATESTRUCT& cs)



{
if (cs.lpszClass == NULL)


{
// make sure the default window class is registered
VERIFY(AfxDeferRegisterClass(AFX_WND_REG));
// no WNDCLASS provided - use child window default
ASSERT(cs.style & WS_CHILD);
cs.lpszClass = _afxWnd;
}
return TRUE;
}
CWnd::CreateEx先設(shè)定cs(CREATESTRUCT),在調(diào)用真正的窗口創(chuàng)建函數(shù)::CreateWindowEx之前,調(diào)用了CWnd::PreCreateWindow函數(shù),并把參數(shù)cs以引用的方式傳遞了進去。而CWnd的PreCreateWindow函數(shù)也只是給cs.lpszClass賦值而已。畢竟,窗口創(chuàng)建函數(shù)CWnd::CreateEx的諸多參數(shù)中,并沒有哪個指定了所要創(chuàng)建窗口的窗口類,而這又是不可缺少的(請參考<<windows程序設(shè)計>>第三章)。所以當(dāng)你需要修改窗口的大小、風(fēng)格、窗口所屬的窗口類等cs成員變量時,要改寫PreCreateWindow函數(shù)。
// From VS Install PathVC98MFCSRCWINCORE.CPP
BOOL CWnd::SubclassWindow(HWND hWnd)



{
if (!Attach(hWnd))
return FALSE;
// allow any other subclassing to occur
PreSubclassWindow();
// now hook into the AFX WndProc
WNDPROC* lplpfn = GetSuperWndProcAddr();
WNDPROC oldWndProc = (WNDPROC)::SetWindowLong(hWnd, GWL_WNDPROC,
(DWORD)AfxGetAfxWndProc());
ASSERT(oldWndProc != (WNDPROC)AfxGetAfxWndProc());
if (*lplpfn == NULL)
*lplpfn = oldWndProc; // the first control of that type created
#ifdef _DEBUG
else if (*lplpfn != oldWndProc)


{

::SetWindowLong(hWnd, GWL_WNDPROC, (DWORD)oldWndProc);
}
#endif
return TRUE;
}

void CWnd::PreSubclassWindow()



{
// no default processing
}
CWnd::SubclassWindow先調(diào)用函數(shù)Attach(hWnd)讓CWnd對象和hWnd所指的窗口發(fā)生關(guān)聯(lián)。接著在用::SetWindowLong修改窗口過程(subclass)前,調(diào)用了PreSubclassWindow。CWnd::PreSubclassWindow則是什么都沒有做。
在CWnd的實現(xiàn)中,除了CWnd::SubclassWindow會調(diào)用PreSubclassWindow外,還有一處。上面所列函數(shù)CreateEx的代碼,其中調(diào)用了一個AfxHookWindowCreate函數(shù),見下面代碼:
// From VS Install PathVC98MFCSRCWINCORE.CPP
BOOL CWnd::CreateEx(
)



{
// allow modification of several common create parameters

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);

return TRUE;
}
接著察看AfxHookWindowCreate的代碼:

// From VS Install PathVC98MFCSRCWINCORE.CPP
void AFXAPI AfxHookWindowCreate(CWnd* pWnd)



{

if (pThreadState->m_hHookOldCbtFilter == NULL)


{
pThreadState->m_hHookOldCbtFilter = ::SetWindowsHookEx(WH_CBT,
_AfxCbtFilterHook, NULL, ::GetCurrentThreadId());
if (pThreadState->m_hHookOldCbtFilter == NULL)
AfxThrowMemoryException();
}

}

其主要作用的::SetWindowsHookEx函數(shù)用于設(shè)置一個掛鉤函數(shù)(Hook函數(shù))_AfxCbtFilterHook,每當(dāng)Windows產(chǎn)生一個窗口時(還有許多其它類似,請參考<<深入淺出MFC>>第9章,563頁),就會調(diào)用你設(shè)定的Hook函數(shù)。
這樣設(shè)定完成后,回到CWnd::CreateEx函數(shù)中,執(zhí)行::CreateWindowEx進行窗口創(chuàng)建,窗口一產(chǎn)生,就會調(diào)用上面設(shè)定的Hook函數(shù)_AfxCbtFilterHook。而正是在_AfxCbtFilterHook中對函數(shù)PreSubclassWindow進行了第二次調(diào)用。見如下代碼:
// From VS Install PathVC98MFCSRCWINCORE.CPP

/**//**//**//////////////////////////////////////////////////////////////////////////////
// Window creation hooks

LRESULT CALLBACK
_AfxCbtFilterHook(int code, WPARAM wParam, LPARAM lParam)



{

// connect the HWND to pWndInit
pWndInit->Attach(hWnd);
// allow other subclassing to occur first
pWndInit->PreSubclassWindow();



{
// subclass the window with standard AfxWndProc
oldWndProc = (WNDPROC)SetWindowLong(hWnd, GWL_WNDPROC, (DWORD)afxWndProc);
ASSERT(oldWndProc != NULL);
*pOldWndProc = oldWndProc;
}

}
也在調(diào)用函數(shù)SetWindowLong進行窗口subclass前調(diào)用了PreSubclassWindow.
*******************************************************************************************
通常情況下窗口是由用戶創(chuàng)建的
CWnd::Create(..)
●在此流程中,MFC提供一個機會"PreCreateWindow()供用戶在創(chuàng)建前作點手腳
而對于對話框等,窗口是通過subclass方式交給用戶的
系統(tǒng)讀入對話框模板,建立其中各個子窗口
然后將各子窗口的 消息處理函數(shù)替換成 對應(yīng)的C++對象 的消息處理函數(shù) (Subclass:子類化,或"接管") ,然后,這個子窗口就會按類中定義的方式來動作了。
在此過程中,調(diào)用的是CWnd:SubclassWindow( HWND hWnd );
●在此流程中,MFC提供一個機會"PreSubclassWindow" 供用戶在關(guān)聯(lián)前作點手腳
具體來說,如果你定義一個窗口(如CButton派生類CMyButton),然后使用對話框數(shù)據(jù)交換將一個按鈕與自己的派生類對象關(guān)聯(lián),這時候,一些"建立前"的處理就應(yīng)該寫在"PreSubclassWindow"中。
如果你用的不是"對話框數(shù)據(jù)關(guān)聯(lián)",而是在OnInitDialg中自己創(chuàng)建m_mybtn.Create(...)
這時候,一些"建立前"的處理就應(yīng)該寫在
"PreCreateWindow"中。
這里“建立前”的處理包括像那些處理,跟PreCreateWindows()做的一些窗口初始化的工作有什么不同?
PreCreateWindows函數(shù)中沒有窗口可以用——還沒有創(chuàng)建
PreSubclassWindow函數(shù)中可以對窗口進行操作。