CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别

http://blog.csdn.net/swimmer2000/archive/2007/10/30/1856213.aspx

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

CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    // From VS Install PathVC98MFCIncludeAFXWIN.H
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    class CWnd : public CCmdTarget
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    public:
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        virtual void PreSubclassWindow();
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        BOOL SubclassWindow(HWND hWnd);
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    };
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别

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

想知道改写函数?让我先告诉你哪个不能改写,那就是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中的这三个函数都是这样实现的吧!

CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    // From VS Install PathVC98MFCSRCWINCORE.CPP
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别 LPCTSTR lpszWindowName, DWORD dwStyle,
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别 int x, int y, int nWidth, int nHeight,
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别 HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam)
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     // allow modification of several common create parameters
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     CREATESTRUCT cs;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.dwExStyle = dwExStyle;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.lpszClass = lpszClassName;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.lpszName = lpszWindowName;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.style = dwStyle;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.x = x;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.y = y;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.cx = nWidth;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.cy = nHeight;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.hwndParent = hWndParent;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.hMenu = nIDorHMenu;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.hInstance = AfxGetInstanceHandle();
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     cs.lpCreateParams = lpParam;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     if (!PreCreateWindow(cs))
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      PostNcDestroy();
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      return FALSE;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     AfxHookWindowCreate(this);
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     HWND hWnd = ::CreateWindowEx(cs.dwExStyle, cs.lpszClass,
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别       cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别       cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     return TRUE;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    // for child windows
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    BOOL CWnd::PreCreateWindow(CREATESTRUCT& cs)
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     if (cs.lpszClass == NULL)
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      // make sure the default window class is registered
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      VERIFY(AfxDeferRegisterClass(AFX_WND_REG));
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      // no WNDCLASS provided - use child window default
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      ASSERT(cs.style & WS_CHILD);
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      cs.lpszClass = _afxWnd;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     return TRUE;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别

CWnd::CreateEx先设定cs(CREATESTRUCT),在调用真正的窗口创建函数::CreateWindowEx之前,调用了CWnd::PreCreateWindow函数,并把参数cs以引用的方式传递了进去。而CWnd的PreCreateWindow函数也只是给cs.lpszClass赋值而已。毕竟,窗口创建函数CWnd::CreateEx的诸多参数中,并没有哪个指定了所要创建窗口的窗口类,而这又是不可缺少的(请参考<<windows程序设计>>第三章)。所以当你需要修改窗口的大小、风格、窗口所属的窗口类等cs成员变量时,要改写PreCreateWindow函数。

CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    // From VS Install PathVC98MFCSRCWINCORE.CPP
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    BOOL CWnd::SubclassWindow(HWND hWnd)
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     if (!Attach(hWnd))
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      return FALSE;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     // allow any other subclassing to occur
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     PreSubclassWindow();
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     // now hook into the AFX WndProc
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     WNDPROC* lplpfn = GetSuperWndProcAddr();
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     WNDPROC oldWndProc = (WNDPROC)::SetWindowLong(hWnd, GWL_WNDPROC,
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      (DWORD)AfxGetAfxWndProc());
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     ASSERT(oldWndProc != (WNDPROC)AfxGetAfxWndProc());
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     if (*lplpfn == NULL)
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      *lplpfn = oldWndProc;   // the first control of that type created
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    #ifdef _DEBUG
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     else if (*lplpfn != oldWndProc)
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      ::SetWindowLong(hWnd, GWL_WNDPROC, (DWORD)oldWndProc);
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    #endif
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     return TRUE;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    void CWnd::PreSubclassWindow()
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     // no default processing
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别

CWnd::SubclassWindow先调用函数Attach(hWnd)让CWnd对象和hWnd所指的窗口发生关联。接着在用::SetWindowLong修改窗口过程(subclass)前,调用了PreSubclassWindow。CWnd::PreSubclassWindow则是什么都没有做。

在CWnd的实现中,除了CWnd::SubclassWindow会调用PreSubclassWindow外,还有一处。上面所列函数CreateEx的代码,其中调用了一个AfxHookWindowCreate函数,见下面代码:

CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    // From VS Install PathVC98MFCSRCWINCORE.CPP
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    BOOL CWnd::CreateEx(...)
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     // allow modification of several common create parameters
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     if (!PreCreateWindow(cs))
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      PostNcDestroy();
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      return FALSE;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     AfxHookWindowCreate(this); 
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     HWND hWnd = ::CreateWindowEx(cs.dwExStyle, cs.lpszClass,
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别       cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别       cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     return TRUE;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别

接着察看AfxHookWindowCreate的代码:

CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    // From VS Install PathVC98MFCSRCWINCORE.CPP
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    void AFXAPI AfxHookWindowCreate(CWnd* pWnd)
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     if (pThreadState->m_hHookOldCbtFilter == NULL)
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      pThreadState->m_hHookOldCbtFilter = ::SetWindowsHookEx(WH_CBT,
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别       _AfxCbtFilterHook, NULL, ::GetCurrentThreadId());
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      if (pThreadState->m_hHookOldCbtFilter == NULL)
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别       AfxThrowMemoryException();
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别     ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别

其主要作用的::SetWindowsHookEx函数用于设置一个挂钩函数(Hook函数)_AfxCbtFilterHook,每当Windows产生一个窗口时(还有许多其它类似,请参考<<深入浅出MFC>>第9章,563页),就会调用你设定的Hook函数。

这样设定完成后,回到CWnd::CreateEx函数中,执行::CreateWindowEx进行窗口创建,窗口一产生,就会调用上面设定的Hook函数_AfxCbtFilterHook。而正是在_AfxCbtFilterHook中对函数PreSubclassWindow进行了第二次调用。见如下代码:

CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    // From VS Install PathVC98MFCSRCWINCORE.CPP
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    /////////////////////////////////////////////////////////////////////////////
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    // Window creation hooks
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    LRESULT CALLBACK
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    _AfxCbtFilterHook(int code, WPARAM wParam, LPARAM lParam)
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别            ...       
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别                 ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        // connect the HWND to pWndInit...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别      pWndInit->Attach(hWnd);
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        // allow other subclassing to occur first
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        pWndInit->PreSubclassWindow();
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别                ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        {
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别                // subclass the window with standard AfxWndProc
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别                oldWndProc = (WNDPROC)SetWindowLong(hWnd, GWL_WNDPROC, (DWORD)afxWndProc);
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别            ASSERT(oldWndProc != NULL);
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别                *pOldWndProc = oldWndProc;
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别        }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别           ...
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别    }
CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别

也在调用函数SetWindowLong进行窗口subclass前调用了PreSubclassWindow.

http://blog.csdn.net/xiexievv/article/details/6233423

上一篇:Linux安装JDK8详细步骤


下一篇:Oracle JDBC驱动安装到Maven本地仓库