WIN32界面开发之四:CPaintManagerUI类构建

来源:互联网 发布:java reflection 编辑:程序博客网 时间:2024/06/06 00:48

转自:http://blog.csdn.net/harvic880925/article/details/9632613

前言:前面我们完成了对DUI雏形的构建,但大家有没有注意到一个问题,我们对消息的处理都是在用户构建的类(CStartPage)中,还有,为了在控件中可以实时刷新,所以每个控件都必须带有一个变量m_hwnd,来保存当前窗体的句柄,而且在每次发送EVENT消息时都要赋值,相当麻烦,所以,我们将这些控件都具有的一些操作和变量,全部都集合起来,封装成一个类,这个类就是CPaintManagerUI;

一、基本变量定义

我带大家想想,我们应该把什么功能集成到CPaintManagerUI中呢,也就是哪些功能是不需要用户做的呢,又有哪些功能是控件所共有的呢:
1、CStartPage中的HandleMessage中,对具体消息的处理部分,比如WM_PAITN,WM_LBUTTONDOWN……
2、控件的Notify机制,上节中,每个控件为了可以NOTIFY到当前窗体,每个控件都要保存一个当前窗体的this指针,相当烦琐
CPaintManagerUI类就是对于公共的成员变量及成员方法的集成,以减少代码量,提高代码重用性,所写的一个类;既然是将原有代码进行独立,所以,独立的同时,也必须将原代码中的变量根据CPaintManagerUI的需要传进来,有几个变量是必须需要的;
3、m_hwnd:当前窗口的句柄,SendMessage()时用到;
4、m_hInstance:当前应用程序的HINSTANCE实例,在创建m_ToolTip(提示条)时用到
5、this指针:SendNotify()时,要将侦听NOTIFY消息的窗体发送NOTIFY消息,所以要将当前窗体的THIS指针,传到CPaintManangerUI中,通过CPaintManangerUI::SetNotifier(this)设置;CPaintManangerUI在发送NOTIFY时,会将消息发送给所有的侦听窗体;
我贴出CPaintManagerUI的核心定义:
注意:在源码中定义的变量要比这里的多些,多余的那些变量主要是为了双缓冲绘图所定义的:

[cpp] view plaincopy
  1. class CPaintManagerUI  
  2. {  
  3. public:  
  4.     CPaintManagerUI();  
  5.     ~CPaintManagerUI();  
  6. public:  
  7.     void Init(HWND hWnd);//set hwnd of the window  
  8.     HWND GetHwnd(){return m_hWnd;}  
  9. //NOTIFY部分 the part of Notify  
  10.     void SendNotify(CControlUI* pControl, LPCTSTR pstrMessage, WPARAM wParam= 0, LPARAM lParam= 0);//send notify messages to all listerners;  
  11.     bool AddNotifier(INotifyUI* pControl);  
  12.     bool RemoveNotifier(INotifyUI* pControl);    
  13. //控件树部分  control-tree set part  
  14.     bool AttachDialog(CControlUI* pControl);//set the root of the control-tree  
  15.     bool InitControls(CControlUI* pControl, CControlUI* pParent = NULL);//set the current manager virable to controls  
  16. //消息处理部分   message-handler  
  17.     bool MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lRes);  
  18. private:  
  19.     CStdPtrArray m_aNotifiers;//NOTIFY消息接收的指针队列 listerners array  
  20.   
  21.     HWND m_hWnd;//主窗体的句柄   the hwnd of main window  
  22.     CControlUI* m_pRoot;//XML控件树的根结点   the root of the DLG control tree,in our app this variable always point to CDlialogUI  
  23. };  
讲解:
跟上面的分析的一样,主要存储的几个部分及其对应函数,分别是:
1、窗体句柄m_hWnd,分别对应:Init(设置)、GetHwnd(获取)
看代码:

[cpp] view plaincopy
  1. void CPaintManagerUI::Init(HWND hWnd)  
  2. {  
  3.     ASSERT(::IsWindow(hWnd));  
  4.     // Remember the window context we came from  
  5.     m_hWnd = hWnd;  
  6. }  
  7. HWND CPaintManagerUI::GetHwnd()  
  8. {  
  9.     return m_hWnd;  
  10. }  
没什么难度,就是赋值与返回值;
2、消息通知部分,虽然我们当前只有一个监听类(CStartPage),但我们不想写的太死,所以使用了链表,用到了链表,当然就有增加和删除结点的部分了,具体函数解析如下:

(1)、m_aNotifiers,要接收NOTIFY消息的窗体指针队列
(2)、AddNotifier(),增加到队列中;RemoveNotifier()从队列中删除
(3)、SendNotify(),发送消息到接收窗体的Notify()函数中。

实现代码讲解:

[cpp] view plaincopy
  1. bool CPaintManagerUI::AddNotifier(INotifyUI* pControl)  
  2. {  
  3.     ASSERT(m_aNotifiers.Find(pControl)<0);  
  4.     return m_aNotifiers.Add(pControl);  
  5. }  
  6. bool CPaintManagerUI::RemoveNotifier(INotifyUI* pControl)  
  7. {  
  8.     forint i = 0; i < m_aNotifiers.GetSize(); i++ ) {  
  9.         ifstatic_cast<INotifyUI*>(m_aNotifiers[i]) == pControl ) {  
  10.             return m_aNotifiers.Remove(i);  
  11.         }  
  12.     }  
  13.     return false;  
  14. }  
讲解:这两个函数比较简单,AddNotifyer()就是把当前要接收NOTIFY消息的用户窗体类的this指针添加到数组里;RemoveNotifier()相反,就是从数组中删除指定的窗体类的this指针;
[cpp] view plaincopy
  1. void CPaintManagerUI::SendNotify(CControlUI* pControl, LPCTSTR pstrMessage, WPARAM wParam /*= 0*/LPARAM lParam /*= 0*/)  
  2. {  
  3.     TNotifyUI Msg;  
  4.     Msg.pSender = pControl;  
  5.     Msg.sType = pstrMessage;  
  6.     Msg.wParam = 0;  
  7.     Msg.lParam = 0;  
  8.   
  9.     Msg.ptMouse =m_ptLastMousePos;  
  10.     Msg.dwTimestamp = ::GetTickCount();  
  11.   
  12.     // Send to all listeners  
  13.     forint i = 0; i < m_aNotifiers.GetSize(); i++ ) {  
  14.         static_cast<INotifyUI*>(m_aNotifiers[i])->Notify(Msg);  
  15.     }  
  16. }  
讲解:发送消息函数(SendNotify),非常重要,可以看到,我们先对MSG结构体进行封装,然后发送给监听者。最后一句m_aNotifiers[i])->Notify(Msg),实际相当于CStartPage->Notify(MSG);
3、由于我们的这个CPaintManagerUI包含控件共有属性的集成部分,所以我们必须将CPaintManagerUI与控件关联起来,而这两个函数AttachDialog()、InitControls()就是实现这些功能。
[cpp] view plaincopy
  1. bool CPaintManagerUI::AttachDialog(CControlUI* pControl)  
  2. {  
  3.     ASSERT(::IsWindow(m_hWnd));  
  4.   
  5.     // Remove the existing control-tree. We might have gotten inside this function as  
  6.     // a result of an event fired or similar, so we cannot just delete the objects and  
  7.     // pull the internal memory of the calling code. We'll delay the cleanup.  
  8.     if( m_pRoot != NULL ) {  
  9.         m_aDelayedCleanup.Add(m_pRoot);  
  10.         ::PostMessage(m_hWnd, WM_APP + 1, 0, 0L);  
  11.     }  
  12.     // Set the dialog root element  
  13.     m_pRoot = pControl;  
  14.   
  15.     return InitControls(pControl);  
  16. }  
  17.   
  18. bool CPaintManagerUI::InitControls(CControlUI* pControl, CControlUI* pParent /*= NULL*/)  
  19. {  
  20.     ASSERT(pControl);  
  21.     if( pControl == NULL ) return false;  
  22.     pControl->SetManager(this, pParent != NULL ? pParent : pControl->GetParent());  
  23.     // We're usually initializing the control after adding some more of them to the tree,  
  24.     // and thus this would be a good time to request the name-map rebuilt.  
  25.     return true;  
  26. }  
讲解:
AttachDialog(),首先判断m_pRoot是否为空,如果不为空,就发送自定义消息WM_APP+1,将其清空;然后将当前的控件树ROOT结点指针赋值为m_pRoot;
InitControls(),就是给所有控件的PaintManagerUI赋值,大家注意执行顺序,这里执行的其实是m_pRoot->SetManager(this,parent);在代码中,我们在两个地方实现了SetManager,一个在CControlUI,另一个是在CContainerUI,而我们的m_pRoot肯定是CDialogUI类型,CDialogUI派生自CContainerUI,CContainerUI派生自CControlUI,所以根据继承关系,会执行CContainerUI的SetManger;具体实现流程:
1、先声明一个变量,CPaintManagerUI* m_pManager;,放在CControlUI中,供CControlUI和CContainerUI关联;
2、CContainerUI的SetManger的具体实现:

[cpp] view plaincopy
  1. void CContainerUI::SetManager(CPaintManagerUI* pManager, CControlUI* pParent)  
  2. {  
  3.     forint it = 0; it < m_items.GetSize(); it++ ) {  
  4.         static_cast<CControlUI*>(m_items[it])->SetManager(pManager, this);  
  5.     }//先设置它所有的子结点    set all his children nodes' manager first  
  6.     //然后再设置自己   and then set his own's manager  
  7.     CControlUI::SetManager(pManager, pParent);  
  8. }  
讲解:大家可以看到,先设置所有的子结点的PaintManager,然后再设置自己;当然如果子结点仍然是CContainerUI的派生类,同样会执行这个函数;所以这样就实现了遍历设置所有控件结点;这里还用到了CControlUI的SetManager,看下具体实现:
[cpp] view plaincopy
  1. void CControlUI::SetManager(CPaintManagerUI* pManager, CControlUI* pParent)  
  2. {  
  3.     m_pManager = pManager;  
  4.     m_pParent = pParent;  
  5. }  
4、消息处理部分,这部分没什么特殊的,就是把原来CStartPage中的消息响应移到了这里来。这部分代码就不讲了,大家可以去看看,不难懂;

二、EVNET消息处理

在上面讲完了CPaintManagerUI的具体实现以后,基本上就没有什么好讲的了。最后看下如何在CButtonUI中发送Notify消息。代码如下:
[cpp] view plaincopy
  1. void CButtonUI::Event(TEventUI& event)  
  2. {  
  3.     if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK )  
  4.     {  
  5.         if( ::PtInRect(&m_RectItem, event.ptMouse)) {  
  6.             m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;  
  7.   
  8.             m_pManager->SendNotify(this,L"click");  
  9.   
  10.             Invalidate();  
  11.         }  
  12.     }  
  13.     if( event.Type == UIEVENT_MOUSEMOVE )  
  14.     {  
  15.         if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {  
  16.   
  17.             m_pManager->SendNotify(this,L"drag");  
  18.   
  19.             Invalidate();  
  20.         }  
  21.     }  
  22. }  
这里直接调用m_pManager的SendNotify函数,由于在SendNotify中实现了MSG的封装,所以这里的代码就显得格外清爽。我这里只列举了发送NOTIFY消息的两个事件,其它事件没有列举出来,看源码就好。

三、重写CStartPage::HandleMessage

由于我们将消息处理函数封装到了CPaintManagerUI中,所以我们要对CStartPage的HandleMessage重新实现,具体代码如下:

[cpp] view plaincopy
  1. LRESULT CStartPage::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)  
  2. {  
  3.     if( uMsg == WM_CREATE ) {       
  4.         m_pm.Init(m_hWnd);  
  5.         CDialogBuilder builder;  
  6.         CControlUI* pRoot = builder.Create(GetDialogResource());  
  7.         ASSERT(pRoot && "Failed to parse XML");  
  8.         m_pm.AttachDialog(pRoot);  
  9.         m_pm.AddNotifier(this);  
  10.         SendMessage(GetHWND(),WM_PAINT,NULL,NULL);  
  11.         //return 0;  
  12.     }  
  13.     LRESULT lRes = 0;  
  14.     if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;  
  15.     return CWindowWnd::HandleMessage(uMsg, wParam, lParam);  
  16. }  
讲解:不难理解,在这里用户可以对消息进行拦截,然后再进入到MessageHandler中处理,对于我们都不做处理的消息传递给系统,让系统默认处理。
比如在WM_CREATE中,首先是用CPaintManagerUI 变量m_pm初始化,初始化内容包括:窗体句柄、控件树、增加THIS指针到notifier;

四、最后

最后就是消除原来放在控件里的m_hwnd和Notify相关的函数了;这里就不一一列举了,代码里我都加了将原来的代码加上双斜线作为删除了,一看就明白,就不说了。看源码吧。

由于本文只是集成类的实现,并没有涉及功能的改变,所以也就不再贴图了。

本文由HARVIC完成,如若转载,请标明出处,请大家尊重初创者的版权,谢谢!!

源文地址:http://blog.csdn.net/harvic880925/article/details/9632613

源码地址:http://download.csdn.net/detail/harvic880925/5843901

0 0
原创粉丝点击