home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / controls / drawpic / drawpicc.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  9KB  |  342 lines

  1. // DrawPicC.cpp : Implementation of the CDrawPicCtrl OLE control class.
  2.  
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "DrawPic.h"
  15. #include "afxcmn.h"
  16. #include "DrawPicC.h"
  17. #include "DrawPicP.h"
  18.  
  19.  
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26.  
  27. IMPLEMENT_DYNCREATE(CDrawPicCtrl, COleControl)
  28.  
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // Message map
  32.  
  33. BEGIN_MESSAGE_MAP(CDrawPicCtrl, COleControl)
  34.     //{{AFX_MSG_MAP(CDrawPicCtrl)
  35.     // NOTE - ClassWizard will add and remove message map entries
  36.     //    DO NOT EDIT what you see in these blocks of generated code !
  37.     //}}AFX_MSG_MAP
  38.     ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  39. END_MESSAGE_MAP()
  40.  
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // Dispatch map
  44.  
  45. BEGIN_DISPATCH_MAP(CDrawPicCtrl, COleControl)
  46.     //{{AFX_DISPATCH_MAP(CDrawPicCtrl)
  47.     DISP_FUNCTION(CDrawPicCtrl, "AddPicture", AddPicture, VT_BOOL, VTS_PICTURE)
  48.     DISP_FUNCTION(CDrawPicCtrl, "ClearImageList", ClearImageList, VT_EMPTY, VTS_NONE)
  49.     //}}AFX_DISPATCH_MAP
  50.     DISP_FUNCTION_ID(CDrawPicCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  51. END_DISPATCH_MAP()
  52.  
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // Event map
  56.  
  57. BEGIN_EVENT_MAP(CDrawPicCtrl, COleControl)
  58.     //{{AFX_EVENT_MAP(CDrawPicCtrl)
  59.     // NOTE - ClassWizard will add and remove event map entries
  60.     //    DO NOT EDIT what you see in these blocks of generated code !
  61.     //}}AFX_EVENT_MAP
  62. END_EVENT_MAP()
  63.  
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // Property pages
  67.  
  68. // TODO: Add more property pages as needed.  Remember to increase the count!
  69. BEGIN_PROPPAGEIDS(CDrawPicCtrl, 1)
  70.     PROPPAGEID(CDrawPicPropPage::guid)
  71. END_PROPPAGEIDS(CDrawPicCtrl)
  72.  
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // Initialize class factory and guid
  76.  
  77. IMPLEMENT_OLECREATE_EX(CDrawPicCtrl, "DRAWPIC.DrawPicCtrl.1",
  78.     0x57f5a423, 0xa324, 0x11cf, 0xb4, 0xa4, 0x44, 0x45, 0x53, 0x54, 0, 0)
  79.  
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // Type library ID and version
  83.  
  84. IMPLEMENT_OLETYPELIB(CDrawPicCtrl, _tlid, _wVerMajor, _wVerMinor)
  85.  
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // Interface IDs
  89.  
  90. const IID BASED_CODE IID_DDrawPic =
  91.         { 0x57f5a421, 0xa324, 0x11cf, { 0xb4, 0xa4, 0x44, 0x45, 0x53, 0x54, 0, 0 } };
  92. const IID BASED_CODE IID_DDrawPicEvents =
  93.         { 0x57f5a422, 0xa324, 0x11cf, { 0xb4, 0xa4, 0x44, 0x45, 0x53, 0x54, 0, 0 } };
  94.  
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // Control type information
  98.  
  99. static const DWORD BASED_CODE _dwDrawPicOleMisc =
  100.     OLEMISC_ACTIVATEWHENVISIBLE |
  101.     OLEMISC_SETCLIENTSITEFIRST |
  102.     OLEMISC_INSIDEOUT |
  103.     OLEMISC_CANTLINKINSIDE |
  104.     OLEMISC_RECOMPOSEONRESIZE;
  105.  
  106. IMPLEMENT_OLECTLTYPE(CDrawPicCtrl, IDS_DRAWPIC, _dwDrawPicOleMisc)
  107.  
  108.  
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CDrawPicCtrl::CDrawPicCtrlFactory::UpdateRegistry -
  111. // Adds or removes system registry entries for CDrawPicCtrl
  112.  
  113. BOOL CDrawPicCtrl::CDrawPicCtrlFactory::UpdateRegistry(BOOL bRegister)
  114. {
  115.     if (bRegister)
  116.         return AfxOleRegisterControlClass(
  117.             AfxGetInstanceHandle(),
  118.             m_clsid,
  119.             m_lpszProgID,
  120.             IDS_DRAWPIC,
  121.             IDB_DRAWPIC,
  122.             FALSE,                      //  Not insertable
  123.             _dwDrawPicOleMisc,
  124.             _tlid,
  125.             _wVerMajor,
  126.             _wVerMinor);
  127.     else
  128.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  129. }
  130.  
  131.  
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CDrawPicCtrl::CDrawPicCtrl - Constructor
  134.  
  135. CDrawPicCtrl::CDrawPicCtrl()
  136. {
  137.     InitializeIIDs(&IID_DDrawPic, &IID_DDrawPicEvents);
  138.  
  139.     m_ImageList.m_hImageList = NULL;
  140. }
  141.  
  142.  
  143. /////////////////////////////////////////////////////////////////////////////
  144. // CDrawPicCtrl::~CDrawPicCtrl - Destructor
  145.  
  146. CDrawPicCtrl::~CDrawPicCtrl()
  147. {
  148.     // TODO: Cleanup your control's instance data here.
  149. }
  150.  
  151.  
  152. /////////////////////////////////////////////////////////////////////////////
  153. // CDrawPicCtrl::OnDraw - Drawing function
  154.  
  155. void CDrawPicCtrl::OnDraw(
  156.             CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
  157. {
  158.     // clear the background
  159.     pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
  160.  
  161.     // make sure we have an imagelist
  162.     if (NULL == m_ImageList.m_hImageList)
  163.     {
  164.         return;
  165.     }
  166.  
  167.     CPoint ptImagePt(rcBounds.left, rcBounds.top);
  168.  
  169.     // get number of images in imagelist
  170.     int nImageCount = m_ImageList.GetImageCount();
  171.  
  172.     // for each image in list
  173.     for (int nImageIndx = 0; nImageIndx < nImageCount; nImageIndx++)
  174.     {
  175.         // calc point to draw it at
  176.         ptImagePt.x = (m_szImageSize.cx * nImageIndx) + rcBounds.left;
  177.  
  178.         // render the image
  179.         m_ImageList.Draw(pdc, nImageIndx, ptImagePt, ILD_NORMAL);
  180.     }
  181. }
  182.  
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185. // CDrawPicCtrl::DoPropExchange - Persistence support
  186.  
  187. void CDrawPicCtrl::DoPropExchange(CPropExchange* pPX)
  188. {
  189.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  190.     COleControl::DoPropExchange(pPX);
  191.  
  192.     // TODO: Call PX_ functions for each persistent custom property.
  193.  
  194. }
  195.  
  196.  
  197. /////////////////////////////////////////////////////////////////////////////
  198. // CDrawPicCtrl::OnResetState - Reset control to default state
  199.  
  200. void CDrawPicCtrl::OnResetState()
  201. {
  202.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  203.  
  204.     // TODO: Reset any other control state here.
  205. }
  206.  
  207.  
  208. /////////////////////////////////////////////////////////////////////////////
  209. // CDrawPicCtrl::AboutBox - Display an "About" box to the user
  210.  
  211. void CDrawPicCtrl::AboutBox()
  212. {
  213.     CDialog dlgAbout(IDD_ABOUTBOX_DRAWPIC);
  214.     dlgAbout.DoModal();
  215. }
  216.  
  217.  
  218. /////////////////////////////////////////////////////////////////////////////
  219. // CDrawPicCtrl message handlers
  220.  
  221. BOOL CDrawPicCtrl::AddPicture(LPPICTUREDISP NewPic)
  222. {
  223.     BOOL bRetVal = TRUE;
  224.  
  225.     long lPicHeight = 0;
  226.     long lPicWidth = 0;
  227.     CDC * cdc = GetDC();
  228.     CSize sizePic;
  229.     int nRetVal;
  230.     CPictureHolder NewPicture;
  231.  
  232.     // put picture into a picture holder
  233.     NewPicture.SetPictureDispatch(NewPic);
  234.  
  235.     if (NULL == NewPicture.m_pPict)
  236.     {
  237.         return FALSE;
  238.     }
  239.  
  240.     // if picture is a bitmap
  241.     if (PICTYPE_BITMAP == NewPicture.GetType())
  242.     {
  243.         HBITMAP hBitmap = NULL;
  244.  
  245.         // get handle of the bitmap
  246.         NewPicture.m_pPict->get_Handle((OLE_HANDLE FAR *) &hBitmap);
  247.  
  248.         // get dimensions of bitmap
  249.         NewPicture.m_pPict->get_Width(&lPicWidth);
  250.         NewPicture.m_pPict->get_Height(&lPicHeight);
  251.  
  252.         sizePic.cx = (int)lPicWidth;
  253.         sizePic.cy = (int)lPicHeight;
  254.  
  255.         // convert coordinates from units to logical units
  256.         cdc->HIMETRICtoLP(&sizePic);
  257.  
  258.         // if image list has not been created
  259.         if (NULL == m_ImageList.m_hImageList)
  260.         {
  261.             m_szImageSize = sizePic;
  262.  
  263.             // create the image list
  264.             bRetVal = m_ImageList.Create(m_szImageSize.cx, m_szImageSize.cy, FALSE, 1, 1);
  265.         }
  266.         // else make sure new picture is the same size
  267.         else if (sizePic != m_szImageSize)
  268.         {
  269.             return FALSE;
  270.         }
  271.  
  272.         // create a temp bitmap
  273.         CBitmap * TempBmp = CBitmap::FromHandle(hBitmap);
  274.  
  275.         if (bRetVal)
  276.         {
  277.             // add bitmap to imagelist; mask is ignored in this sample
  278.             nRetVal = m_ImageList.Add(TempBmp, RGB(0, 0, 0) ) ;
  279.             bRetVal = (nRetVal != -1);
  280.         }
  281.  
  282.         // redraw with new picture
  283.         InvalidateControl();
  284.     }
  285.     // else if picture is an icon
  286.     else if (PICTYPE_ICON == NewPicture.GetType())
  287.     {
  288.         HICON hIcon;
  289.  
  290.         // get handle of the icon
  291.         NewPicture.m_pPict->get_Handle((OLE_HANDLE FAR *) &hIcon);
  292.  
  293.         // get dimensions of icon
  294.         NewPicture.m_pPict->get_Width(&lPicWidth);
  295.         NewPicture.m_pPict->get_Height(&lPicHeight);
  296.  
  297.         CDC * cdc = GetDC();
  298.  
  299.         sizePic.cx = (int)lPicWidth;
  300.         sizePic.cy = (int)lPicHeight;
  301.  
  302.         // convert coordinates from units to logical units
  303.         cdc->HIMETRICtoLP(&sizePic);
  304.  
  305.         // if image list has not been created
  306.         if (NULL == m_ImageList.m_hImageList)
  307.         {
  308.             m_szImageSize = sizePic;
  309.  
  310.             // create the image list
  311.             bRetVal = m_ImageList.Create(m_szImageSize.cx, m_szImageSize.cy, FALSE, 1, 1);
  312.         }
  313.         // else make sure new picture is the same size
  314.         else if (sizePic != m_szImageSize)
  315.         {
  316.             return FALSE;
  317.         }
  318.  
  319.         if (bRetVal)
  320.         {
  321.             // add icon to image list
  322.             nRetVal = m_ImageList.Add(hIcon);
  323.             bRetVal = (nRetVal != -1);
  324.         }
  325.  
  326.         InvalidateControl();
  327.     }
  328.     else
  329.     {
  330.         bRetVal = FALSE;
  331.     }
  332.  
  333.     return (bRetVal);
  334. }
  335.  
  336. void CDrawPicCtrl::ClearImageList()
  337. {
  338.     // delete all the images from the list
  339.     m_ImageList.DeleteImageList();
  340.     InvalidateControl();
  341. }
  342.