DRAW.H
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:8k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // Draw.h : Declaration of the CDrawCtl
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12. #include <objsafe.h>
  13. #include "resource.h"       // main symbols
  14. #include "..\drawserv\drawserv.h"
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CDrawCtl
  17. class CDrawCtl :
  18. public CComObjectRoot,
  19. public CComCoClass<CDrawCtl, &CLSID_CDrawCtl>,
  20. public CComControl<CDrawCtl>,
  21. public IPersistStreamInitImpl<CDrawCtl>,
  22. public IPersistStorageImpl<CDrawCtl>,
  23. public IQuickActivateImpl<CDrawCtl>,
  24. public IProvideClassInfo2Impl<&CLSID_CDrawCtl, NULL, &LIBID_DRAWCTLLib>,
  25. public IOleControlImpl<CDrawCtl>,
  26. public IOleObjectImpl<CDrawCtl>,
  27. public IOleInPlaceActiveObjectImpl<CDrawCtl>,
  28. public IViewObjectExImpl<CDrawCtl>,
  29. public IOleInPlaceObjectWindowlessImpl<CDrawCtl>,
  30. public IDataObjectImpl<CDrawCtl>,
  31. public ISupportErrorInfo,
  32. public IDispatchImpl<IDrawCtl, &IID_IDrawCtl, &LIBID_DRAWCTLLib>,
  33. public IDispatchImpl<IDrawServ, &IID_IDrawServ, &LIBID_DRAWCTLLib>,
  34. public IObjectSafetyImpl<CDrawCtl, INTERFACESAFE_FOR_UNTRUSTED_CALLER>
  35. {
  36. public:
  37. CDrawCtl()
  38. {
  39. m_bDragging = FALSE;
  40. m_col = RGB(255, 0, 0);
  41. m_dwSafety = 0;     // We haven't been asked to be safe yet
  42. m_pDrawServ = NULL; // We're not connected yet
  43. }
  44. ~CDrawCtl()
  45. {
  46. // Disconnect if necessary
  47. Disconnect();
  48. }
  49. DECLARE_REGISTRY_RESOURCEID(IDR_DrawCtl)
  50. DECLARE_GET_CONTROLLING_UNKNOWN()
  51. BEGIN_COM_MAP(CDrawCtl)
  52. COM_INTERFACE_ENTRY(IDrawCtl)
  53. COM_INTERFACE_ENTRY2(IDispatch,IDrawCtl)
  54. COM_INTERFACE_ENTRY(IDrawServ)
  55. COM_INTERFACE_ENTRY_IMPL(IViewObjectEx)
  56. COM_INTERFACE_ENTRY_IMPL_IID(IID_IViewObject2, IViewObjectEx)
  57. COM_INTERFACE_ENTRY_IMPL_IID(IID_IViewObject, IViewObjectEx)
  58. COM_INTERFACE_ENTRY_IMPL(IOleInPlaceObjectWindowless)
  59. COM_INTERFACE_ENTRY_IMPL_IID(IID_IOleInPlaceObject, IOleInPlaceObjectWindowless)
  60. COM_INTERFACE_ENTRY_IMPL_IID(IID_IOleWindow, IOleInPlaceObjectWindowless)
  61. COM_INTERFACE_ENTRY_IMPL(IOleInPlaceActiveObject)
  62. COM_INTERFACE_ENTRY_IMPL(IOleControl)
  63. COM_INTERFACE_ENTRY_IMPL(IOleObject)
  64. COM_INTERFACE_ENTRY_IMPL(IQuickActivate)
  65. COM_INTERFACE_ENTRY_IMPL(IPersistStorage)
  66. COM_INTERFACE_ENTRY_IMPL(IPersistStreamInit)
  67. COM_INTERFACE_ENTRY_IMPL(IDataObject)
  68. COM_INTERFACE_ENTRY(IObjectSafety)
  69. COM_INTERFACE_ENTRY(IProvideClassInfo)
  70. COM_INTERFACE_ENTRY(IProvideClassInfo2)
  71. COM_INTERFACE_ENTRY(ISupportErrorInfo)
  72. END_COM_MAP()
  73. BEGIN_MSG_MAP(CDrawCtl)
  74. MESSAGE_HANDLER(WM_PAINT, OnPaint)
  75. MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
  76. MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
  77. MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
  78. END_MSG_MAP()
  79. BEGIN_PROPERTY_MAP(CDrawCtl)
  80. END_PROPERTY_MAP()
  81. // ISupportsErrorInfo
  82. STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
  83. // IDrawCtl
  84. public:
  85. // Implementation
  86. // We currently don't repaint so do nothing
  87. HRESULT OnDraw(ATL_DRAWINFO& di)
  88. {
  89. return 0;
  90. }
  91. // Needed to resolve ambiguity with Draw method on IDrawServ interface
  92. STDMETHOD(Draw)(DWORD dwDrawAspect, LONG lindex, void  *pvAspect,
  93. DVTARGETDEVICE  *ptd, HDC hicTargetDev, HDC hdcDraw,
  94. LPCRECTL prcBounds, LPCRECTL prcWBounds,
  95. BOOL (__stdcall  *pfnContinue)(DWORD dwContinue),
  96. DWORD dwContinue)
  97. {
  98. return IViewObjectExImpl<CDrawCtl>::Draw(dwDrawAspect, lindex, pvAspect,
  99. ptd, hicTargetDev, hdcDraw,
  100. prcBounds, prcWBounds,
  101. pfnContinue,
  102. dwContinue);
  103. }
  104. // IDrawServ
  105. STDMETHOD(Draw)(long x1, long y1, long x2, long y2, unsigned long col)
  106. {
  107. HRESULT hr;
  108. HDC  hDC;
  109. HPEN hOldPen;
  110. POINT ptOld;
  111. // If we're not in place active then we don't necessarily have a window,
  112. // so we can't draw.
  113. if (!m_bInPlaceActive)
  114. return S_OK;
  115. if (m_bWndLess)
  116. {
  117. // We're windowless so we need the DC from the client
  118. hr = m_spInPlaceSite->GetDC(NULL, 0, &hDC);
  119. ATLASSERT(SUCCEEDED(hr));
  120. // We need to reset the origin if we are drawing in client coordinates
  121. ::SetWindowOrgEx(hDC, -m_rcPos.left, -m_rcPos.top, &ptOld);
  122. }
  123. else
  124. hDC  = ::GetDC(m_hWnd);
  125. HPEN hPen = ::CreatePen(PS_SOLID, 2, (COLORREF)col);
  126. hOldPen = (HPEN)::SelectObject(hDC, hPen);
  127. ::MoveToEx(hDC, x1, y1, NULL);
  128. ::LineTo(hDC, x2, y2);
  129. ::SelectObject(hDC, hOldPen);
  130. ::DeleteObject(hPen);
  131. if (m_bWndLess)
  132. {
  133. ::SetWindowOrgEx(hDC, ptOld.x, ptOld.y, NULL);
  134. hr = m_spInPlaceSite->ReleaseDC(hDC);
  135. }
  136. else
  137. ::ReleaseDC(m_hWnd, hDC);
  138. return S_OK;
  139. }
  140. // IDrawCtl
  141. STDMETHOD(Connect)(BSTR pFileName)
  142. {
  143. HRESULT hr;
  144. // Everyone can connect
  145. hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
  146. RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
  147. COSERVERINFO si;
  148. MULTI_QI     qi;
  149. si.dwReserved1 = 0;
  150. si.pwszName = OLE2W(pFileName);
  151. si.pAuthInfo = NULL;
  152. si.dwReserved2 = 0;
  153. qi.pIID = &IID_IDrawServ;
  154. qi.pItf = NULL;
  155. //qi.hr = 0;
  156. hr = CoCreateInstanceEx(CLSID_CDrawServ, NULL, CLSCTX_SERVER, &si, 1, &qi);
  157. if (FAILED(hr))
  158. {
  159. ATLTRACE(_T("CoCreateInstanceEx failed"));
  160. return hr;
  161. }
  162. if (FAILED(qi.hr))
  163. {
  164. ATLTRACE(_T("Failed to connect to server"));
  165. return qi.hr;
  166. }
  167. m_pDrawServ = (IDrawServ*)qi.pItf;
  168. if (FAILED(AtlAdvise(m_pDrawServ, GetUnknown(), IID_IDrawServ, &m_dwDrawServ)))
  169. {
  170. m_pDrawServ->Release();
  171. m_pDrawServ = NULL;
  172. hr = E_FAIL;
  173. }
  174. return hr;
  175. }
  176. // IDrawCtl
  177. STDMETHOD(Disconnect)()
  178. {
  179. if (m_pDrawServ != NULL)
  180. {
  181. AtlUnadvise(m_pDrawServ, IID_IDrawServ, m_dwDrawServ);
  182. m_pDrawServ->Release();
  183. m_pDrawServ = NULL;
  184. }
  185. return S_OK;
  186. }
  187. private:
  188. LRESULT OnLButtonDown(UINT, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  189. {
  190. if (m_bWndLess)
  191. m_spInPlaceSite->SetCapture(TRUE);
  192. else
  193. ::SetCapture(m_hWnd);
  194. m_xPos = LOWORD(lParam);  // horizontal position of cursor
  195. m_yPos = HIWORD(lParam);  // vertical position of cursor
  196. m_bDragging = TRUE;
  197. {
  198. TCHAR aMsg[80];
  199. wsprintf(aMsg,_T("%d,%dn"),m_xPos,m_yPos);
  200. ATLTRACE(aMsg);
  201. }
  202. return 0;
  203. }
  204. LRESULT OnLButtonUp(UINT, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  205. {
  206. if (m_bWndLess)
  207. m_spInPlaceSite->SetCapture(FALSE);
  208. else
  209. ::ReleaseCapture();
  210. m_bDragging = FALSE;
  211. return 0;
  212. }
  213. LRESULT OnMouseMove(UINT, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  214. {
  215. if (m_bDragging && m_pDrawServ)
  216. {
  217. HRESULT hr;
  218. RECT rcObject;
  219. POINT pt;
  220. WORD xPos = LOWORD(lParam);
  221. WORD yPos = HIWORD(lParam);
  222. pt.x = xPos;
  223. pt.y = yPos;
  224. // If we are windowless then we use the client's coordinates,
  225. // otherwise we are offset from 0,0.
  226. rcObject = m_rcPos;
  227. if (!m_bWndLess)
  228. {
  229. OffsetRect(&rcObject, -rcObject.left, -rcObject.top);
  230. }
  231. if (PtInRect(&rcObject,pt))
  232. {
  233. hr = m_pDrawServ->Draw(
  234. m_xPos - rcObject.left, m_yPos - rcObject.top,
  235. xPos - rcObject.left, yPos - rcObject.top,
  236. m_col);
  237. if (FAILED(hr))
  238. {
  239. TCHAR buf[32];
  240. // There's an error so we might as well disconnect to stop multiple errors
  241. Disconnect();
  242. wsprintf(buf, _T("Error calling server's Draw(%x)"), hr);
  243. ::MessageBox(NULL,buf,_T(""),0);
  244. }
  245. m_xPos = xPos;
  246. m_yPos = yPos;
  247. }
  248. }
  249. return 0;
  250. }
  251. IDrawServ*  m_pDrawServ;
  252. DWORD       m_dwDrawServ;   // Connection cookie
  253. WORD        m_xPos;         // Current position
  254. WORD        m_yPos;         // when drawing.
  255. BOOL        m_bDragging;    // Are we drawing? (left mouse button down)
  256. COLORREF    m_col;
  257. DWORD       m_dwSafety;     // Code safety level
  258. };