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

Windows编程

开发平台:

Visual C++

  1. // PolyCtl.h : Declaration of the CPolyCtl
  2. #ifndef __POLYCTL_H_
  3. #define __POLYCTL_H_
  4. #include <math.h>
  5. #include "resource.h"       // main symbols
  6. #include <atlctl.h>
  7. #include "PolygonCP.h"
  8. /////////////////////////////////////////////////////////////////////////////
  9. // CPolyCtl
  10. class ATL_NO_VTABLE CPolyCtl :
  11. public CComObjectRootEx<CComSingleThreadModel>,
  12. public CStockPropImpl<CPolyCtl, IPolyCtl, &IID_IPolyCtl, &LIBID_POLYGONLib>,
  13. public CComControl<CPolyCtl>,
  14. public IPersistStreamInitImpl<CPolyCtl>,
  15. public IOleControlImpl<CPolyCtl>,
  16. public IOleObjectImpl<CPolyCtl>,
  17. public IOleInPlaceActiveObjectImpl<CPolyCtl>,
  18. public IViewObjectExImpl<CPolyCtl>,
  19. public IOleInPlaceObjectWindowlessImpl<CPolyCtl>,
  20. public ISupportErrorInfo,
  21. public IConnectionPointContainerImpl<CPolyCtl>,
  22. public IPersistStorageImpl<CPolyCtl>,
  23. public ISpecifyPropertyPagesImpl<CPolyCtl>,
  24. public IQuickActivateImpl<CPolyCtl>,
  25. public IDataObjectImpl<CPolyCtl>,
  26. public IProvideClassInfo2Impl<&CLSID_PolyCtl, &DIID__IPolyCtlEvents, &LIBID_POLYGONLib>,
  27. public IPropertyNotifySinkCP<CPolyCtl>,
  28. public CComCoClass<CPolyCtl, &CLSID_PolyCtl>,
  29. public CProxy_IPolyCtlEvents< CPolyCtl >,
  30. public IObjectSafetyImpl<CPolyCtl, INTERFACESAFE_FOR_UNTRUSTED_CALLER>
  31. {
  32. public:
  33. CPolyCtl()
  34. {
  35. m_nSides = 3;
  36. m_clrFillColor = RGB(0, 0xFF, 0);
  37. }
  38. DECLARE_REGISTRY_RESOURCEID(IDR_POLYCTL)
  39. BEGIN_COM_MAP(CPolyCtl)
  40. COM_INTERFACE_ENTRY_IMPL(IConnectionPointContainer)
  41. COM_INTERFACE_ENTRY(IPolyCtl)
  42. COM_INTERFACE_ENTRY(IDispatch)
  43. COM_INTERFACE_ENTRY(IViewObjectEx)
  44. COM_INTERFACE_ENTRY(IViewObject2)
  45. COM_INTERFACE_ENTRY(IViewObject)
  46. COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless)
  47. COM_INTERFACE_ENTRY(IOleInPlaceObject)
  48. COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless)
  49. COM_INTERFACE_ENTRY(IOleInPlaceActiveObject)
  50. COM_INTERFACE_ENTRY(IOleControl)
  51. COM_INTERFACE_ENTRY(IOleObject)
  52. COM_INTERFACE_ENTRY(IPersistStreamInit)
  53. COM_INTERFACE_ENTRY2(IPersist, IPersistStreamInit)
  54. COM_INTERFACE_ENTRY(ISupportErrorInfo)
  55. COM_INTERFACE_ENTRY(IConnectionPointContainer)
  56. COM_INTERFACE_ENTRY(ISpecifyPropertyPages)
  57. COM_INTERFACE_ENTRY(IQuickActivate)
  58. COM_INTERFACE_ENTRY(IPersistStorage)
  59. COM_INTERFACE_ENTRY(IDataObject)
  60. COM_INTERFACE_ENTRY(IProvideClassInfo)
  61. COM_INTERFACE_ENTRY(IProvideClassInfo2)
  62. COM_INTERFACE_ENTRY(IObjectSafety)
  63. END_COM_MAP()
  64. BEGIN_PROP_MAP(CPolyCtl)
  65. PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4)
  66. PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4)
  67. PROP_ENTRY("FillColor", DISPID_FILLCOLOR, CLSID_StockColorPage)
  68. PROP_ENTRY("Sides", 1, CLSID_PolyProp)
  69. // Example entries
  70. // PROP_ENTRY("Property Description", dispid, clsid)
  71. // PROP_PAGE(CLSID_StockColorPage)
  72. END_PROP_MAP()
  73. BEGIN_CONNECTION_POINT_MAP(CPolyCtl)
  74. CONNECTION_POINT_ENTRY(DIID__IPolyCtlEvents)
  75. CONNECTION_POINT_ENTRY(IID_IPropertyNotifySink)
  76. END_CONNECTION_POINT_MAP()
  77. BEGIN_MSG_MAP(CPolyCtl)
  78. MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
  79. CHAIN_MSG_MAP(CComControl<CPolyCtl>)
  80. DEFAULT_REFLECTION_HANDLER()
  81. END_MSG_MAP()
  82. // Handler prototypes:
  83. //  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  84. //  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
  85. //  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
  86. // ISupportsErrorInfo
  87. STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid)
  88. {
  89. static const IID* arr[] =
  90. {
  91. &IID_IPolyCtl,
  92. };
  93. for (int i=0; i<sizeof(arr)/sizeof(arr[0]); i++)
  94. {
  95. if (InlineIsEqualGUID(*arr[i], riid))
  96. return S_OK;
  97. }
  98. return S_FALSE;
  99. }
  100. // IViewObjectEx
  101. DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE)
  102. // IPolyCtl
  103. public:
  104. STDMETHOD(get_Sides)(/*[out, retval]*/ short *pVal);
  105. STDMETHOD(put_Sides)(/*[in]*/ short newVal);
  106. void    CalcPoints(const RECT& rc);
  107. HRESULT CPolyCtl::OnDraw(ATL_DRAWINFO& di)
  108. {
  109.    RECT& rc = *(RECT*)di.prcBounds;
  110.    HDC hdc  = di.hdcDraw;
  111.    COLORREF    colFore;
  112.    HBRUSH      hOldBrush, hBrush;
  113.    HPEN        hOldPen, hPen;
  114.    // Translate m_colFore into a COLORREF type
  115.    OleTranslateColor(m_clrFillColor, NULL, &colFore);
  116.    // Create and select the colors to draw the circle
  117.    hPen = (HPEN)GetStockObject(BLACK_PEN);
  118.    hOldPen = (HPEN)SelectObject(hdc, hPen);
  119.    hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
  120.    hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
  121.    Ellipse(hdc, rc.left, rc.top, rc.right, rc.bottom);
  122.    // Create and select the brush that will be used to fill the polygon
  123.    hBrush    = CreateSolidBrush(colFore);
  124.    SelectObject(hdc, hBrush);
  125.    CalcPoints(rc);
  126.    Polygon(hdc, &m_arrPoint[0], m_nSides);
  127.    // Select back the old pen and brush and delete the brush we created
  128.    SelectObject(hdc, hOldPen);
  129.    SelectObject(hdc, hOldBrush);
  130.    DeleteObject(hBrush);
  131.    return S_OK;
  132. }
  133. OLE_COLOR m_clrFillColor;
  134. short m_nSides;
  135. POINT m_arrPoint[100];
  136. LRESULT CPolyCtl::OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  137. {
  138. HRGN hRgn;
  139. WORD xPos = LOWORD(lParam);  // horizontal position of cursor
  140. WORD yPos = HIWORD(lParam);  // vertical position of cursor
  141. CalcPoints(m_rcPos);   // Create a region from our list of points
  142. hRgn = CreatePolygonRgn(&m_arrPoint[0], m_nSides, WINDING);
  143. // If the clicked point is in our polygon then fire the ClickIn
  144. //  event otherwise we fire the ClickOut event
  145. if (PtInRegion(hRgn, xPos, yPos))
  146. Fire_ClickIn(xPos, yPos);
  147. else
  148. Fire_ClickOut(xPos, yPos);   // Delete the region that we created
  149. DeleteObject(hRgn);
  150. return 0;
  151. }
  152. };
  153. #endif //__POLYCTL_H_