CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别
1、函数原型
virtual BOOL PreCreateWindow(
CREATESTRUCT& cs
);
virtual void PreSubclassWindow( );
BOOL SubclassWindow(
HWND hWnd
);
//在afxwin.h中的声明
// subclassing/unsubclassing functions
virtual void PreSubclassWindow();
BOOL SubclassWindow(HWND hWnd);
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
================================================================================================================
2、说明
让人很不容易区分,不知道它们究竟干了些什么,在什么情况下要改写哪个函数?想知道改写函数?让我先告诉你哪个不能改写,那就是SubclassWindow。
Scott Meyers的杰作<<Effective C++>>的第36条是这样的Differentiate between inheritance of interface and inheritance of implementation. 看了后你马上就
知道,父类中的非虚拟函数是设计成不被子类改写的。根据有无virtual关键字,我们在排除了SubclassWindow后,
也就知道PreCreateWindow和PreSubClassWindow是被设计成可改写的。接着的问题便是该在什么时候该写了。
要知道什么时候该写,必须知道函数是在什么时候被调用,还有执行函数的想要达到的目的。我们先看看对这三个函数,
MSDN给的解释:
1、PreCreateWindow:Called by the framework before the creation of the Windows window attached to this CWnd object.
(译:在窗口被创建并attach到this指针所指的CWnd对象之前,被framework调用)
2、PreSubclassWindow:This member function is called by the framework to allow other necessary subclassing to occur before the window is subclassed.
(译:在window被subclassed之前被framework调用,用来允许其它必要的subclassing发生)
3、SubClassWindow:Call this member function to "dynamically subclass" a window and attach it to thisCWnd object.
(译:调用这个函数来“动态子类”一个窗口,并将其附加到这个CWnd对象)
================================================================================================================
解释一下:CWnd的attach和窗口的subclass:
要理解attach,我们必须要知道一个C++的CWnd对象和窗口(window)的区别:
1、window就是实在的窗口,而CWnd就是MFC用类对window所进行C++封装。attach,就是把窗口附加到CWnd对象上操作。附加(attach)完成后,
CWnd对象才和窗口发生了联系。
2、窗口的subclass是指修改窗口过程的操作,而不是面向对象中的派生子类。好了,PreCreateWindow由framework在窗口创建前被调用,函数名也
说明了这一点,Pre应该是previous的缩写,PreSubclassWindow由framework在subclass窗口前调用。
我们就看看MFC中的这三个函数都是这样实现的吧!
================================================================================================================
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);
}
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);
#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
if (hWnd == NULL)
return FALSE;
ASSERT(hWnd == m_hWnd); // should have been set in send msg hook
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先设定cs(CREATESTRUCT),在调用真正的窗口创建函数::CreateWindowEx之前,调用了CWnd::PreCreateWindow函数,并把参数cs以引用的方式传递了进去。而CWnd的PreCreateWindow函数也只是给cs.lpszClass赋值而已。毕竟,窗口创建函数CWnd::CreateEx的诸多参数中,并没有哪个指定了所要创建窗口的窗口类,而这又是不可缺少的(请参考<<windows程序设计>>第三章)。所以当你需要修改窗口的大小、风格、窗口所属的窗口类等cs成员变量时,要改写PreCreateWindow函数。
================================================================================================================
// Extra CWnd support for dynamic subclassing of controls
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)
{
TRACE0("Error: Trying to use SubclassWindow with incorrect CWnd\n");
TRACE0("\tderived class.\n");
TRACE3("\thWnd = $%04X (nIDC=$%04X) is not a %hs.\n", (UINT)hWnd,
_AfxGetDlgCtrlID(hWnd), GetRuntimeClass()->m_lpszClassName);
ASSERT(FALSE);
// undo the subclassing if continuing after assert
::SetWindowLong(hWnd, GWL_WNDPROC, (DWORD)oldWndProc);
}
#endif
return TRUE;
}PreSubclassWindow:原型void CWnd::PreSubclassWindow()
{
// no default processing
}
================================================================================================================
说明:
CWnd::SubclassWindow先调用函数Attach(hWnd)让CWnd对象和hWnd所指的窗口发生关联。接着在用::SetWindowLong修改窗口过程(subclass)前,
调用了PreSubclassWindow。
CWnd::PreSubclassWindow则是什么都没有做。在CWnd的实现中,除了CWnd::SubclassWindow会调用PreSubclassWindow外,还有一处。PreSubclassWindow是在CWnd::SubclassWindow中调用的,其目的是允许程序在动态子类化控件或窗口之前做一些其它所需要的动作,
PreSubclassWindow是一个很好的定制控件的位置 上面所列函数CreateEx的代码,其中调用了一个AfxHookWindowCreate函数。
================================================================================================================
3、应用
在自定义按键时 需要重载PreSubclassWindow函数
virtual void PreSubclassWindow( );
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct );
代码
// 定位控件的位置---运行时修改控件风格
void C*****::PreSubclassWindow()
{
// change window style to allow owner draw
ModifyStyle(0, BS_OWNERDRAW | BS_PUSHBUTTON);
CButton::PreSubclassWindow();
}
void C*****::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// prepare DC
CDC* pDC = CDC::FromHandle(lpDrawItemStruct -> hDC);
CRect rect;
GetClientRect(rect);
// prepare bitmaps they need to be prepared
if (m_bNeedBitmaps)
PrepareStateBitmaps(pDC, &rect);
// draw button to the screen 绘制在主窗体上
DrawButton(pDC, &rect, lpDrawItemStruct -> itemState);
}