PreSubclassWindow详细分析

PreSubclassWindow
PreSubclassWindow是一个很好的定制控件的位置。如果我们通过重载CWnd::PreCreateWindow定制控件,而用户在对话框中使用控件。由于对话框中的控件窗口是通过CreateDlgIndirect创建,不经过CWnd::CreateEx函数,PreCreateWindow函数不会被调用。

其实,用户要在对话框中使用定制控件,必须用DDX或者SubclassDlgItem函数子类化控件,这时PreSubclassWindow一定会被调用。

如果用户直接创建定制控件窗口,CWnd::CreateEx函数就一定会被调用,控件窗口一定会被子类化以安装MFC消息泵。所以在MFC中,PreSubclassWindow是创建窗口的必经之路。


CWnd::PreSubclassWindow

virtual void PreSubclassWindow( );

说明:
框架调用这个成员函数以允许在窗口被子类化之前进行其它必要的子类化。重载这个函数以允许控件的动态子类化。这是个高级可重载函数。


CWnd中PreCreateWindow、PreSubclassWindow、SubclassWin MFC(VC6.0)的CWnd及其子类中,有如下三个函数: 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中的这三个函数都是这样实现的吧! 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. 
通常情况下窗口是由用户创建的 CWnd::Create(..) 
     ●在此流程中,MFC提供一个机会"PreCreateWindow()供用户在创建前作点手脚 
     而对于对话框等,窗口是通过subclass方式交给用户的 
     系统读入对话框模板,建立其中各个子窗口 
     然后将各子窗口的 消息处理函数替换成 对应的C++对象 的消息处理函数 (Subclass:子类化,或"接管") ,然后,这个子窗口就会按类中定义的方式来动作了。 
     在此过程中,调用的是CWnd:SubclassWindow( HWND hWnd ); 
     ●在此流程中,MFC提供一个机会"PreSubclassWindow" 供用户在关联前作点手脚 
     具体来说,如果你定义一个窗口(如CButton派生类CMyButton),然后使用对话框数据交换将一个按钮与自己的派生类对象关联,这时候,一些"建立前"的处理就应该写在"PreSubclassWindow"中。 
     如果你用的不是"对话框数据关联",而是在OnInitDialg中自己创建m_mybtn.Create(...) 
     这时候,一些"建立前"的处理就应该写在   "PreCreateWindow"中。


转自百度贴吧Lizhongjun1984。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第一步:新建一个基于对话框的MFC工程,拖动一棵标准树型控件放于界面之上。添加一个新类CTreeCtrlBT派生自CTreeCtrl。 第二步:为该类添加一些消息响应以及虚函数。其详细作用如下: void PreSubclassWindow(); //用于添加初始化控件的相关信息 WM_SIZE //控件区域发生变化时候的处理 WM_PAINT //绘制控件 WM_MOUSEMOVE //主要获取鼠标所在位置的单元项 第三步:TreeCtrlBT.h中首先声明一个结构用于保存单元项的相关信息,并把所有项的信息放入一个map。实现代码如下: typedef struct tagItemMsg { HTREEITEM hItem; //项的句柄 CBitmap ItemBitmap; //项的位图 COLORREF crTransparent; //透明颜色 tagItemMsg() { hItem = NULL; crTransparent = RGB(255,255,255); } ~tagItemMsg() { if ( ItemBitmap.GetSafeHandle() ) { ItemBitmap.DeleteObject(); } } }ITEM_MSG; typedef map ITEMMSG_MAP; 第四步:计算控件的客户区域大小,以及滚动条滚动的偏移量。详细过程参考源代码 Calculate函数。 第五步:添加接口函数以及成员变量。如下: (注意:TransparentBlt函数,需要包含msimg32.dll) //设置背景位图BOOL SetBackgroudBitmap( UINT nID );BOOL SetBackgroudBitmap(LPCTSTR lpszRecourceName);//设置某一项位图void SetItemBitmap(HTREEITEM hItem, UINT nIdBitmap);void SetItemBitmap(HTREEITEM hItem, LPCTSTR lpszBitmap);//设置展开收缩的图表void SetExpandBitmap(UINT nIdExpand, UINT nIdCollapse, COLORREF crTransparent = RGB(255,255,255));//插入新的项目HTREEITEM InsertItemEx(HTREEITEM hParent, LPCTSTR lpszItem, UINT nIdBitmap = 0, OLORREF crTransparent = RGB(255,255,255));HTREEITEM InsertItemEx(HTREEITEM hParent, LPCTSTR lpszItem, LPCTSTR lpszBitmap, COLORREF crTransparent = RGB(255,255,255));//开启横线void EnableRowLine( BOOL bEnable = TRUE );//是否开启横线BOOL IsEnableRowLine();//开启根节点背景void EnableRootBk( BOOL bEnable = TRUE );//是否开启根节点背景BOOL IsEnableRootBk();//获取某一项信息ITEM_MSG* GetItemMsg(HTREEITEM hItem); 第六步:绘制控件。需要的绘制函数如下: //绘制渐变色矩形区域void GradientFillRect( CDC *pDC, CRect &rect, COLORREF col_from, COLORREF col_to, bool vert_grad ); //绘制控件背景void DrawBackgroudBitmap(CDC* pDc); //绘制单元项void DrawItem( CDC* pDC ); //绘制单元项下部横线void DrawRowLine(CPoint ptBegin, CPoint ptEnd, CDC *pDc = NULL); //绘制展开收缩按钮void DrawExpand(CRect rect, int state,

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值