Guest User

Untitled

a guest
Oct 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. //-----------------------------------------------------------------
  2. // Bitmap Object
  3. // C++ Source - Bitmap.cpp
  4. //-----------------------------------------------------------------
  5.  
  6. //-----------------------------------------------------------------
  7. // Include Files
  8. //-----------------------------------------------------------------
  9. #include "Bitmap.h"
  10.  
  11. //-----------------------------------------------------------------
  12. // Bitmap Constructor(s)/Destructor
  13. //-----------------------------------------------------------------
  14. Bitmap::Bitmap()
  15. : m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
  16. {
  17. }
  18.  
  19. // Create a bitmap from a file
  20. Bitmap::Bitmap(HDC hDC, LPTSTR szFileName)
  21. : m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
  22. {
  23. Create(hDC, szFileName);
  24. }
  25.  
  26. // Create a bitmap from a resource
  27. Bitmap::Bitmap(HDC hDC, UINT uiResID, HINSTANCE hInstance)
  28. : m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
  29. {
  30. Create(hDC, uiResID, hInstance);
  31. }
  32.  
  33. // Create a blank bitmap from scratch
  34. Bitmap::Bitmap(HDC hDC, int iWidth, int iHeight, COLORREF crColor)
  35. : m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
  36. {
  37. Create(hDC, iWidth, iHeight, crColor);
  38. }
  39.  
  40. Bitmap::~Bitmap()
  41. {
  42. Free();
  43. }
  44.  
  45. //-----------------------------------------------------------------
  46. // Bitmap Helper Methods
  47. //-----------------------------------------------------------------
  48. void Bitmap::Free()
  49. {
  50. // Delete the bitmap graphics object
  51. if (m_hBitmap != NULL)
  52. {
  53. DeleteObject(m_hBitmap);
  54. m_hBitmap = NULL;
  55. }
  56. }
  57.  
  58. //-----------------------------------------------------------------
  59. // Bitmap General Methods
  60. //-----------------------------------------------------------------
  61. BOOL Bitmap::Create(HDC hDC, LPTSTR szFileName)
  62. {
  63. // Free any previous bitmap info
  64. Free();
  65.  
  66. // Open the bitmap file
  67. HANDLE hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
  68. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  69. if (hFile == INVALID_HANDLE_VALUE)
  70. return FALSE;
  71.  
  72. // Read the bitmap file header
  73. BITMAPFILEHEADER bmfHeader;
  74. DWORD dwBytesRead;
  75. BOOL bOK = ReadFile(hFile, &bmfHeader, sizeof(BITMAPFILEHEADER),
  76. &dwBytesRead, NULL);
  77. if ((!bOK) || (dwBytesRead != sizeof(BITMAPFILEHEADER)) ||
  78. (bmfHeader.bfType != 0x4D42))
  79. {
  80. CloseHandle(hFile);
  81. return FALSE;
  82. }
  83.  
  84. BITMAPINFO* pBitmapInfo = (BITMAPINFO*)(new BITMAPINFO_256);
  85. if (pBitmapInfo != NULL)
  86. {
  87. // Read the bitmap info header
  88. bOK = ReadFile(hFile, pBitmapInfo, sizeof(BITMAPINFOHEADER),
  89. &dwBytesRead, NULL);
  90. if ((!bOK) || (dwBytesRead != sizeof(BITMAPINFOHEADER)))
  91. {
  92. CloseHandle(hFile);
  93. Free();
  94. return FALSE;
  95. }
  96.  
  97. // Store the width and height of the bitmap
  98. m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth;
  99. m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;
  100.  
  101. // Skip (forward or backward) to the color info, if necessary
  102. if (pBitmapInfo->bmiHeader.biSize != sizeof(BITMAPINFOHEADER))
  103. SetFilePointer(hFile, pBitmapInfo->bmiHeader.biSize - sizeof
  104. (BITMAPINFOHEADER), NULL, FILE_CURRENT);
  105.  
  106. // Read the color info
  107. bOK = ReadFile(hFile, pBitmapInfo->bmiColors,
  108. pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD), &dwBytesRead,
  109. NULL);
  110.  
  111. // Get a handle to the bitmap and copy the image bits
  112. PBYTE pBitmapBits;
  113. m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS,
  114. (PVOID*)&pBitmapBits, NULL, 0);
  115. if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
  116. {
  117. SetFilePointer(hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);
  118. bOK = ReadFile(hFile, pBitmapBits, pBitmapInfo->bmiHeader.biSizeImage,
  119. &dwBytesRead, NULL);
  120. if (bOK)
  121. return TRUE;
  122. }
  123. }
  124.  
  125. // Something went wrong, so cleanup everything
  126. Free();
  127. return FALSE;
  128. }
  129.  
  130. BOOL Bitmap::Create(HDC hDC, UINT uiResID, HINSTANCE hInstance)
  131. {
  132. // Free any previous DIB info
  133. Free();
  134.  
  135. // Find the bitmap resource
  136. HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(uiResID), RT_BITMAP);
  137. if (hResInfo == NULL)
  138. return FALSE;
  139.  
  140. // Load the bitmap resource
  141. HGLOBAL hMemBitmap = LoadResource(hInstance, hResInfo);
  142. if (hMemBitmap == NULL)
  143. return FALSE;
  144.  
  145. // Lock the resource and access the entire bitmap image
  146. PBYTE pBitmapImage = (BYTE*)LockResource(hMemBitmap);
  147. if (pBitmapImage == NULL)
  148. {
  149. FreeResource(hMemBitmap);
  150. return FALSE;
  151. }
  152.  
  153. // Store the width and height of the bitmap
  154. BITMAPINFO* pBitmapInfo = (BITMAPINFO*)pBitmapImage;
  155. m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth;
  156. m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;
  157.  
  158. // Get a handle to the bitmap and copy the image bits
  159. PBYTE pBitmapBits;
  160. m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS,
  161. (PVOID*)&pBitmapBits, NULL, 0);
  162. if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
  163. {
  164. const PBYTE pTempBits = pBitmapImage + pBitmapInfo->bmiHeader.biSize +
  165. pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD);
  166. CopyMemory(pBitmapBits, pTempBits, pBitmapInfo->bmiHeader.biSizeImage);
  167.  
  168. // Unlock and free the bitmap graphics object
  169. UnlockResource(hMemBitmap);
  170. FreeResource(hMemBitmap);
  171. return TRUE;
  172. }
  173.  
  174. // Something went wrong, so cleanup everything
  175. UnlockResource(hMemBitmap);
  176. FreeResource(hMemBitmap);
  177. Free();
  178. return FALSE;
  179. }
  180.  
  181. BOOL Bitmap::Create(HDC hDC, int iWidth, int iHeight, COLORREF crColor)
  182. {
  183. // Create a blank bitmap
  184. m_hBitmap = CreateCompatibleBitmap(hDC, iWidth, iHeight);
  185. if (m_hBitmap == NULL)
  186. return FALSE;
  187.  
  188. // Set the width and height
  189. m_iWidth = iWidth;
  190. m_iHeight = iHeight;
  191.  
  192. // Create a memory device context to draw on the bitmap
  193. HDC hMemDC = CreateCompatibleDC(hDC);
  194.  
  195. // Create a solid brush to fill the bitmap
  196. HBRUSH hBrush = CreateSolidBrush(crColor);
  197.  
  198. // Select the bitmap into the device context
  199. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);
  200.  
  201. // Fill the bitmap with a solid color
  202. RECT rcBitmap = { 0, 0, m_iWidth, m_iHeight };
  203. FillRect(hMemDC, &rcBitmap, hBrush);
  204.  
  205. // Cleanup
  206. SelectObject(hMemDC, hOldBitmap);
  207. DeleteDC(hMemDC);
  208. DeleteObject(hBrush);
  209.  
  210. return TRUE;
  211. }
  212.  
  213. void Bitmap::Draw(HDC hDC, int x, int y, BOOL bTrans, COLORREF crTransColor)
  214. {
  215. if (m_hBitmap != NULL)
  216. {
  217. // Create a memory device context for the bitmap
  218. HDC hMemDC = CreateCompatibleDC(hDC);
  219.  
  220. // Select the bitmap into the device context
  221. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);
  222.  
  223. // Draw the bitmap to the destination device context
  224. if (bTrans)
  225. TransparentBlt(hDC, x, y, GetWidth(), GetHeight(), hMemDC, 0, 0,
  226. GetWidth(), GetHeight(), crTransColor);
  227. else
  228. BitBlt(hDC, x, y, GetWidth(), GetHeight(), hMemDC, 0, 0, SRCCOPY);
  229.  
  230. // Restore and delete the memory device context
  231. SelectObject(hMemDC, hOldBitmap);
  232. DeleteDC(hMemDC);
  233. }
  234. }
Add Comment
Please, Sign In to add comment