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

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

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

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

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

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

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

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

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

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

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

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

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

CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow的区别的更多相关文章

  1. CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow

    原文链接:http://blog.chinaunix.net/uid-14607221-id-2794642.html 1. PreCreateWindow: Called by the framew ...

  2. MFC:OnCreate PreCreateWindow PreSubclassWindow

    OnCreate PreCreateWindow PreSubclassWindow PreCreateWindow和PreSubclassWindow是虚函数,而OnCreate是一个消息响应函数. ...

  3. 【转】为什么我们都理解错了HTTP中GET与POST的区别

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  4. Visual Studio 中 Build 和 Rebuild 的区别

    因为之前写的程序比较小,编译起来比较快,所以一直都没有太在意 Build 和 Rebuild 之间的区别,后来发现两个还是有很大不同. Build 只针对在上次编译之后更改过的文件进行编译,在项目比较 ...

  5. SQL Server 中 EXEC 与 SP_EXECUTESQL 的区别

    SQL Server 中 EXEC 与 SP_EXECUTESQL 的区别 MSSQL为我们提供了两种动态执行SQL语句的命令,分别是 EXEC 和 SP_EXECUTESQL ,我们先来看一下两种方 ...

  6. java集合中List与set的区别

       java集合中List与set的区别.     List可以存储元素为有序性并且元素可以相同.     set存储元素为无序性并且元素不可以相同.     下面贴几段代码感受一下: ArrayL ...

  7. Java中Set Map List 的区别

    java中set map list的区别: 都是集合接口 简要说明 set --其中的值不允许重复,无序的数据结构 list   --其中的值允许重复,因为其为有序的数据结构 map--成对的数据结构 ...

  8. oracle中函数和存储过程的区别和联系【转载竹沥半夏】

    oracle中函数和存储过程的区别和联系[转载竹沥半夏] 在oracle中,函数和存储过程是经常使用到的,他们的语法中有很多相似的地方,但也有自己的特点.刚学完函数和存储过程,下面来和大家分享一下自己 ...

  9. JS中isPrototypeOf 和hasOwnProperty 的区别 ------- js使用in和hasOwnProperty获取对象属性的区别

    JS中isPrototypeOf 和hasOwnProperty 的区别 1.isPrototypeOf isPrototypeOf是用来判断指定对象object1是否存在于另一个对象object2的 ...

随机推荐

  1. DOM操作HTML文档

    概述 之前写过一些关于DOM方法的知识,理论方法的偏多,所以,这篇博客主要是实践方面的Demo,如果,大家觉得理论方面有所欠缺,大家可以看看这几篇博客:JavaScript总结(一.基本概念)和Jav ...

  2. nginx前端负载,后端apache获取真实IP设置

    原文链接: nginx前端负载,后端apache获取真实IP设置 参考文献: 前端Nginx,后端Apache获取用户真实IP地址  按照第二种方法设置不成功! 网站最前端是nginx,做的PROXY ...

  3. Python之路Day7

    第7天主要是面向对象的内容. 学到现在越来越吃力了,从上节课开始博客就没时间写了,看看别人写的博客都那么棒.又想起了那句话比你牛逼的人都在努力,你却在放羊...唉,我写作业的效率有点低,以后得抓紧时间 ...

  4. 【集训笔记】二分图及其应用【HDOJ1068【HDOJ1150【HDOJ1151

    匈牙利算法样例程序 格式说明 输入格式: 第1行3个整数,V1,V2的节点数目n1,n2,G的边数m 第2-m+1行,每行两个整数t1,t2,代表V1中编号为t1的点和V2中编号为t2的点之间有边相连 ...

  5. 省份、城市、区县三级联动Html代码

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  6. iOS开发之计算动态cell的高度并缓存

    项目中有个类似微博那样的动态cell,文字和图片的多少都不是确定的 刚开始使用autolayout,结果很多问题,最后我发现了一个框架 FDTemplateLayoutCell 写的很好,自动布局ce ...

  7. php 登陆动作详解

    <?php class LoginAction extends Action { function index(){ $this->display(); } function do_log ...

  8. Java中的位运算符、移位运算

    一.位运算 Java中有4个位运算,它们的运算规则如下: (1)按位与 (&)  :两位全为1,结果为1,否则为0: (2)按位或  (|)   :两位有一个为1,结果为1,否则为0: (3) ...

  9. javascript每日一练(十)——运动二:缓冲运动

    一.缓冲运动 实现原理:(目标距离-当前距离) / 基数 = 速度(运动距离越大速度越小,运动距离和速度成反比) (500 - oDiv.offsetLeft) / 7 = iSpeed; 需要注意: ...

  10. Python 第三篇(下):collections系列、集合(set)、单双队列、深浅copy、内置函数

     一.collections系列: collections其实是python的标准库,也就是python的一个内置模块,因此使用之前导入一下collections模块即可,collections在py ...