CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别

泄露秘密 提交于 2020-02-29 17:58:50

MFC(VC6.0)的CWnd及其子类中,有如下三个函数:

 

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. };  
 

 

让人很不容易区分,不知道它们究竟干了些什么,在什么情况下要改写哪个函数?

想知道改写函数?让我先告诉你哪个不能改写,那就是SubclassWindow。Scott Meyers的杰作<<Effective C++>>的第36条是这样的Differentiate between inheritance of interface and inheritance of implementation. 看了后你马上就知道,父类中的非虚拟函数是设计成不被子类改写的。根据有无virtual关键字,我们在排除了SubclassWindow后,也就知道PreCreateWindow和PreSubClassWindow是被设计成可改写的。接着的问题便是该在什么时候该写了。要知道什么时候该写,必须知道函数是在什么时候被调用,还有执行函数的想要达到的目的。我们先看看对这三个函数,MSDN给的解释:

PreCreateWindow:

Called by the framework before the creation of the Windows window 

attached to this CWnd object.

(译:在窗口被创建并attach到this指针所指的CWnd对象之前,被framework调用)

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发生)

虽然我已有译文,但还是让我对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);  
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!