EnBitmap.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // EnBitmap.cpp: implementation of the CEnBitmap class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "EnBitmap.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. const int HIMETRIC_INCH = 2540;
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CEnBitmap::CEnBitmap()
  16. {
  17. }
  18. CEnBitmap::~CEnBitmap()
  19. {
  20. }
  21. BOOL CEnBitmap::LoadImage(UINT uIDRes, LPCTSTR szResourceType, HMODULE hInst, COLORREF crBack)
  22. {
  23. ASSERT(m_hObject == NULL); // only attach once, detach on destroy
  24. if (m_hObject != NULL)
  25. return FALSE;
  26. BYTE* pBuff = NULL;
  27. int nSize = 0;
  28. BOOL bResult = FALSE;
  29. // first call is to get buffer size
  30. if (GetResource(MAKEINTRESOURCE(uIDRes), szResourceType, hInst, 0, nSize))
  31. {
  32. if (nSize > 0)
  33. {
  34. pBuff = new BYTE[nSize];
  35. // this loads it
  36. if (GetResource(MAKEINTRESOURCE(uIDRes), szResourceType, hInst, pBuff, nSize))
  37. {
  38. IPicture* pPicture = LoadFromBuffer(pBuff, nSize);
  39. if (pPicture)
  40. {
  41. bResult = Attach(pPicture, crBack);
  42. pPicture->Release();
  43. }
  44. }
  45. delete [] pBuff;
  46. }
  47. }
  48. return bResult;
  49. }
  50. BOOL CEnBitmap::LoadImage(LPCTSTR szImagePath, COLORREF crBack)
  51. {
  52. ASSERT(m_hObject == NULL); // only attach once, detach on destroy
  53. if (m_hObject != NULL)
  54. return FALSE;
  55. BOOL bResult = FALSE;
  56. CFile cFile;
  57. CFileException e;
  58. if (cFile.Open(szImagePath, CFile::modeRead | CFile::typeBinary, &e))
  59. {
  60. int nSize = cFile.GetLength();
  61. BYTE* pBuff = new BYTE[nSize];
  62. if (cFile.Read(pBuff, nSize) > 0)
  63. {
  64. IPicture* pPicture = LoadFromBuffer(pBuff, nSize);
  65. if (pPicture)
  66. {
  67. bResult = Attach(pPicture, crBack);
  68. pPicture->Release();
  69. }
  70. }
  71. delete [] pBuff;
  72. }
  73. return bResult;
  74. }
  75. IPicture* CEnBitmap::LoadFromBuffer(BYTE* pBuff, int nSize)
  76. {
  77. bool bResult = false;
  78. HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
  79. void* pData = GlobalLock(hGlobal);
  80. memcpy(pData, pBuff, nSize);
  81. GlobalUnlock(hGlobal);
  82. IStream* pStream = NULL;
  83. IPicture* pPicture = NULL;
  84. if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
  85. {
  86. HRESULT hr = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&pPicture);
  87. pStream->Release();
  88. }
  89. return pPicture; // caller releases
  90. }
  91. BOOL CEnBitmap::GetResource(LPCTSTR lpName, LPCTSTR lpType, HMODULE hInst, void* pResource, int& nBufSize)
  92. {
  93. HRSRC hResInfo;
  94. HANDLE hRes;
  95. LPSTR lpRes = NULL;
  96. int nLen = 0;
  97. bool bResult = FALSE;
  98. // Find the resource
  99. hResInfo = FindResource(hInst, lpName, lpType);
  100. if (hResInfo == NULL)
  101. return false;
  102. // Load the resource
  103. hRes = LoadResource(hInst, hResInfo);
  104. if (hRes == NULL)
  105. return false;
  106. // Lock the resource
  107. lpRes = (char*)LockResource(hRes);
  108. if (lpRes != NULL)
  109. {
  110. if (pResource == NULL)
  111. {
  112. nBufSize = SizeofResource(hInst, hResInfo);
  113. bResult = true;
  114. }
  115. else
  116. {
  117. if (nBufSize >= (int)SizeofResource(hInst, hResInfo))
  118. {
  119. memcpy(pResource, lpRes, nBufSize);
  120. bResult = true;
  121. }
  122. }
  123. UnlockResource(hRes);
  124. }
  125. // Free the resource
  126. FreeResource(hRes);
  127. return bResult;
  128. }
  129. BOOL CEnBitmap::Attach(IPicture* pPicture, COLORREF crBack)
  130. {
  131. ASSERT(m_hObject == NULL); // only attach once, detach on destroy
  132. if (m_hObject != NULL)
  133. return FALSE;
  134. ASSERT(pPicture);
  135. if (!pPicture)
  136. return FALSE;
  137. BOOL bResult = FALSE;
  138. CDC dcMem;
  139. CDC* pDC = CWnd::GetDesktopWindow()->GetDC();
  140. if (dcMem.CreateCompatibleDC(pDC))
  141. {
  142. long hmWidth;
  143. long hmHeight;
  144. pPicture->get_Width(&hmWidth);
  145. pPicture->get_Height(&hmHeight);
  146. int nWidth = MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH);
  147. int nHeight = MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH);
  148. CBitmap bmMem;
  149. if (bmMem.CreateCompatibleBitmap(pDC, nWidth, nHeight))
  150. {
  151. CBitmap* pOldBM = dcMem.SelectObject(&bmMem);
  152. if (crBack != -1)
  153. dcMem.FillSolidRect(0, 0, nWidth, nHeight, crBack);
  154. HRESULT hr = pPicture->Render(dcMem, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, NULL);
  155. dcMem.SelectObject(pOldBM);
  156. if (hr == S_OK)
  157. bResult = CBitmap::Attach(bmMem.Detach());
  158. }
  159. }
  160. CWnd::GetDesktopWindow()->ReleaseDC(pDC);
  161. return bResult;
  162. }