home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap05 / polyline / polyline.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  4KB  |  215 lines

  1. /*
  2.  * POLYLINE.CPP
  3.  * Polyline Component Chapter 5
  4.  *
  5.  * Implementation of the CPolyline class that we expose as a
  6.  * component object.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "polyline.h"
  17.  
  18.  
  19. /*
  20.  * CPolyline:CPolyline
  21.  * CPolyline::~CPolyline
  22.  *
  23.  * Constructor Parameters:
  24.  *  pUnkOuter       LPUNKNOWN of the controlling unknown.
  25.  *  pfnDestroy      PFNDESTROYED to call when an object is
  26.  *                  destroyed.
  27.  *  hInst           HINSTANCE of the application we're in.
  28.  */
  29.  
  30. CPolyline::CPolyline(LPUNKNOWN pUnkOuter, PFNDESTROYED pfnDestroy
  31.     , HINSTANCE hInst)
  32.     {
  33.     m_hWnd=NULL;
  34.     m_hInst=hInst;
  35.  
  36.     m_cRef=0;
  37.     m_pUnkOuter=pUnkOuter;
  38.     m_pfnDestroy=pfnDestroy;
  39.     m_fDirty=FALSE;
  40.  
  41.     m_pImpIPolyline=NULL;
  42.     m_pImpIConnPtCont=NULL;
  43.  
  44.     m_pAdv=NULL;
  45.     m_pConnPt=NULL;
  46.  
  47.     return;
  48.     }
  49.  
  50.  
  51. CPolyline::~CPolyline(void)
  52.     {
  53.     DeleteInterfaceImp(m_pImpIConnPtCont);
  54.     DeleteInterfaceImp(m_pImpIPolyline);
  55.  
  56.     ReleaseInterface(m_pAdv);
  57.     ReleaseInterface(m_pConnPt);
  58.  
  59.     return;
  60.     }
  61.  
  62.  
  63.  
  64.  
  65. /*
  66.  * CPolyline::Init
  67.  *
  68.  * Purpose:
  69.  *  Performs any intiailization of a CPolyline that's prone to
  70.  *  failure that we also use internally before exposing the
  71.  *  object outside this DLL.
  72.  *
  73.  * Parameters:
  74.  *  None
  75.  *
  76.  * Return Value:
  77.  *  BOOL            TRUE if the function is successful,
  78.  *                  FALSE otherwise.
  79.  */
  80.  
  81. BOOL CPolyline::Init(void)
  82.     {
  83.     LPUNKNOWN       pIUnknown=this;
  84.  
  85.     if (NULL!=m_pUnkOuter)
  86.         pIUnknown=m_pUnkOuter;
  87.  
  88.     //Allocate contained interfaces.
  89.     m_pImpIPolyline=new CImpIPolyline(this, pIUnknown);
  90.  
  91.     if (NULL==m_pImpIPolyline)
  92.         return FALSE;
  93.  
  94.     m_pImpIConnPtCont=new CImpIConnPtCont(this, pIUnknown);
  95.  
  96.     if (NULL==m_pImpIConnPtCont)
  97.         return FALSE;
  98.  
  99.     m_pConnPt=new CConnectionPoint(this);
  100.  
  101.     if (NULL==m_pConnPt)
  102.         return FALSE;
  103.  
  104.     m_pConnPt->AddRef();    //Reversed in destructor
  105.     return TRUE;
  106.     }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. /*
  115.  * CPolyline::QueryInterface
  116.  * CPolyline::AddRef
  117.  * CPolyline::Release
  118.  */
  119.  
  120. STDMETHODIMP CPolyline::QueryInterface(REFIID riid, PPVOID ppv)
  121.     {
  122.     *ppv=NULL;
  123.  
  124.     if (IID_IUnknown==riid)
  125.         *ppv=this;
  126.  
  127.     if (IID_IConnectionPointContainer==riid)
  128.         *ppv=m_pImpIConnPtCont;
  129.  
  130.     if (IID_IPolyline5==riid)
  131.         *ppv=m_pImpIPolyline;
  132.  
  133.     if (NULL!=*ppv)
  134.         {
  135.         ((LPUNKNOWN)*ppv)->AddRef();
  136.         return NOERROR;
  137.         }
  138.  
  139.     return ResultFromScode(E_NOINTERFACE);
  140.     }
  141.  
  142.  
  143. STDMETHODIMP_(ULONG) CPolyline::AddRef(void)
  144.     {
  145.     return ++m_cRef;
  146.     }
  147.  
  148.  
  149. STDMETHODIMP_(ULONG) CPolyline::Release(void)
  150.     {
  151.     if (0L!=--m_cRef)
  152.         return m_cRef;
  153.  
  154.     if (NULL!=m_pfnDestroy)
  155.         (*m_pfnDestroy)();
  156.  
  157.     delete this;
  158.     return 0L;
  159.     }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. /*
  168.  * CPolyline::RectConvertMappings
  169.  *
  170.  * Purpose:
  171.  *  Converts the contents of a rectangle from device (MM_TEXT) or
  172.  *  HIMETRIC to the other.
  173.  *
  174.  * Parameters:
  175.  *  pRect           LPRECT containing the rectangle to convert.
  176.  *  fToDevice       BOOL TRUE to convert from HIMETRIC to device,
  177.  *                  FALSE to convert device to HIMETRIC.
  178.  *
  179.  * Return Value:
  180.  *  None
  181.  */
  182.  
  183. void CPolyline::RectConvertMappings(LPRECT pRect, BOOL fToDevice)
  184.     {
  185.     HDC      hDC;
  186.     int      iLpx, iLpy;
  187.  
  188.     if (NULL==pRect)
  189.         return;
  190.  
  191.     hDC=GetDC(NULL);
  192.     iLpx=GetDeviceCaps(hDC, LOGPIXELSX);
  193.     iLpy=GetDeviceCaps(hDC, LOGPIXELSY);
  194.     ReleaseDC(NULL, hDC);
  195.  
  196.     if (fToDevice)
  197.         {
  198.         pRect->left=MulDiv(iLpx, pRect->left, HIMETRIC_PER_INCH);
  199.         pRect->top =MulDiv(iLpy, pRect->top , HIMETRIC_PER_INCH);
  200.  
  201.         pRect->right =MulDiv(iLpx, pRect->right, HIMETRIC_PER_INCH);
  202.         pRect->bottom=MulDiv(iLpy, pRect->bottom,HIMETRIC_PER_INCH);
  203.         }
  204.     else
  205.         {
  206.         pRect->left=MulDiv(pRect->left, HIMETRIC_PER_INCH, iLpx);
  207.         pRect->top =MulDiv(pRect->top , HIMETRIC_PER_INCH, iLpy);
  208.  
  209.         pRect->right =MulDiv(pRect->right, HIMETRIC_PER_INCH, iLpx);
  210.         pRect->bottom=MulDiv(pRect->bottom,HIMETRIC_PER_INCH, iLpy);
  211.         }
  212.  
  213.     return;
  214.     }
  215.