Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
6.0 KiB

  1. //
  2. // cmydc.h
  3. //
  4. #ifndef CMYDC_H
  5. #define CMYDC_H
  6. class CSolidBrush
  7. {
  8. public:
  9. CSolidBrush(int r, int g, int b)
  10. {
  11. _hbr = CreateSolidBrush(RGB(r, g, b));
  12. }
  13. CSolidBrush(COLORREF rgb)
  14. {
  15. _hbr = CreateSolidBrush(rgb);
  16. }
  17. CSolidBrush()
  18. {
  19. _hbr = NULL;
  20. }
  21. BOOL Init(int r, int g, int b)
  22. {
  23. Assert(!_hbr);
  24. _hbr = CreateSolidBrush(RGB(r, g, b));
  25. return _hbr != NULL;
  26. }
  27. BOOL Init(COLORREF rgb)
  28. {
  29. Assert(!_hbr);
  30. _hbr = CreateSolidBrush(rgb);
  31. return _hbr != NULL;
  32. }
  33. ~CSolidBrush()
  34. {
  35. if (_hbr)
  36. DeleteObject(_hbr);
  37. }
  38. operator HBRUSH()
  39. {
  40. return _hbr;
  41. }
  42. private:
  43. HBRUSH _hbr;
  44. };
  45. class CSolidPen
  46. {
  47. public:
  48. CSolidPen()
  49. {
  50. _hpen = NULL;
  51. }
  52. BOOL Init(int r, int g, int b)
  53. {
  54. Assert(!_hpen);
  55. _hpen = CreatePen(PS_SOLID, 0, RGB(r, g, b));
  56. return _hpen != NULL;
  57. }
  58. BOOL Init(COLORREF rgb)
  59. {
  60. Assert(!_hpen);
  61. _hpen = CreatePen(PS_SOLID, 0, rgb);
  62. return _hpen != NULL;
  63. }
  64. ~CSolidPen()
  65. {
  66. if (_hpen)
  67. DeleteObject(_hpen);
  68. }
  69. operator HPEN()
  70. {
  71. return _hpen;
  72. }
  73. private:
  74. HPEN _hpen;
  75. };
  76. class CPatternBrush
  77. {
  78. public:
  79. CPatternBrush(HBITMAP hbmp)
  80. {
  81. _hbr = CreatePatternBrush(hbmp);
  82. }
  83. ~CPatternBrush()
  84. {
  85. if (_hbr)
  86. DeleteObject(_hbr);
  87. }
  88. operator HBRUSH()
  89. {
  90. return _hbr;
  91. }
  92. private:
  93. HBRUSH _hbr;
  94. };
  95. class CBitmapDC
  96. {
  97. public:
  98. CBitmapDC(BOOL fCompat = FALSE)
  99. {
  100. _hbmp = NULL;
  101. _hbmpOld = NULL;
  102. _hbrOld = NULL;
  103. if (!fCompat)
  104. _hdc = CreateDC("DISPLAY", NULL, NULL, NULL);
  105. else
  106. {
  107. _hdc = CreateCompatibleDC(NULL);
  108. }
  109. Assert(HandleToULong(_hdc));
  110. }
  111. ~CBitmapDC()
  112. {
  113. Uninit();
  114. DeleteDC(_hdc);
  115. }
  116. void Uninit(BOOL fKeep = FALSE)
  117. {
  118. if (_hbmpOld)
  119. {
  120. SelectObject(_hdc, _hbmpOld);
  121. _hbmpOld = NULL;
  122. }
  123. if (_hbrOld)
  124. {
  125. SelectObject(_hdc, _hbrOld);
  126. _hbrOld = NULL;
  127. }
  128. if (!fKeep && _hbmp != NULL)
  129. {
  130. DeleteObject(_hbmp);
  131. _hbmp = NULL;
  132. }
  133. }
  134. BOOL SetCompatibleBitmap(int x, int y)
  135. {
  136. Assert(!_hbmp);
  137. Assert(!_hbmpOld);
  138. HDC hdc = GetDC(NULL);
  139. _hbmp = CreateCompatibleBitmap(hdc, x, y);
  140. ReleaseDC(NULL, hdc);
  141. Assert(HandleToULong(_hbmp));
  142. _hbmpOld = (HBITMAP)SelectObject(_hdc, _hbmp);
  143. Assert(HandleToULong(_hbmpOld));
  144. return TRUE;
  145. }
  146. BOOL SetDIB(int cx, int cy, WORD iPlanes = 1, WORD iBitCount = 32)
  147. {
  148. BITMAPINFO bi = {0};
  149. bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
  150. bi.bmiHeader.biWidth = cx;
  151. bi.bmiHeader.biHeight = cy;
  152. bi.bmiHeader.biPlanes = iPlanes;
  153. bi.bmiHeader.biBitCount = iBitCount;
  154. bi.bmiHeader.biCompression = BI_RGB;
  155. _hbmp = CreateDIBSection(_hdc, &bi, DIB_RGB_COLORS, NULL, NULL, 0);
  156. Assert(HandleToULong(_hbmp));
  157. _hbmpOld = (HBITMAP)SelectObject(_hdc, _hbmp);
  158. Assert(HandleToULong(_hbmpOld));
  159. return TRUE;
  160. }
  161. BOOL SetBitmap(int x, int y, int cPlanes, int cBitPerPixel)
  162. {
  163. Assert(!_hbmp);
  164. Assert(!_hbmpOld);
  165. _hbmp = CreateBitmap(x, y, cPlanes, cBitPerPixel, NULL);
  166. Assert(HandleToULong(_hbmp));
  167. _hbmpOld = (HBITMAP)SelectObject(_hdc, _hbmp);
  168. Assert(HandleToULong(_hbmpOld));
  169. return TRUE;
  170. }
  171. BOOL SetBitmap(HBITMAP hbmp)
  172. {
  173. if (_hdc)
  174. {
  175. Assert(!_hbmpOld);
  176. _hbmpOld = (HBITMAP)SelectObject(_hdc, hbmp);
  177. Assert(HandleToULong(_hbmpOld));
  178. }
  179. return TRUE;
  180. }
  181. BOOL SetBitmapFromRes(HINSTANCE hInst, LPCSTR lp)
  182. {
  183. Assert(!_hbmp);
  184. Assert(!_hbmpOld);
  185. _hbmp = LoadBitmap(hInst, lp);
  186. Assert(HandleToULong(_hbmp));
  187. _hbmpOld = (HBITMAP)SelectObject(_hdc, _hbmp);
  188. Assert(HandleToULong(_hbmpOld));
  189. return TRUE;
  190. }
  191. BOOL SetBrush(HBRUSH hbr)
  192. {
  193. if (hbr)
  194. {
  195. _hbrOld = (HBRUSH)SelectObject(_hdc, hbr);
  196. Assert(HandleToULong(_hbrOld));
  197. }
  198. else
  199. {
  200. SelectObject(_hdc, _hbrOld);
  201. _hbrOld = NULL;
  202. }
  203. return TRUE;
  204. }
  205. operator HDC()
  206. {
  207. return _hdc;
  208. }
  209. HBITMAP GetBitmapAndKeep()
  210. {
  211. HBITMAP hbmp = _hbmp;
  212. // don't delet _hbmp;
  213. _hbmp = NULL;
  214. return hbmp;
  215. }
  216. HBITMAP GetBitmap()
  217. {
  218. return _hbmp;
  219. }
  220. private:
  221. HBITMAP _hbmp;
  222. HBITMAP _hbmpOld;
  223. HBRUSH _hbrOld;
  224. HDC _hdc;
  225. };
  226. __inline HBITMAP StretchBitmap(HBITMAP hbmp, int cx, int cy)
  227. {
  228. BITMAP bmp;
  229. CBitmapDC hdcSrc(TRUE);
  230. CBitmapDC hdcDst(TRUE);
  231. GetObject( hbmp, sizeof(bmp), &bmp );
  232. hdcSrc.SetBitmap(hbmp);
  233. hdcDst.SetCompatibleBitmap(cx, cy);
  234. StretchBlt(hdcDst, 0, 0, cx, cy,
  235. hdcSrc, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
  236. return hdcDst.GetBitmapAndKeep();
  237. }
  238. _inline UINT GetPhysicalFontHeight(LOGFONT &lf)
  239. {
  240. HDC hdc = GetDC(NULL);
  241. HFONT hfont;
  242. UINT nRet = 0;
  243. if((hfont = CreateFontIndirect(&lf)))
  244. {
  245. TEXTMETRIC tm;
  246. HFONT hfontOld;
  247. hfontOld = (HFONT)SelectObject( hdc, hfont);
  248. GetTextMetrics(hdc, &tm);
  249. nRet = tm.tmHeight + tm.tmExternalLeading;
  250. if (hfontOld)
  251. SelectObject(hdc, hfontOld);
  252. DeleteObject(hfont);
  253. }
  254. ReleaseDC(NULL, hdc);
  255. return nRet;
  256. }
  257. #endif // CMYDC_H