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.

321 lines
10 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright(c) Microsoft Corp., 1994 **
  4. //*********************************************************************
  5. #include "pre.h"
  6. // Local private function for drawing transparent bitmaps
  7. static void DrawTransparentBitmap
  8. (
  9. HDC hdc, // Destination DC
  10. HBITMAP hBitmap, // The Bitmap to Draw
  11. long xStart, // Upper Left starting point
  12. long yStart, // Upport Left Starting Point
  13. COLORREF cTransparentColor
  14. )
  15. {
  16. BITMAP bm;
  17. COLORREF cColor;
  18. HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
  19. HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
  20. HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
  21. POINT ptSize;
  22. hdcTemp = CreateCompatibleDC(hdc);
  23. SelectObject(hdcTemp, hBitmap); // Select the bitmap
  24. GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  25. ptSize.x = bm.bmWidth; // Get width of bitmap
  26. ptSize.y = bm.bmHeight; // Get height of bitmap
  27. DPtoLP(hdcTemp, &ptSize, 1); // Convert from device
  28. // to logical points
  29. // Create some DCs to hold temporary data.
  30. hdcBack = CreateCompatibleDC(hdc);
  31. hdcObject = CreateCompatibleDC(hdc);
  32. hdcMem = CreateCompatibleDC(hdc);
  33. hdcSave = CreateCompatibleDC(hdc);
  34. // Create a bitmap for each DC. DCs are required for a number of
  35. // GDI functions.
  36. // Monochrome DC
  37. bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
  38. // Monochrome DC
  39. bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
  40. bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
  41. bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
  42. // Each DC must select a bitmap object to store pixel data.
  43. bmBackOld = (HBITMAP)SelectObject(hdcBack, bmAndBack);
  44. bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject);
  45. bmMemOld = (HBITMAP)SelectObject(hdcMem, bmAndMem);
  46. bmSaveOld = (HBITMAP)SelectObject(hdcSave, bmSave);
  47. // Set proper mapping mode.
  48. SetMapMode(hdcTemp, GetMapMode(hdc));
  49. // Save the bitmap sent here, because it will be overwritten.
  50. BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
  51. // Set the background color of the source DC to the color.
  52. // contained in the parts of the bitmap that should be transparent
  53. cColor = SetBkColor(hdcTemp, cTransparentColor);
  54. // Create the object mask for the bitmap by performing a BitBlt
  55. // from the source bitmap to a monochrome bitmap.
  56. BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
  57. // Set the background color of the source DC back to the original
  58. // color.
  59. SetBkColor(hdcTemp, cColor);
  60. // Create the inverse of the object mask.
  61. BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, NOTSRCCOPY);
  62. // Copy the background of the main DC to the destination.
  63. BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
  64. SRCCOPY);
  65. // Mask out the places where the bitmap will be placed.
  66. BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
  67. // Mask out the transparent colored pixels on the bitmap.
  68. BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
  69. // XOR the bitmap with the background on the destination DC.
  70. BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
  71. // Copy the destination to the screen.
  72. BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY);
  73. // Place the original bitmap back into the bitmap sent here.
  74. BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
  75. // Delete the memory bitmaps.
  76. DeleteObject(SelectObject(hdcBack, bmBackOld));
  77. DeleteObject(SelectObject(hdcObject, bmObjectOld));
  78. DeleteObject(SelectObject(hdcMem, bmMemOld));
  79. DeleteObject(SelectObject(hdcSave, bmSaveOld));
  80. // Delete the memory DCs.
  81. DeleteDC(hdcMem);
  82. DeleteDC(hdcBack);
  83. DeleteDC(hdcObject);
  84. DeleteDC(hdcSave);
  85. DeleteDC(hdcTemp);
  86. }
  87. CICWButton::CICWButton(void)
  88. {
  89. m_vAlign = DT_VCENTER;
  90. m_bDisplayButton = TRUE;
  91. }
  92. CICWButton::~CICWButton( void )
  93. {
  94. if (m_hbmPressed)
  95. DeleteObject(m_hbmPressed);
  96. if (m_hbmUnpressed)
  97. DeleteObject(m_hbmUnpressed);
  98. if (m_hfont)
  99. DeleteObject(m_hfont);
  100. }
  101. HRESULT CICWButton::SetButtonParams
  102. (
  103. long xPos,
  104. LPTSTR lpszPressedBmp,
  105. LPTSTR lpszUnpressedBmp,
  106. LPTSTR lpszFontFace,
  107. long lFontSize,
  108. long lFontWeight,
  109. COLORREF clrFontColor,
  110. COLORREF clrTransparentColor,
  111. COLORREF clrDisabled,
  112. long vAlign
  113. )
  114. {
  115. BITMAP bmInfo;
  116. LOGFONT lfButtonText;
  117. // Set the Button's xPosition
  118. m_xPos = xPos;
  119. if (NULL == (m_hbmPressed = (HBITMAP)LoadImage(g_hInstance,
  120. lpszPressedBmp,
  121. IMAGE_BITMAP,
  122. 0,
  123. 0,
  124. LR_LOADFROMFILE)))
  125. {
  126. return E_FAIL;
  127. }
  128. if (NULL == (m_hbmUnpressed = (HBITMAP)LoadImage(g_hInstance,
  129. lpszUnpressedBmp,
  130. IMAGE_BITMAP,
  131. 0,
  132. 0,
  133. LR_LOADFROMFILE)))
  134. {
  135. return E_FAIL;
  136. }
  137. // Set the transparent color
  138. m_clrTransparent = clrTransparentColor;
  139. // Set the text color
  140. m_clrText = clrFontColor;
  141. // Set the Disabled color
  142. m_clrDisabledText = clrDisabled;
  143. // Set the vertical alignment
  144. if (-1 != vAlign)
  145. m_vAlign = vAlign;
  146. // Fill in the default text log font
  147. lfButtonText.lfHeight = -lFontSize;
  148. lfButtonText.lfWidth = 0;
  149. lfButtonText.lfEscapement = 0;
  150. lfButtonText.lfOrientation = 0;
  151. lfButtonText.lfWeight = lFontWeight;
  152. lfButtonText.lfItalic = FALSE;
  153. lfButtonText.lfUnderline = FALSE;
  154. lfButtonText.lfStrikeOut = FALSE;
  155. lfButtonText.lfCharSet = DEFAULT_CHARSET;
  156. lfButtonText.lfOutPrecision = OUT_DEFAULT_PRECIS;
  157. lfButtonText.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  158. lfButtonText.lfQuality = DEFAULT_QUALITY;
  159. lfButtonText.lfPitchAndFamily = VARIABLE_PITCH | FF_DONTCARE;
  160. lstrcpy(lfButtonText.lfFaceName, lpszFontFace);
  161. // Create the font for drawing the button
  162. if (NULL == (m_hfont = CreateFontIndirect(&lfButtonText)))
  163. return E_FAIL;
  164. // Compute the client button area
  165. if (GetObject(m_hbmUnpressed, sizeof(BITMAP), (LPVOID) &bmInfo))
  166. {
  167. m_rcBtnClient.left = 0;
  168. m_rcBtnClient.top = 0;
  169. m_rcBtnClient.right = bmInfo.bmWidth;
  170. m_rcBtnClient.bottom = bmInfo.bmHeight;
  171. return S_OK;
  172. }
  173. else
  174. {
  175. return E_FAIL;
  176. }
  177. }
  178. HRESULT CICWButton::CreateButtonWindow(HWND hWndParent, UINT uiCtlID)
  179. {
  180. HRESULT hr = S_OK;
  181. m_hWndButton = CreateWindow( TEXT("Button"),
  182. NULL,
  183. BS_OWNERDRAW | WS_VISIBLE | WS_CHILD | WS_TABSTOP,
  184. m_xPos,
  185. m_yPos,
  186. RECTWIDTH(m_rcBtnClient),
  187. RECTHEIGHT(m_rcBtnClient),
  188. hWndParent,
  189. (HMENU) UlongToPtr(uiCtlID),
  190. g_hInstance,
  191. NULL);
  192. if (m_hWndButton)
  193. {
  194. ShowWindow(m_hWndButton, m_bDisplayButton ? SW_SHOW : SW_HIDE);
  195. UpdateWindow(m_hWndButton);
  196. }
  197. else
  198. {
  199. hr = E_FAIL;
  200. }
  201. return (hr);
  202. }
  203. void CICWButton::DrawButton(HDC hdc, UINT itemState, LPPOINT lppt)
  204. {
  205. HFONT hOldFont;
  206. COLORREF clrOldColor;
  207. COLORREF clrText;
  208. HBITMAP hbmUsed;
  209. DWORD dwStyle=GetWindowLong(m_hWndButton,GWL_STYLE);
  210. RECT rcFocus;
  211. if (itemState & ODS_SELECTED)
  212. hbmUsed = m_hbmPressed;
  213. else
  214. hbmUsed = m_hbmUnpressed;
  215. if (itemState & ODS_DISABLED)
  216. clrText = m_clrDisabledText;
  217. else
  218. clrText = m_clrText;
  219. DrawTransparentBitmap(hdc,
  220. hbmUsed,
  221. lppt->x,
  222. lppt->y,
  223. m_clrTransparent);
  224. hOldFont = (HFONT)SelectObject(hdc, m_hfont);
  225. clrOldColor = SetTextColor(hdc, clrText);
  226. DrawText(hdc, m_szButtonText, -1, &m_rcBtnClient, m_vAlign | DT_CENTER | DT_SINGLELINE);
  227. SetTextColor(hdc, clrOldColor);
  228. SelectObject(hdc, hOldFont);
  229. if (itemState & ODS_FOCUS)
  230. {
  231. CopyRect(&rcFocus, &m_rcBtnClient);
  232. DrawText(hdc, m_szButtonText, -1, &rcFocus, DT_CALCRECT | DT_SINGLELINE | DT_LEFT | DT_TOP);
  233. OffsetRect(&rcFocus, (m_rcBtnClient.left + m_rcBtnClient.right - rcFocus.right) /
  234. 2, (m_rcBtnClient.top + m_rcBtnClient.bottom - rcFocus.bottom) / 2);
  235. InflateRect(&rcFocus, 10, 1);
  236. DrawFocusRect(hdc, &rcFocus);
  237. }
  238. };
  239. HRESULT CICWButton::GetClientRect
  240. (
  241. LPRECT lpRect
  242. )
  243. {
  244. if (!lpRect)
  245. return E_POINTER;
  246. memcpy(lpRect, &m_rcBtnClient, sizeof(RECT));
  247. return (S_OK);
  248. }
  249. HRESULT CICWButton::Enable
  250. (
  251. BOOL bEnable
  252. )
  253. {
  254. EnableWindow(m_hWndButton, bEnable);
  255. return S_OK;
  256. }
  257. HRESULT CICWButton::Show
  258. (
  259. int nShowCmd
  260. )
  261. {
  262. ShowWindow(m_hWndButton, m_bDisplayButton ? nShowCmd : SW_HIDE);
  263. return S_OK;
  264. }