home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / ATL_Samples / polygon / polyctl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  3.9 KB  |  144 lines

  1. // PolyCtl.cpp : Implementation of CPolyCtl
  2. #include "stdafx.h"
  3. #include "Polygon.h"
  4. #include "PolyCtl.h"
  5.  
  6.  
  7. #ifdef UNDER_CE 
  8.     #define GSC_MASK 0x80000000L
  9.     #ifdef _WIN32_WCE_EMULATION
  10.         #define GSC_SETBIT 0x00000000L
  11.     #else
  12.         #define GSC_SETBIT 0x04000000L
  13.     #endif
  14.     inline HRESULT wce_OleTranslateColor(OLE_COLOR clr, HPALETTE, COLORREF *retClr)
  15.     {
  16.         *retClr = (clr & GSC_MASK) ? GetSysColor(clr & (~GSC_MASK) | GSC_SETBIT) : (COLORREF)(clr & 0x00FFFFFF);
  17.         return S_OK;
  18.     }
  19. #else
  20.     #define wce_OleTranslateColor(x,y,z) OleTranslateColor(x,y,z)
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CPolyCtl
  25.  
  26. STDMETHODIMP CPolyCtl::InterfaceSupportsErrorInfo(REFIID riid)
  27. {
  28.     static const IID* arr[] = 
  29.     {
  30.         &IID_IPolyCtl,
  31.     };
  32.     for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  33.     {
  34.         if (InlineIsEqualGUID(*arr[i],riid))
  35.             return S_OK;
  36.     }
  37.     return S_FALSE;
  38. }
  39.  
  40. HRESULT CPolyCtl::OnDraw(ATL_DRAWINFO& di)
  41. {
  42.     RECT& rc = *(RECT*)di.prcBounds;
  43.     HDC hdc  = di.hdcDraw;
  44.  
  45.     COLORREF    colFore;
  46.     HBRUSH        hOldBrush, hBrush;
  47.     HPEN        hOldPen, hPen;
  48.  
  49.     // Translate m_colFore into a COLORREF type
  50.     wce_OleTranslateColor(m_clrFillColor, NULL, &colFore);
  51.     
  52.     // Create and select the colors to draw the circle
  53.     hPen = (HPEN)GetStockObject(BLACK_PEN);
  54.     hOldPen = (HPEN)SelectObject(hdc, hPen);
  55.     hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
  56.     hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
  57.  
  58.     const double pi = 3.14159265358979;
  59.     POINT    ptCenter;
  60.     double  dblRadiusx = (rc.right - rc.left) / 2;
  61.     double  dblRadiusy = (rc.bottom - rc.top) / 2;
  62.     double    dblAngle = 3 * pi / 2;            // Start at the top
  63.     double    dblDiff  = 2 * pi / m_nSides;    // Angle each side will make
  64.     ptCenter.x = (rc.left + rc.right) / 2;
  65.     ptCenter.y = (rc.top + rc.bottom) / 2;
  66.     
  67.     // Calculate the points for each side
  68.     for (int i = 0; i < m_nSides; i++)
  69.     {
  70.         m_arrPoint[i].x = (long)(dblRadiusx * cos(dblAngle) + ptCenter.x + 0.5);
  71.         m_arrPoint[i].y = (long)(dblRadiusy * sin(dblAngle) + ptCenter.y + 0.5);
  72.         dblAngle += dblDiff;
  73.     }
  74.     Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);
  75.     Ellipse(hdc, rc.left, rc.top, rc.right, rc.bottom);
  76.  
  77.     // Create and select the brush that will be used to fill the polygon
  78.     hBrush      = CreateSolidBrush(colFore);
  79.     SelectObject(hdc, hBrush);
  80.     Polygon(hdc, &m_arrPoint[0], m_nSides);
  81.  
  82.     POINT pp[2];
  83.     dblAngle = pi + pi / 4.0;
  84.     pp[0].x = (long)(dblRadiusx * cos(dblAngle) + ptCenter.x + 0.5);
  85.     pp[0].y = (long)(dblRadiusy * sin(dblAngle) + ptCenter.y + 0.5);
  86.     pp[1].x = ptCenter.x + ptCenter.x - pp[0].x;
  87.     pp[1].y = ptCenter.y + ptCenter.y - pp[0].y;
  88.     Polyline(hdc, pp, 2);
  89.  
  90.     // Select back the old pen and brush and delete the brush we created
  91.     SelectObject(hdc, hOldPen);
  92.     SelectObject(hdc, hOldBrush);
  93.     DeleteObject(hBrush);
  94.  
  95.     return S_OK;
  96. }
  97.  
  98. LRESULT CPolyCtl::OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  99. {
  100.     WORD xPos = LOWORD(lParam);  // horizontal position of cursor 
  101.     WORD yPos = HIWORD(lParam);  // vertical position of cursor 
  102.     
  103.     // If the clicked point is in our polygon then fire the ClickIn
  104.     //  event otherwise we fire the ClickOut event
  105.     if(xPos * m_rcPos.bottom > yPos * m_rcPos.right)
  106.         ClickIn(xPos, yPos);
  107.     else
  108.         ClickOut(xPos, yPos);
  109.  
  110.     return 0;
  111. }
  112.  
  113. LRESULT CPolyCtl::OnRButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  114. {
  115.     WORD xPos = LOWORD(lParam);  // horizontal position of cursor 
  116.     WORD yPos = HIWORD(lParam);  // vertical position of cursor 
  117.  
  118.     ClickOut(xPos, yPos);
  119.  
  120.     return 0;
  121. }
  122.  
  123. STDMETHODIMP CPolyCtl::get_Sides(short *pVal)
  124. {
  125.     *pVal = m_nSides;
  126.     return S_OK;
  127. }
  128.  
  129. STDMETHODIMP CPolyCtl::put_Sides(short newVal)
  130. {
  131.     if (newVal > 2 && newVal < 101)
  132.     {
  133.         m_nSides = newVal;
  134.         FireViewChange();
  135.         return S_OK;
  136.     }
  137.     else 
  138.     {
  139.         TCHAR szBuf1[80] = _T("");
  140.         int nResult = LoadString (_Module.m_hInst, IDS_ERROR, szBuf1, sizeof(szBuf1) );
  141.         return Error(szBuf1);
  142.     }
  143. }
  144.