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.

376 lines
8.8 KiB

  1. #include <windows.h>
  2. #include <assert.h>
  3. #include "transbmp.h"
  4. void GetMetrics( HBITMAP hBmp, int &nWidth, int &nHeight )
  5. {
  6. // Get width & height
  7. BITMAP bm;
  8. if ( GetObject(hBmp, sizeof(bm), &bm) > 0)
  9. {
  10. nWidth = bm.bmWidth;
  11. nHeight = bm.bmHeight;
  12. }
  13. assert( nWidth && nHeight );
  14. }
  15. void CreateMask( HDC hDC, HBITMAP hBmp, HBITMAP& hBmpMask, int nWidth, int nHeight )
  16. {
  17. //
  18. // We have to verify if hDC is a valid handler
  19. //
  20. hBmpMask = NULL;
  21. if( (NULL == hDC) || (NULL == hBmp) )
  22. {
  23. hBmpMask = NULL;
  24. return;
  25. }
  26. // Create memory DCs to work with
  27. HDC hdcMask = CreateCompatibleDC( hDC );
  28. //
  29. // We have to verify if hdcMask is a valid handler
  30. //
  31. if( NULL == hdcMask )
  32. {
  33. return;
  34. }
  35. HDC hdcImage = CreateCompatibleDC( hDC );
  36. //
  37. // We have to verify if hdcMask is a valid handler
  38. //
  39. if( NULL == hdcImage )
  40. {
  41. DeleteDC( hdcMask );
  42. return;
  43. }
  44. // Create a monochrome bitmap for the mask
  45. hBmpMask = CreateBitmap( nWidth, nHeight, 1, 1, NULL );
  46. //
  47. // We have to verify if hdcMask is a valid handler
  48. //
  49. if( NULL == hBmpMask )
  50. {
  51. DeleteDC( hdcImage );
  52. DeleteDC( hdcMask );
  53. return;
  54. }
  55. // Select the mono bitmap into its DC
  56. HBITMAP hbmOldMask = (HBITMAP) SelectObject( hdcMask, hBmpMask );
  57. //
  58. // We have to verify if hdcMask is a valid handler
  59. //
  60. if( NULL == hbmOldMask )
  61. {
  62. DeleteObject( hBmpMask );
  63. hBmpMask = NULL;
  64. DeleteDC( hdcImage );
  65. DeleteDC( hdcMask );
  66. return;
  67. }
  68. // Select the image bitmap into its DC
  69. HBITMAP hbmOldImage = (HBITMAP) SelectObject( hdcImage, hBmp );
  70. //
  71. // We have to verify if hdcMask is a valid handler
  72. //
  73. if( NULL == hbmOldImage )
  74. {
  75. SelectObject( hdcMask, hbmOldMask);
  76. DeleteObject( hBmpMask );
  77. hBmpMask = NULL;
  78. DeleteDC( hdcImage );
  79. DeleteDC( hdcMask );
  80. return;
  81. }
  82. // Set the transparency color to be the top-left pixel
  83. SetBkColor( hdcImage, GetPixel(hdcImage, 0, 0) );
  84. // Make the mask
  85. BitBlt( hdcMask, 0, 0, nWidth, nHeight, hdcImage, 0, 0, SRCCOPY );
  86. // Clean up
  87. SelectObject( hdcImage, hbmOldImage );
  88. SelectObject( hdcMask, hbmOldMask );
  89. DeleteDC( hdcMask );
  90. DeleteDC( hdcImage );
  91. }
  92. void Draw( HDC hDC, HBITMAP hBmp, int x, int y, int dx /*= -1*/, int dy /*= -1*/, bool bStretch /* = false*/ )
  93. {
  94. assert( hDC && hBmp );
  95. int nWidth, nHeight;
  96. GetMetrics( hBmp, nWidth, nHeight );
  97. // Create a memory DC
  98. HDC hDCMem = CreateCompatibleDC( hDC );
  99. if ( hDCMem )
  100. {
  101. // Make sure we have valid values for width & height
  102. if ( dx == -1 ) dx = nWidth;
  103. if ( dy == -1 ) dy = nHeight;
  104. if ( !bStretch )
  105. {
  106. dx = min( dx, nWidth );
  107. dy = min( dy, nHeight );
  108. }
  109. HBITMAP hbmOld = (HBITMAP) SelectObject( hDCMem, hBmp );
  110. // Blt the bits
  111. if ( !bStretch )
  112. {
  113. BitBlt( hDC, x, y, dx, dy, hDCMem, 0, 0, SRCCOPY );
  114. }
  115. else
  116. {
  117. SetStretchBltMode((HDC) hDC, COLORONCOLOR);
  118. StretchBlt( hDC, x, y, dx, dy,
  119. hDCMem, 0, 0, nWidth, nHeight, SRCCOPY );
  120. }
  121. SelectObject( hDCMem, hbmOld );
  122. DeleteDC( hDCMem );
  123. }
  124. }
  125. void DrawTrans( HDC hDC, HBITMAP hBmp, int x, int y, int dx /*= -1*/, int dy /*= -1*/ )
  126. {
  127. //
  128. // We should initialize local variables
  129. //
  130. if( (NULL == hDC) || (NULL == hBmp))
  131. {
  132. return;
  133. }
  134. int nWidth = 0, nHeight = 0;
  135. GetMetrics( hBmp, nWidth, nHeight );
  136. // Create transparent bitmap mask
  137. HBITMAP hBmpMask = NULL;
  138. CreateMask( hDC, hBmp, hBmpMask, nWidth, nHeight );
  139. //
  140. //
  141. if( NULL == hBmpMask )
  142. {
  143. return;
  144. }
  145. // Make sure we have valid values for width & height
  146. if ( dx == -1 ) dx = nWidth;
  147. if ( dy == -1 ) dy = nHeight;
  148. dx = min( dx, nWidth );
  149. dy = min( dy, nHeight );
  150. // Create a memory DC in which to draw
  151. HDC hdcOffScr = CreateCompatibleDC( hDC );
  152. //
  153. // We have to verify hdcOffScr is valid
  154. //
  155. if( NULL == hdcOffScr )
  156. {
  157. DeleteObject( hBmpMask );
  158. return;
  159. }
  160. // Create a bitmap for the off-screen DC that is really color-compatible with the
  161. // destination DC
  162. HBITMAP hbmOffScr = CreateBitmap( dx, dy, (BYTE) GetDeviceCaps(hDC, PLANES),
  163. (BYTE) GetDeviceCaps(hDC, BITSPIXEL),
  164. NULL );
  165. //
  166. //
  167. if( NULL == hbmOffScr )
  168. {
  169. DeleteDC( hdcOffScr );
  170. DeleteObject( hBmpMask );
  171. return;
  172. }
  173. // Select the buffer bitmap into the off-screen DC
  174. HBITMAP hbmOldOffScr = (HBITMAP) SelectObject( hdcOffScr, hbmOffScr );
  175. //
  176. //
  177. if( NULL == hbmOldOffScr )
  178. {
  179. DeleteObject( hbmOffScr );
  180. DeleteDC( hdcOffScr );
  181. DeleteObject( hBmpMask );
  182. return;
  183. }
  184. // Copy the image of the destination rectangle to the off-screen buffer DC so
  185. // we can manipulate it
  186. BitBlt( hdcOffScr, 0, 0, dx, dy, hDC, x, y, SRCCOPY);
  187. // Create a memory DC for the source image
  188. HDC hdcImage = CreateCompatibleDC( hDC );
  189. //
  190. // We have to verify the hdcImage
  191. //
  192. if( NULL == hdcImage )
  193. {
  194. // Restore
  195. SelectObject( hdcOffScr, hbmOldOffScr );
  196. DeleteObject( hbmOffScr );
  197. DeleteDC( hdcOffScr );
  198. DeleteObject( hBmpMask );
  199. return;
  200. }
  201. HBITMAP hbmOldImage = (HBITMAP) SelectObject( hdcImage, hBmp );
  202. //
  203. // We have to verify the hbmOldImage
  204. //
  205. if( NULL == hbmOldImage )
  206. {
  207. // Restore
  208. DeleteDC( hdcImage );
  209. SelectObject( hdcOffScr, hbmOldOffScr );
  210. DeleteObject( hbmOffScr );
  211. DeleteDC( hdcOffScr );
  212. DeleteObject( hBmpMask );
  213. return;
  214. }
  215. // Create a memory DC for the mask
  216. HDC hdcMask = CreateCompatibleDC( hDC );
  217. //
  218. //
  219. if( NULL == hdcMask )
  220. {
  221. // Restore
  222. SelectObject( hdcImage, hbmOldImage );
  223. DeleteDC( hdcImage );
  224. SelectObject( hdcOffScr, hbmOldOffScr );
  225. DeleteObject( hbmOffScr );
  226. DeleteDC( hdcOffScr );
  227. DeleteObject( hBmpMask );
  228. return;
  229. }
  230. HBITMAP hbmOldMask = (HBITMAP) SelectObject( hdcMask, hBmpMask );
  231. //
  232. // We have to verify the hbmOldMask
  233. //
  234. if( NULL == hbmOldMask )
  235. {
  236. // Restore
  237. DeleteDC( hdcMask );
  238. SelectObject( hdcImage, hbmOldImage );
  239. DeleteDC( hdcImage );
  240. SelectObject( hdcOffScr, hbmOldOffScr );
  241. DeleteObject( hbmOffScr );
  242. DeleteDC( hdcOffScr );
  243. DeleteObject( hBmpMask );
  244. return;
  245. }
  246. // XOR the image with the destination
  247. SetBkColor( hdcOffScr, RGB(255, 255, 255) );
  248. BitBlt( hdcOffScr, 0, 0, dx, dy, hdcImage, 0, 0, SRCINVERT );
  249. // AND the destination with the mask
  250. BitBlt( hdcOffScr, 0, 0, dx, dy, hdcMask, 0, 0, SRCAND );
  251. // XOR the destination with the image again
  252. BitBlt( hdcOffScr, 0, 0, dx, dy, hdcImage, 0, 0, SRCINVERT);
  253. // Copy the resultant image back to the screen DC
  254. BitBlt( hDC, x, y, dx, dy, hdcOffScr, 0, 0, SRCCOPY );
  255. // Clean up
  256. //
  257. // We have to clean up corectly
  258. //
  259. SelectObject( hdcMask, hbmOldMask);
  260. DeleteDC( hdcMask );
  261. SelectObject( hdcImage, hbmOldImage );
  262. DeleteDC( hdcImage );
  263. SelectObject( hdcOffScr, hbmOldOffScr );
  264. DeleteObject( hbmOffScr );
  265. DeleteDC( hdcOffScr );
  266. DeleteObject( hBmpMask );
  267. }
  268. void Draw3dBox(HDC hDC, RECT& rect, bool bUp)
  269. {
  270. assert ( hDC );
  271. HBRUSH hbrOld = (HBRUSH) SelectObject( hDC, GetSysColorBrush((bUp) ? COLOR_BTNHIGHLIGHT : COLOR_BTNSHADOW) );
  272. // Draw left and top sides of indent.
  273. PatBlt( hDC, rect.left, rect.top, (rect.right - rect.left), 1, PATCOPY );
  274. PatBlt( hDC, rect.left, rect.top, 1, (rect.bottom - rect.top), PATCOPY );
  275. // Draw bottom and right sides of indent.
  276. SelectObject( hDC, GetSysColorBrush((!bUp) ? COLOR_BTNHIGHLIGHT : COLOR_BTNSHADOW) );
  277. PatBlt( hDC, rect.right - 1, rect.top, 1, (rect.bottom - rect.top), PATCOPY );
  278. PatBlt( hDC, rect.left, rect.bottom - 1, (rect.right - rect.left), 1, PATCOPY );
  279. if ( hbrOld )
  280. SelectObject( hDC, hbrOld );
  281. }
  282. void Erase3dBox(HDC hDC, RECT& rect, HBRUSH hbr )
  283. {
  284. assert ( hDC );
  285. HBRUSH hbrOld = (HBRUSH) SelectObject( hDC, (hbr) ? hbr : GetSysColorBrush(GetBkColor(hDC)) );
  286. // Draw left and top sides of indent.
  287. PatBlt( hDC, rect.left, rect.top, (rect.right - rect.left), 1, PATCOPY );
  288. PatBlt( hDC, rect.left, rect.top, 1, (rect.bottom - rect.top), PATCOPY );
  289. // Draw bottom and right sides of indent.
  290. PatBlt( hDC, rect.right - 1, rect.top, 1, (rect.bottom - rect.top), PATCOPY );
  291. PatBlt( hDC, rect.left, rect.bottom - 1, (rect.right - rect.left), 1, PATCOPY );
  292. if ( hbrOld )
  293. SelectObject( hDC, hbrOld );
  294. }