Picture.cpp
上传用户:dengkfang
上传日期:2008-12-30
资源大小:5233k
文件大小:6k
源码类别:

CA认证

开发平台:

Visual C++

  1. // Picture.cpp: implementation of the CPicture class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Picture.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. CPicture::CPicture()
  15. {
  16. m_pPicture = NULL;
  17. }
  18. CPicture::~CPicture()
  19. {
  20. UnLoad();
  21. }
  22. bool CPicture::Load(CString sResourceType, CString sResource)
  23. {
  24. bool bResult = false;
  25. if (m_pPicture != NULL)
  26. UnLoad();
  27. if (m_pPicture == NULL)
  28. {
  29. BYTE* pBuff = NULL;
  30. int nSize = 0;
  31. if (GetResource(sResource.GetBuffer(0), sResourceType.GetBuffer(0), pBuff, nSize))
  32. {
  33. if (nSize > 0)
  34. {
  35. pBuff = new BYTE[nSize];
  36. if (GetResource(sResource.GetBuffer(0), sResourceType.GetBuffer(0), pBuff, nSize))
  37. {
  38. if (LoadFromBuffer(pBuff, nSize))
  39. bResult = true;
  40. }
  41. delete [] pBuff;
  42. }
  43. }
  44. }
  45. return bResult;
  46. }
  47. bool CPicture::Load(CString sFileName)
  48. {
  49. bool bResult = false;
  50. if (m_pPicture != NULL)
  51. UnLoad();
  52. if (m_pPicture == NULL)
  53. {
  54. CFile cFile;
  55. CFileException e;
  56. if (cFile.Open(sFileName, CFile::modeRead | CFile::typeBinary, &e))
  57. {
  58. BYTE* pBuff = new BYTE[cFile.GetLength()];
  59. if (cFile.Read(pBuff, cFile.GetLength()) > 0)
  60. {
  61. if (LoadFromBuffer(pBuff, cFile.GetLength()))
  62. bResult = true;
  63. }
  64. delete [] pBuff;
  65. }
  66. }
  67. return bResult;
  68. }
  69. void CPicture::UnLoad()
  70. {
  71. if (m_pPicture != NULL)
  72. {
  73. m_pPicture->Release();
  74. m_pPicture = NULL;
  75. }
  76. }
  77. bool CPicture::Draw(CDC* pDC)
  78. {
  79. if (m_pPicture != NULL)
  80. {
  81. long hmWidth;
  82. long hmHeight;
  83. m_pPicture->get_Width(&hmWidth);
  84. m_pPicture->get_Height(&hmHeight);
  85. int nWidth = MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH);
  86. int nHeight = MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH);
  87. return Draw(pDC, 0, 0, nWidth, nHeight);
  88. }
  89. return false;
  90. }
  91. bool CPicture::Draw(CDC* pDC, CPoint Pos)
  92. {
  93. if (m_pPicture != NULL)
  94. {
  95. long hmWidth;
  96. long hmHeight;
  97. m_pPicture->get_Width(&hmWidth);
  98. m_pPicture->get_Height(&hmHeight);
  99. int nWidth = MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH);
  100. int nHeight = MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH);
  101. return Draw(pDC, Pos.x, Pos.y, nWidth, nHeight);
  102. }
  103. return false;
  104. }
  105. bool CPicture::Draw(CDC* pDC, CPoint Pos, CSize Size)
  106. {
  107. if (m_pPicture != NULL)
  108. {
  109. long hmWidth;
  110. long hmHeight;
  111. m_pPicture->get_Width(&hmWidth);
  112. m_pPicture->get_Height(&hmHeight);
  113. int nWidth = Size.cx;
  114. int nHeight = Size.cy;
  115. return Draw(pDC, Pos.x, Pos.y, nWidth, nHeight);
  116. }
  117. return false;
  118. }
  119. bool CPicture::Draw(CDC* pDC, double nSizeRatio)
  120. {
  121. if (m_pPicture != NULL)
  122. {
  123. long hmWidth;
  124. long hmHeight;
  125. m_pPicture->get_Width(&hmWidth);
  126. m_pPicture->get_Height(&hmHeight);
  127. int nWidth = int(MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH) * nSizeRatio);
  128. int nHeight = int(MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH) * nSizeRatio);
  129. return Draw(pDC, 0, 0, nWidth, nHeight);
  130. }
  131. return false;
  132. }
  133. bool CPicture::Draw(CDC* pDC, CPoint Pos, double nSizeRatio)
  134. {
  135. if (m_pPicture != NULL)
  136. {
  137. long hmWidth;
  138. long hmHeight;
  139. m_pPicture->get_Width(&hmWidth);
  140. m_pPicture->get_Height(&hmHeight);
  141. int nWidth = int(MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH) * nSizeRatio);
  142. int nHeight = int(MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH) * nSizeRatio);
  143. return Draw(pDC, Pos.x, Pos.y, nWidth, nHeight);
  144. }
  145. return false;
  146. }
  147. bool CPicture::Draw(CDC* pDC, int x, int y, int cx, int cy)
  148. {
  149. long hmWidth;
  150. long hmHeight;
  151. m_pPicture->get_Width(&hmWidth);
  152. m_pPicture->get_Height(&hmHeight);
  153. if (m_pPicture->Render(pDC->m_hDC, x, y, cx, cy, 0, hmHeight, hmWidth, -hmHeight, NULL) == S_OK)
  154. return true;
  155. return false;
  156. }
  157. bool CPicture::LoadFromBuffer(BYTE* pBuff, int nSize)
  158. {
  159. bool bResult = false;
  160. HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
  161. void* pData = GlobalLock(hGlobal);
  162. memcpy(pData, pBuff, nSize);
  163. GlobalUnlock(hGlobal);
  164. IStream* pStream = NULL;
  165. if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
  166. {
  167. HRESULT hr;
  168. if ((hr = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&m_pPicture)) == S_OK)
  169. bResult = true;
  170. pStream->Release();
  171. }
  172. return bResult;
  173. }
  174. bool CPicture::GetResource(LPSTR lpName, LPSTR lpType, void* pResource, int& nBufSize)
  175. HRSRC hResInfo;
  176. HANDLE hRes;
  177. HMODULE hInst = NULL; 
  178. LPSTR lpRes = NULL; 
  179. int nLen = 0;
  180. bool bResult = FALSE;
  181. // Find the resource
  182. hResInfo = FindResource(hInst, lpName, lpType);
  183. if (hResInfo == NULL) 
  184. return false;
  185. // Load the resource
  186. hRes = LoadResource(hInst, hResInfo);
  187. if (hRes == NULL) 
  188. return false;
  189. // Lock the resource
  190. lpRes = (char*)LockResource(hRes);
  191. if (lpRes != NULL)
  192. if (pResource == NULL)
  193. {
  194. nBufSize = SizeofResource(hInst, hResInfo);
  195. bResult = true;
  196. }
  197. else
  198. {
  199. if (nBufSize >= (int)SizeofResource(hInst, hResInfo))
  200. {
  201. memcpy(pResource, lpRes, nBufSize);
  202. bResult = true;
  203. }
  204. UnlockResource(hRes);  
  205. }
  206. // Free the resource
  207. FreeResource(hRes);
  208. return bResult;
  209. }
  210. CSize CPicture::GetSize(CDC* pDC)
  211. {
  212. CSize rcResult = CSize(0,0);
  213. if (m_pPicture != NULL)
  214. {
  215. long hmWidth;
  216. long hmHeight;
  217. m_pPicture->get_Width(&hmWidth);
  218. m_pPicture->get_Height(&hmHeight);
  219. rcResult.cx = MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH);
  220. rcResult.cy = MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH);
  221. }
  222. return rcResult;
  223. }