home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / ctlpict.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  5KB  |  202 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFXCTL_CORE2_SEG
  14. #pragma code_seg(AFXCTL_CORE2_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. CPictureHolder::CPictureHolder() :
  25.     m_pPict(NULL)
  26. {
  27. }
  28.  
  29. CPictureHolder::~CPictureHolder()
  30. {
  31.     RELEASE(m_pPict);
  32. }
  33.  
  34. BOOL CPictureHolder::CreateEmpty()
  35. {
  36.     RELEASE(m_pPict);
  37.     PICTDESC pdesc;
  38.     pdesc.cbSizeofstruct = sizeof(pdesc);
  39.     pdesc.picType = PICTYPE_NONE;
  40.     return SUCCEEDED(::OleCreatePictureIndirect(&pdesc, IID_IPicture, FALSE,
  41.         (LPVOID*)&m_pPict));
  42. }
  43.  
  44. BOOL CPictureHolder::CreateFromBitmap(UINT idResource)
  45. {
  46.     CBitmap bmp;
  47.     bmp.LoadBitmap(idResource);
  48.     return CreateFromBitmap((HBITMAP)bmp.Detach(), NULL, TRUE);
  49. }
  50.  
  51. BOOL CPictureHolder::CreateFromBitmap(CBitmap* pBitmap, CPalette* pPal,
  52.     BOOL bTransferOwnership)
  53. {
  54.     HBITMAP hbm = (HBITMAP)(pBitmap->GetSafeHandle());
  55.     HPALETTE hpal = (HPALETTE)(pPal->GetSafeHandle());
  56.  
  57.     if (bTransferOwnership)
  58.     {
  59.         if (pBitmap != NULL)
  60.             pBitmap->Detach();
  61.  
  62.         if (pPal != NULL)
  63.             pPal->Detach();
  64.     }
  65.  
  66.     return CreateFromBitmap(hbm, hpal, bTransferOwnership);
  67. }
  68.  
  69. BOOL CPictureHolder::CreateFromBitmap(HBITMAP hbm, HPALETTE hpal,
  70.     BOOL bTransferOwnership)
  71. {
  72.     RELEASE(m_pPict);
  73.     PICTDESC pdesc;
  74.     pdesc.cbSizeofstruct = sizeof(pdesc);
  75.     pdesc.picType = PICTYPE_BITMAP;
  76.     pdesc.bmp.hbitmap = hbm;
  77.     pdesc.bmp.hpal = hpal;
  78.     return SUCCEEDED(::OleCreatePictureIndirect(&pdesc, IID_IPicture,
  79.         bTransferOwnership, (LPVOID*)&m_pPict));
  80. }
  81.  
  82. BOOL CPictureHolder::CreateFromMetafile(HMETAFILE hmf, int xExt,
  83.     int yExt, BOOL bTransferOwnership)
  84. {
  85.     RELEASE(m_pPict);
  86.     PICTDESC pdesc;
  87.     pdesc.cbSizeofstruct = sizeof(pdesc);
  88.     pdesc.picType = PICTYPE_METAFILE;
  89.     pdesc.wmf.hmeta = hmf;
  90.     pdesc.wmf.xExt = xExt;
  91.     pdesc.wmf.yExt = yExt;
  92.     return SUCCEEDED(::OleCreatePictureIndirect(&pdesc, IID_IPicture,
  93.         bTransferOwnership, (LPVOID*)&m_pPict));
  94. }
  95.  
  96. BOOL CPictureHolder::CreateFromIcon(UINT idResource)
  97. {
  98.     HICON hIcon = AfxGetApp()->LoadIcon(idResource);
  99.     return CreateFromIcon(hIcon, TRUE);
  100. }
  101.  
  102. BOOL CPictureHolder::CreateFromIcon(HICON hicon, BOOL bTransferOwnership)
  103. {
  104.     RELEASE(m_pPict);
  105.     PICTDESC pdesc;
  106.     pdesc.cbSizeofstruct = sizeof(pdesc);
  107.     pdesc.picType = PICTYPE_ICON;
  108.     pdesc.icon.hicon = hicon;
  109.     return SUCCEEDED(::OleCreatePictureIndirect(&pdesc, IID_IPicture,
  110.         bTransferOwnership, (LPVOID*)&m_pPict));
  111. }
  112.  
  113. LPPICTUREDISP CPictureHolder::GetPictureDispatch()
  114. {
  115.     LPPICTUREDISP pPictDisp = NULL;
  116.  
  117.     if ((m_pPict != NULL) &&
  118.         SUCCEEDED(m_pPict->QueryInterface(IID_IPictureDisp, (LPVOID*)&pPictDisp)))
  119.     {
  120.         ASSERT(pPictDisp != NULL);
  121.     }
  122.  
  123.     return pPictDisp;
  124. }
  125.  
  126. void CPictureHolder::SetPictureDispatch(LPPICTUREDISP pDisp)
  127. {
  128.     LPPICTURE pPict = NULL;
  129.  
  130.     if (m_pPict != NULL)
  131.         m_pPict->Release();
  132.  
  133.     if ((pDisp != NULL) &&
  134.         SUCCEEDED(pDisp->QueryInterface(IID_IPicture, (LPVOID*)&pPict)))
  135.     {
  136.         ASSERT(pPict != NULL);
  137.  
  138.         m_pPict = pPict;
  139.     }
  140.     else
  141.     {
  142.         m_pPict = NULL;
  143.     }
  144. }
  145.  
  146. void CPictureHolder::Render(CDC* pDC, const CRect& rcRender,
  147.     const CRect& rcWBounds)
  148. {
  149.     if (m_pPict != NULL)
  150.     {
  151.         long hmWidth;
  152.         long hmHeight;
  153.  
  154.         m_pPict->get_Width(&hmWidth);
  155.         m_pPict->get_Height(&hmHeight);
  156.  
  157.         m_pPict->Render(pDC->m_hDC, rcRender.left, rcRender.top,
  158.             rcRender.Width(), rcRender.Height(), 0, hmHeight-1,
  159.             hmWidth, -hmHeight, (LPCRECT)rcWBounds);
  160.     }
  161. }
  162.  
  163. short CPictureHolder::GetType()
  164. {
  165.     short sPicType = (short)PICTYPE_UNINITIALIZED;
  166.  
  167.     if (m_pPict != NULL)
  168.     {
  169.         m_pPict->get_Type(&sPicType);
  170.     }
  171.  
  172.     return sPicType;
  173. }
  174.  
  175. BOOL CPictureHolder::GetDisplayString(CString& strValue)
  176. {
  177.     short sPicType = GetType();
  178.  
  179.     UINT idsType = AFX_IDS_PICTYPE_UNKNOWN;
  180.  
  181.     if ((sPicType >= PICTYPE_NONE) && (sPicType <= PICTYPE_ICON))
  182.         idsType = AFX_IDS_PICTYPE_NONE + sPicType;
  183.  
  184.     CString strType;
  185.     CString strFormat;
  186.     strType.LoadString(idsType);
  187.     strFormat.LoadString(AFX_IDS_DISPLAYSTRING_PICTURE);
  188.  
  189.     TCHAR szValue[_MAX_PATH];
  190.     wsprintf(szValue, (LPCTSTR)strFormat, (LPCTSTR)strType);
  191.  
  192.     strValue = szValue;
  193.     return TRUE;
  194. }
  195.  
  196. /////////////////////////////////////////////////////////////////////////////
  197. // Force any extra compiler-generated code into AFX_INIT_SEG
  198.  
  199. #ifdef AFX_INIT_SEG
  200. #pragma code_seg(AFX_INIT_SEG)
  201. #endif
  202.