用于实现拖入操作的通用类

ContractedBlock.gif ExpandedBlockStart.gif Code
  1 #pragma once
  2 // COleDropTargetEx command target
  3 
  4 /************************************************************************/
  5 /* ClassName    : COleDropTargetEx
  6 /* Author        : yangguichun
  7 /* Date            : 2009/02/01
  8 /* Description    : 这是对COleDropTarget的扩展,因为COleDropTarget只支持CView
  9                   这个类将支持所有类型的窗体类
 10 /************************************************************************/
 11 template<typename CTargetCtrl>
 12 class COleDropTargetEx : public COleDropTarget 
 13 
 14 public
 15     COleDropTargetEx(){}; 
 16     virtual ~COleDropTargetEx(){}; 
 17 
 18 protected
 19     //处理OnDropEx消息是必须的,否则OnDrop函数不会被执行 
 20     //当然也可以在OnDropEx函数中处理数据 
 21     virtual DROPEFFECT OnDragEnter(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point); 
 22     virtual DROPEFFECT OnDragOver(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);    
 23     virtual DROPEFFECT OnDropEx(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, DROPEFFECT dropEffectList, CPoint point); 
 24     virtual BOOL OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point); 
 25     virtual void OnDragLeave(CWnd* pWnd); 
 26 }; 
 27 
 28 template<typename CTargetCtrl>
 29 DROPEFFECT COleDropTargetEx<CTargetCtrl>::OnDragEnter(CWnd* pWnd, COleDataObject* pDataObject,DWORD dwKeyState, CPoint point)   
 30 {   
 31     ASSERT_VALID(this);   
 32     ASSERT(::IsWindow(pWnd->m_hWnd));   
 33 
 34     if (pWnd->IsKindOf(RUNTIME_CLASS(CView)))//if CView,Call base class    
 35         return COleDropTarget::OnDragEnter(pWnd,pDataObject, dwKeyState, point);       
 36 
 37 
 38     CTargetCtrl* pTargetCtrl = NULL;
 39     if(pWnd->IsKindOf(RUNTIME_CLASS(CTargetCtrl)))
 40     {
 41         pTargetCtrl = (CTargetCtrl*)pWnd;        
 42         return pTargetCtrl->OnDragEnter(pWnd, pDataObject, dwKeyState, point);
 43     }
 44     else
 45     {
 46         return COleDropTarget::OnDragEnter(pWnd, pDataObject, dwKeyState, point);
 47     }
 48 }   
 49 
 50 template<typename CTargetCtrl>
 51 DROPEFFECT COleDropTargetEx<CTargetCtrl>::OnDragOver(CWnd* pWnd, COleDataObject* pDataObject,   
 52                                         DWORD dwKeyState, CPoint point)   
 53 {   
 54     ASSERT_VALID(this);   
 55     ASSERT(::IsWindow(pWnd->m_hWnd));   
 56 
 57     if (pWnd->IsKindOf(RUNTIME_CLASS(CView)))//if CView,Call base class    
 58         return COleDropTarget::OnDragOver(pWnd,pDataObject, dwKeyState, point);   
 59 
 60     CTargetCtrl* pTargetCtrl = NULL;
 61     if(pWnd->IsKindOf(RUNTIME_CLASS(CTargetCtrl)))
 62     {
 63         pTargetCtrl = (CTargetCtrl*)pWnd;        
 64         return pTargetCtrl->OnDragOver(pWnd, pDataObject, dwKeyState, point);
 65     }
 66     else
 67     {
 68         return COleDropTarget::OnDragOver(pWnd, pDataObject, dwKeyState, point);
 69     }
 70 }   
 71 
 72 template<typename CTargetCtrl>
 73 BOOL COleDropTargetEx<CTargetCtrl>::OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)
 74 {   
 75     ASSERT_VALID(this);   
 76     ASSERT(::IsWindow(pWnd->m_hWnd));   
 77 
 78     if (pWnd->IsKindOf(RUNTIME_CLASS(CView)))//if CView,Call base class    
 79         return COleDropTarget::OnDrop(pWnd, pDataObject,dropEffect,point);   
 80 
 81     CTargetCtrl* pTargetCtrl = NULL;
 82     if(pWnd->IsKindOf(RUNTIME_CLASS(CTargetCtrl)))
 83     {
 84         pTargetCtrl = (CTargetCtrl*)pWnd;        
 85         return pTargetCtrl->OnDrop(pWnd, pDataObject, dropEffect, point);
 86     }
 87     else
 88     {
 89         return COleDropTarget::OnDrop(pWnd, pDataObject, dropEffect, point);
 90     }
 91 }   
 92 
 93 template<typename CTargetCtrl>
 94 DROPEFFECT COleDropTargetEx<CTargetCtrl>::OnDropEx(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, DROPEFFECT dropEffectList, CPoint point)   
 95 {   
 96     ASSERT_VALID(this);   
 97     ASSERT(::IsWindow(pWnd->m_hWnd));   
 98 
 99     if (pWnd->IsKindOf(RUNTIME_CLASS(CView)))//if CView,Call base class    
100         return COleDropTarget::OnDropEx( pWnd, pDataObject, dropEffect, dropEffectList, point) ;   
101 
102     CTargetCtrl* pTargetCtrl = NULL;
103     if(pWnd->IsKindOf(RUNTIME_CLASS(CTargetCtrl)))
104     {
105         pTargetCtrl = (CTargetCtrl*)pWnd;        
106         return pTargetCtrl->OnDropEx( pWnd, pDataObject, dropEffect, dropEffectList, point);
107     }
108     else
109     {
110         return COleDropTarget::OnDropEx( pWnd, pDataObject, dropEffect, dropEffectList, point);
111     }
112 }   
113 
114 template<typename CTargetCtrl>
115 void COleDropTargetEx<CTargetCtrl>::OnDragLeave(CWnd* pWnd)   
116 {   
117     ASSERT_VALID(this);   
118     ASSERT(::IsWindow(pWnd->m_hWnd));   
119 
120     if (pWnd->IsKindOf(RUNTIME_CLASS(CView)))//if CView,Call base class    
121     {   
122         COleDropTarget::OnDragLeave( pWnd );   
123         return;   
124     }   
125 
126     CTargetCtrl* pTargetCtrl = NULL;
127     if(pWnd->IsKindOf(RUNTIME_CLASS(CTargetCtrl)))
128     {
129         pTargetCtrl = (CTargetCtrl*)pWnd;        
130         return pTargetCtrl->OnDragLeave( pWnd );   
131     }
132     else
133     {
134         return COleDropTarget::OnDragLeave( pWnd );
135     }
136 }   
137 
138 
139 /************************************************************************/
140 /* ClassName    : CDropPolicy
141 /* Author        : yangguichun
142 /* Date            : 2009/02/01
143 /* Description    : 凡是需要支持拖入操作的控件,只需继承该类,并将自身作为
144                   模板参数传进来即可。
145                   然后,需要重载OnDrop函数,在该函数的实现中编写需要实现的拖放动作                  
146 /************************************************************************/
147 template<typename CTargetCtrl>
148 class CDropPolicy
149 {
150 public:
151     CDropPolicy(){};
152     virtual ~CDropPolicy(){};
153 
154 public:
155     virtual BOOL Register()
156     {
157         return m_oleDropTarget.Register((CTargetCtrl*)this);
158     }
159 
160     virtual DROPEFFECT OnDragEnter(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
161     {
162         return (DROPEFFECT)-1;   
163     }
164     virtual DROPEFFECT OnDragOver(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
165     {
166         return (DROPEFFECT)-1;   
167     }
168     virtual DROPEFFECT OnDropEx(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, DROPEFFECT dropList, CPoint point)
169     {
170         return (DROPEFFECT)-1;   
171     }
172     virtual BOOL OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)=0;
173     virtual void OnDragLeave(CWnd* pWnd){};
174 
175 private:
176     COleDropTargetEx<CTargetCtrl> m_oleDropTarget;
177 };

你可能感兴趣的:(用于实现拖入操作的通用类)