backBuffer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "main.h"
  2. #include "./backBuffer.h"
  3. BOOL
  4. BackBuffer_Initialize(BackBuffer *self, HWND hwnd)
  5. {
  6. if (NULL == self)
  7. return FALSE;
  8. ZeroMemory(self, sizeof(BackBuffer));
  9. self->hwnd = hwnd;
  10. return TRUE;
  11. }
  12. void
  13. BackBuffer_Uninitialize(BackBuffer *self)
  14. {
  15. BackBuffer_Reset(self);
  16. }
  17. void
  18. BackBuffer_Reset(BackBuffer *self)
  19. {
  20. if (NULL == self)
  21. return;
  22. if (NULL != self->hdc)
  23. {
  24. if (NULL != self->previous)
  25. SelectBitmap(self->hdc, self->previous);
  26. DeleteDC(self->hdc);
  27. }
  28. if (NULL != self->bitmap)
  29. {
  30. DeleteObject(self->bitmap);
  31. }
  32. ZeroMemory(self, sizeof(BackBuffer));
  33. }
  34. BOOL
  35. BackBuffer_EnsureSize(BackBuffer *self, long width, long height)
  36. {
  37. return BackBuffer_EnsureSizeEx(self, width, height, width, height);
  38. }
  39. BOOL
  40. BackBuffer_EnsureSizeEx(BackBuffer *self, long width, long height, long allocWidth, long allocHeight)
  41. {
  42. BOOL result;
  43. HDC windowDC;
  44. HBITMAP bitmap;
  45. long bitmapWidth, bitmapHeight;
  46. if (NULL == self)
  47. return FALSE;
  48. if (width < 0)
  49. width = 0;
  50. if (height < 0)
  51. height = 0;
  52. if (NULL != self->bitmap)
  53. {
  54. BITMAP bitmapInfo;
  55. if (sizeof(bitmapInfo) == GetObject(self->bitmap, sizeof(bitmapInfo), &bitmapInfo))
  56. {
  57. if (bitmapInfo.bmWidth >= width && bitmapInfo.bmHeight >= height)
  58. return TRUE;
  59. bitmapWidth = bitmapInfo.bmWidth;
  60. bitmapHeight = bitmapInfo.bmHeight;
  61. }
  62. else
  63. {
  64. bitmapWidth = 0;
  65. bitmapHeight = 0;
  66. }
  67. }
  68. else
  69. {
  70. bitmapWidth = 0;
  71. bitmapHeight = 0;
  72. }
  73. result = FALSE;
  74. bitmap = NULL;
  75. windowDC = GetDCEx(self->hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
  76. if(NULL != windowDC)
  77. {
  78. if (allocWidth < width)
  79. allocWidth = width;
  80. if (allocWidth < bitmapWidth)
  81. allocWidth = bitmapWidth;
  82. if (allocHeight < height)
  83. allocHeight = height;
  84. if (allocHeight < bitmapHeight)
  85. allocHeight = bitmapHeight;
  86. bitmap = CreateCompatibleBitmap(windowDC, allocWidth, allocHeight);
  87. ReleaseDC(self->hwnd, windowDC);
  88. }
  89. if (NULL != bitmap)
  90. {
  91. if (NULL != self->hdc)
  92. SelectBitmap(self->hdc, bitmap);
  93. if (NULL != self->bitmap)
  94. DeleteObject(self->bitmap);
  95. self->bitmap = bitmap;
  96. result = TRUE;
  97. }
  98. return result;
  99. }
  100. HDC
  101. BackBuffer_GetDC(BackBuffer *self)
  102. {
  103. if (NULL == self)
  104. return FALSE;
  105. if (NULL == self->hdc)
  106. {
  107. HDC windowDC;
  108. windowDC = GetDCEx(self->hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
  109. if (NULL != windowDC)
  110. {
  111. self->hdc = CreateCompatibleDC(windowDC);
  112. ReleaseDC(self->hwnd, windowDC);
  113. if (NULL != self->hdc)
  114. {
  115. if (NULL != self->bitmap)
  116. self->previous = SelectBitmap(self->hdc, self->bitmap);
  117. else
  118. self->previous = GetCurrentBitmap(self->hdc);
  119. }
  120. }
  121. }
  122. return self->hdc;
  123. }
  124. BOOL
  125. BackBuffer_Copy(BackBuffer *self, HDC hdc, long x, long y, long width, long height)
  126. {
  127. HDC sourceDC;
  128. if (NULL == self || NULL == self->bitmap)
  129. return FALSE;
  130. sourceDC = BackBuffer_GetDC(self);
  131. if (NULL == sourceDC)
  132. return FALSE;
  133. return BitBlt(hdc, x, y, width, height, sourceDC, 0, 0, SRCCOPY);
  134. }
  135. BOOL
  136. BackBuffer_DrawTextEx(BackBuffer *self, HDC hdc, const wchar_t *string,
  137. int length, RECT *rect, unsigned int format,
  138. HFONT font, COLORREF backColor, COLORREF textColor, int backMode)
  139. {
  140. BOOL result = FALSE;
  141. RECT bufferRect;
  142. if (NULL == hdc || NULL == rect)
  143. return FALSE;
  144. SetRect(&bufferRect, 0, 0, RECTWIDTH(*rect), RECTHEIGHT(*rect));
  145. if (NULL != self &&
  146. FALSE != BackBuffer_EnsureSize(self, bufferRect.right, bufferRect.bottom))
  147. {
  148. HDC bufferDC = BackBuffer_GetDC(self);
  149. if (NULL != bufferDC)
  150. {
  151. HFONT prevFont;
  152. prevFont = SelectFont(bufferDC, font);
  153. SetTextColor(bufferDC, textColor);
  154. SetBkColor(bufferDC, backColor);
  155. SetBkMode(bufferDC, backMode);
  156. if (OPAQUE == backMode)
  157. ExtTextOut(bufferDC, 0, 0, ETO_OPAQUE, &bufferRect, NULL, 0, NULL);
  158. if (FALSE != DrawText(bufferDC, string, length, &bufferRect, format))
  159. {
  160. result = BackBuffer_Copy(self, hdc, rect->left, rect->top,
  161. bufferRect.right, bufferRect.bottom);
  162. }
  163. SelectFont(bufferDC, prevFont);
  164. }
  165. }
  166. if (FALSE == result)
  167. {
  168. HFONT prevFont;
  169. COLORREF prevBackColor, prevTextColor;
  170. int prevBkMode;
  171. prevFont = SelectFont(hdc, font);
  172. prevTextColor = SetTextColor(hdc, textColor);
  173. prevBackColor = SetBkColor(hdc, backColor);
  174. prevBkMode= SetBkMode(hdc, backMode);
  175. result = DrawText(hdc, string, length, rect, format);
  176. SelectFont(hdc, prevFont);
  177. SetTextColor(hdc, prevTextColor);
  178. SetBkColor(hdc, prevBackColor);
  179. SetBkMode(hdc, prevBkMode);
  180. }
  181. return result;
  182. }
  183. BOOL
  184. BackBuffer_DrawText(BackBuffer *self, HDC hdc, const wchar_t *string,
  185. int length, RECT *rect, unsigned int format)
  186. {
  187. BOOL result = FALSE;
  188. RECT bufferRect;
  189. if (NULL == hdc || NULL == rect)
  190. return FALSE;
  191. SetRect(&bufferRect, 0, 0, RECTWIDTH(*rect), RECTHEIGHT(*rect));
  192. if (NULL != self &&
  193. FALSE != BackBuffer_EnsureSize(self, bufferRect.right, bufferRect.bottom))
  194. {
  195. HDC bufferDC = BackBuffer_GetDC(self);
  196. if (NULL != bufferDC)
  197. {
  198. HFONT prevFont;
  199. int backMode;
  200. prevFont = SelectFont(bufferDC, GetCurrentFont(hdc));
  201. SetTextColor(bufferDC, GetTextColor(hdc));
  202. SetBkColor(bufferDC, GetBkColor(hdc));
  203. backMode = GetBkMode(hdc);
  204. SetBkMode(bufferDC, backMode);
  205. if (OPAQUE == backMode)
  206. ExtTextOut(bufferDC, 0, 0, ETO_OPAQUE, &bufferRect, NULL, 0, NULL);
  207. if (FALSE != DrawText(bufferDC, string, length, &bufferRect, format))
  208. {
  209. result = BackBuffer_Copy(self, hdc, rect->left, rect->top,
  210. bufferRect.right, bufferRect.bottom);
  211. }
  212. SelectFont(bufferDC, prevFont);
  213. }
  214. }
  215. if (FALSE == result)
  216. result = DrawText(hdc, string, length, rect, format);
  217. return result;
  218. }