home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / polygon / polyctl.h < prev    next >
C/C++ Source or Header  |  1998-03-26  |  6KB  |  182 lines

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