ctlpict.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include "stdafx.h"
  11. #ifdef AFXCTL_CORE2_SEG
  12. #pragma code_seg(AFXCTL_CORE2_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. CPictureHolder::CPictureHolder() :
  20. m_pPict(NULL)
  21. {
  22. }
  23. CPictureHolder::~CPictureHolder()
  24. {
  25. RELEASE(m_pPict);
  26. }
  27. BOOL CPictureHolder::CreateEmpty()
  28. {
  29. RELEASE(m_pPict);
  30. PICTDESC pdesc;
  31. pdesc.cbSizeofstruct = sizeof(pdesc);
  32. pdesc.picType = PICTYPE_NONE;
  33. return SUCCEEDED(::OleCreatePictureIndirect(&pdesc, IID_IPicture, FALSE,
  34. (LPVOID*)&m_pPict));
  35. }
  36. BOOL CPictureHolder::CreateFromBitmap(UINT idResource)
  37. {
  38. CBitmap bmp;
  39. bmp.LoadBitmap(idResource);
  40. return CreateFromBitmap((HBITMAP)bmp.Detach(), NULL, TRUE);
  41. }
  42. BOOL CPictureHolder::CreateFromBitmap(CBitmap* pBitmap, CPalette* pPal,
  43. BOOL bTransferOwnership)
  44. {
  45. HBITMAP hbm = (HBITMAP)(pBitmap->GetSafeHandle());
  46. HPALETTE hpal = (HPALETTE)(pPal->GetSafeHandle());
  47. if (bTransferOwnership)
  48. {
  49. if (pBitmap != NULL)
  50. pBitmap->Detach();
  51. if (pPal != NULL)
  52. pPal->Detach();
  53. }
  54. return CreateFromBitmap(hbm, hpal, bTransferOwnership);
  55. }
  56. BOOL CPictureHolder::CreateFromBitmap(HBITMAP hbm, HPALETTE hpal,
  57. BOOL bTransferOwnership)
  58. {
  59. RELEASE(m_pPict);
  60. PICTDESC pdesc;
  61. pdesc.cbSizeofstruct = sizeof(pdesc);
  62. pdesc.picType = PICTYPE_BITMAP;
  63. pdesc.bmp.hbitmap = hbm;
  64. pdesc.bmp.hpal = hpal;
  65. return SUCCEEDED(::OleCreatePictureIndirect(&pdesc, IID_IPicture,
  66. bTransferOwnership, (LPVOID*)&m_pPict));
  67. }
  68. BOOL CPictureHolder::CreateFromMetafile(HMETAFILE hmf, int xExt,
  69. int yExt, BOOL bTransferOwnership)
  70. {
  71. RELEASE(m_pPict);
  72. PICTDESC pdesc;
  73. pdesc.cbSizeofstruct = sizeof(pdesc);
  74. pdesc.picType = PICTYPE_METAFILE;
  75. pdesc.wmf.hmeta = hmf;
  76. pdesc.wmf.xExt = xExt;
  77. pdesc.wmf.yExt = yExt;
  78. return SUCCEEDED(::OleCreatePictureIndirect(&pdesc, IID_IPicture,
  79. bTransferOwnership, (LPVOID*)&m_pPict));
  80. }
  81. BOOL CPictureHolder::CreateFromIcon(UINT idResource)
  82. {
  83. HICON hIcon = AfxGetApp()->LoadIcon(idResource);
  84. return CreateFromIcon(hIcon, TRUE);
  85. }
  86. BOOL CPictureHolder::CreateFromIcon(HICON hicon, BOOL bTransferOwnership)
  87. {
  88. RELEASE(m_pPict);
  89. PICTDESC pdesc;
  90. pdesc.cbSizeofstruct = sizeof(pdesc);
  91. pdesc.picType = PICTYPE_ICON;
  92. pdesc.icon.hicon = hicon;
  93. return SUCCEEDED(::OleCreatePictureIndirect(&pdesc, IID_IPicture,
  94. bTransferOwnership, (LPVOID*)&m_pPict));
  95. }
  96. LPPICTUREDISP CPictureHolder::GetPictureDispatch()
  97. {
  98. LPPICTUREDISP pPictDisp = NULL;
  99. if ((m_pPict != NULL) &&
  100. SUCCEEDED(m_pPict->QueryInterface(IID_IPictureDisp, (LPVOID*)&pPictDisp)))
  101. {
  102. ASSERT(pPictDisp != NULL);
  103. }
  104. return pPictDisp;
  105. }
  106. void CPictureHolder::SetPictureDispatch(LPPICTUREDISP pDisp)
  107. {
  108. LPPICTURE pPict = NULL;
  109. if (m_pPict != NULL)
  110. m_pPict->Release();
  111. if ((pDisp != NULL) &&
  112. SUCCEEDED(pDisp->QueryInterface(IID_IPicture, (LPVOID*)&pPict)))
  113. {
  114. ASSERT(pPict != NULL);
  115. m_pPict = pPict;
  116. }
  117. else
  118. {
  119. m_pPict = NULL;
  120. }
  121. }
  122. void CPictureHolder::Render(CDC* pDC, const CRect& rcRender,
  123. const CRect& rcWBounds)
  124. {
  125. if (m_pPict != NULL)
  126. {
  127. long hmWidth;
  128. long hmHeight;
  129. m_pPict->get_Width(&hmWidth);
  130. m_pPict->get_Height(&hmHeight);
  131. m_pPict->Render(pDC->m_hDC, rcRender.left, rcRender.top,
  132. rcRender.Width(), rcRender.Height(), 0, hmHeight-1,
  133. hmWidth, -hmHeight, (LPCRECT)rcWBounds);
  134. }
  135. }
  136. short CPictureHolder::GetType()
  137. {
  138. short sPicType = (short)PICTYPE_UNINITIALIZED;
  139. if (m_pPict != NULL)
  140. {
  141. m_pPict->get_Type(&sPicType);
  142. }
  143. return sPicType;
  144. }
  145. BOOL CPictureHolder::GetDisplayString(CString& strValue)
  146. {
  147. short sPicType = GetType();
  148. UINT idsType = AFX_IDS_PICTYPE_UNKNOWN;
  149. if ((sPicType >= PICTYPE_NONE) && (sPicType <= PICTYPE_ICON))
  150. idsType = AFX_IDS_PICTYPE_NONE + sPicType;
  151. CString strType;
  152. CString strFormat;
  153. strType.LoadString(idsType);
  154. strFormat.LoadString(AFX_IDS_DISPLAYSTRING_PICTURE);
  155. TCHAR szValue[_MAX_PATH];
  156. wsprintf(szValue, (LPCTSTR)strFormat, (LPCTSTR)strType);
  157. strValue = szValue;
  158. return TRUE;
  159. }
  160. /////////////////////////////////////////////////////////////////////////////
  161. // Force any extra compiler-generated code into AFX_INIT_SEG
  162. #ifdef AFX_INIT_SEG
  163. #pragma code_seg(AFX_INIT_SEG)
  164. #endif