Source code of Windows XP (NT5)
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.

396 lines
9.7 KiB

  1. /*
  2. **************************************************************************
  3. *
  4. * dispfram.c
  5. *
  6. * DispFrame Control DLL defines a bitmap display control to be used
  7. * by any windows application.
  8. *
  9. * Copyright 1991-3, Microsoft Corporation
  10. *
  11. * History:
  12. *
  13. * In Sik Rhee - 7/15/91 (original slider.dll)
  14. * Ben Mejia - 1/22/92 (made display.dll)
  15. **************************************************************************
  16. */
  17. #pragma warning(disable:4704)
  18. #include <windows.h>
  19. #include <custcntl.h>
  20. #include <commctrl.h>
  21. #include "draw.h"
  22. /*
  23. **************************************************************************
  24. * global static variables
  25. **************************************************************************
  26. */
  27. UINT gwPm_DispFrame;
  28. extern HINSTANCE ghInstance;
  29. /*
  30. **************************************************************************
  31. * prototypes
  32. **************************************************************************
  33. */
  34. LONG PASCAL dfPaint (HWND hWnd);
  35. LONG PASCAL dfSetBitmap (HWND hWnd, HBITMAP hBmpNew, HPALETTE
  36. hPalNew);
  37. BOOL PASCAL RegSndCntrlClass (LPCTSTR lpszSndCntrlClass);
  38. LRESULT PASCAL dfDispFrameWndFn (HWND hWnd, UINT wMessage, WPARAM wParam,
  39. LPARAM lParam);
  40. BOOL dfDrawRect (HDC hdcRect, RECT rFrame);
  41. /*
  42. **************************************************************************
  43. * RegSndCntrlClass
  44. *
  45. * Description: Registers the SndCntrlClass, must be called in LibMain
  46. *
  47. * Arguments:
  48. * LPTSTR lpszSndCntrlClass
  49. *
  50. * Returns: BOOL
  51. * TRUE if RegisterClass succeeds, else FALSE
  52. *
  53. **************************************************************************
  54. */
  55. BOOL PASCAL RegSndCntrlClass(LPCTSTR lpszSndCntrlClass)
  56. {
  57. extern UINT gwPm_DispFrame;
  58. /* local variables */
  59. WNDCLASS ClassStruct;
  60. /* check to see if class already exists; if so, simply return TRUE */
  61. if (GetClassInfo(ghInstance, lpszSndCntrlClass, &ClassStruct))
  62. return TRUE;
  63. /* define dispfram class attributes */
  64. ClassStruct.lpszClassName = (LPTSTR)DISPFRAMCLASS;
  65. ClassStruct.hCursor = LoadCursor( NULL, IDC_ARROW );
  66. ClassStruct.lpszMenuName = (LPTSTR)NULL;
  67. ClassStruct.style = CS_HREDRAW|CS_VREDRAW|CS_GLOBALCLASS;
  68. ClassStruct.lpfnWndProc = dfDispFrameWndFn;
  69. ClassStruct.hInstance = ghInstance;
  70. ClassStruct.hIcon = NULL;
  71. ClassStruct.cbWndExtra = DF_DISP_EXTRA;
  72. ClassStruct.cbClsExtra = 0;
  73. ClassStruct.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1 );
  74. /* register display frame window class */
  75. if (!RegisterClass(&ClassStruct))
  76. return FALSE;
  77. gwPm_DispFrame = RegisterWindowMessage((LPTSTR) DF_WMDISPFRAME);
  78. if (!gwPm_DispFrame) /* failed to create message */
  79. return FALSE;
  80. return TRUE;
  81. }
  82. /*
  83. **************************************************************************
  84. * dfDispFrameWndFn
  85. *
  86. * Description: Window function for display frame control.
  87. *
  88. * Arguments:
  89. * HWND hWnd - handle to control window.
  90. * UINT wMessage - the message
  91. * WPARAM wParam
  92. * LPARAM lParam
  93. * Returns: LONG
  94. * result of message processing... depends on message sent.
  95. *
  96. **************************************************************************
  97. */
  98. LRESULT PASCAL dfDispFrameWndFn(HWND hWnd, UINT wMessage, WPARAM wParam,
  99. LPARAM lParam)
  100. {
  101. HBITMAP hBmp;
  102. switch (wMessage)
  103. {
  104. case WM_CREATE:
  105. DF_SET_BMPHANDLE(0);
  106. DF_SET_BMPPAL(0);
  107. return 0;
  108. case WM_DESTROY:
  109. /* Free up stored bitmap and palette, if any.
  110. */
  111. hBmp = (HBITMAP)DF_GET_BMPHANDLE;
  112. if (hBmp)
  113. DeleteObject(hBmp);
  114. return 0;
  115. case WM_SYSCOLORCHANGE:
  116. InvalidateRect(hWnd, NULL, TRUE);
  117. return 0;
  118. case WM_PAINT:
  119. return dfPaint(hWnd);
  120. /* Custom Control Messages */
  121. case DF_PM_SETBITMAP:
  122. return dfSetBitmap(hWnd, (HBITMAP)wParam, (HPALETTE)lParam);
  123. }
  124. return DefWindowProc(hWnd, wMessage, wParam, lParam);
  125. }
  126. /*
  127. **************************************************************************
  128. * dfDrawRect
  129. *
  130. * Description: Draws background of control window.
  131. *
  132. * Params:
  133. * HWND hWnd - handle to control window.
  134. * RECT rFrame - bounding rectangle.
  135. *
  136. * Returns: BOOL
  137. * Pass/Fail indicator 0 indicates failure.
  138. **************************************************************************
  139. */
  140. BOOL dfDrawRect(HDC hdcRect, RECT rFrame)
  141. {
  142. HANDLE hBrush;
  143. HANDLE hOldBrush;
  144. HANDLE hPen;
  145. HANDLE hOldPen;
  146. HANDLE hPen3DHILIGHT;
  147. /* Get DC's pen and brush for frame redraw
  148. */
  149. hBrush = CreateSolidBrush(GetSysColor(COLOR_3DFACE));
  150. if (!hBrush)
  151. return FALSE;
  152. hPen = CreatePen(PS_SOLID, 2, GetSysColor(COLOR_WINDOWFRAME));
  153. if (!hPen)
  154. {
  155. DeleteObject(hBrush);
  156. return FALSE;
  157. }
  158. hPen3DHILIGHT = CreatePen(PS_SOLID, 2, GetSysColor(COLOR_3DHILIGHT));
  159. if (!hPen3DHILIGHT)
  160. {
  161. DeleteObject(hBrush);
  162. DeleteObject(hPen);
  163. return FALSE;
  164. }
  165. hOldBrush = SelectObject(hdcRect, hBrush);
  166. //hOldPen = SelectObject(hdcRect, hPen);
  167. hOldPen = SelectObject(hdcRect, hPen3DHILIGHT);
  168. /* paint the window.
  169. */
  170. //Rectangle(hdcRect, rFrame.left, rFrame.top, rFrame.right,
  171. // rFrame.bottom);
  172. MoveToEx(hdcRect, rFrame.left,rFrame.bottom, NULL);
  173. LineTo(hdcRect, rFrame.right,rFrame.bottom);
  174. LineTo(hdcRect, rFrame.right,rFrame.top);
  175. SelectObject(hdcRect, hPen);
  176. LineTo(hdcRect, rFrame.left,rFrame.top);
  177. LineTo(hdcRect, rFrame.left,rFrame.bottom);
  178. SelectObject(hdcRect, hOldPen);
  179. SelectObject(hdcRect, hOldBrush);
  180. /*clean up brush and pen */
  181. //DeleteObject();
  182. DeleteObject(hBrush);
  183. DeleteObject(hPen3DHILIGHT);
  184. DeleteObject(hPen);
  185. return TRUE;
  186. }
  187. /*
  188. **************************************************************************
  189. * dfPaint
  190. *
  191. * Description: Paints background and bitmap, if any.
  192. *
  193. * Params:
  194. * HWND hWnd - handle to control window
  195. *
  196. * Returns: LONG
  197. * 0 if OK, -1 otherwise. (for windproc return)
  198. *
  199. **************************************************************************
  200. */
  201. LONG PASCAL dfPaint(HWND hWnd)
  202. {
  203. HBITMAP hBmp;
  204. HBITMAP hPrev;
  205. RECT rFrame;
  206. PAINTSTRUCT ps;
  207. HDC hdcMem;
  208. BITMAP bmp;
  209. int x, y, dx, dy;
  210. /* Setup to do the painting
  211. */
  212. if(!GetUpdateRect(hWnd,NULL,FALSE))
  213. return 0L;
  214. BeginPaint(hWnd, &ps);
  215. GetClientRect(hWnd, &rFrame);
  216. hBmp = (HBITMAP)DF_GET_BMPHANDLE;
  217. if (hBmp)
  218. {
  219. hdcMem = CreateCompatibleDC(ps.hdc);
  220. if (!hdcMem)
  221. {
  222. EndPaint(hWnd, &ps);
  223. return -1L;
  224. }
  225. hPrev = SelectObject(hdcMem, hBmp);
  226. /* Get the size of the bitmap to center it in the frame.
  227. */
  228. GetObject(hBmp, sizeof(BITMAP), (LPTSTR)&bmp);
  229. if (bmp.bmWidth > (rFrame.right-rFrame.left))
  230. {
  231. x = 0;
  232. dx = rFrame.right-rFrame.left;
  233. }
  234. else
  235. {
  236. x = ((rFrame.right-rFrame.left - bmp.bmWidth) >> 1);
  237. dx = bmp.bmWidth;
  238. }
  239. if (bmp.bmHeight > (rFrame.bottom-rFrame.top))
  240. {
  241. y = 0;
  242. dy = rFrame.bottom-rFrame.top;
  243. }
  244. else
  245. {
  246. y = ((rFrame.bottom-rFrame.top - bmp.bmHeight) >> 1);
  247. dy = bmp.bmHeight;
  248. }
  249. /* Draw the frame & background, then blt in the bitmap.
  250. */
  251. dfDrawRect(ps.hdc, rFrame);
  252. BitBlt(ps.hdc, x, y, dx, dy, hdcMem, 0, 0, SRCCOPY);
  253. /* Cleanup and exit.
  254. */
  255. SelectObject(hdcMem, hPrev);
  256. DeleteDC(hdcMem);
  257. }
  258. else
  259. /* if no bitmap, just repaint the background.
  260. */
  261. dfDrawRect(ps.hdc, rFrame);
  262. EndPaint(hWnd, &ps);
  263. return 0L;
  264. }
  265. /*
  266. **************************************************************************
  267. * dfSetBitmap
  268. *
  269. * Description: Load a new bitmap into the control.
  270. *
  271. * Arguments:
  272. * HWND hWnd - handle to control window.
  273. * HBITMAP hBmpNew - handle to new bitmap.
  274. * HPALETTE hPalNew - handle to new bitmap's palette (Optional).
  275. * Returns: LONG
  276. * 0 for success; -1 if fails (for return by wndproc).
  277. *
  278. **************************************************************************
  279. */
  280. LONG PASCAL dfSetBitmap(HWND hWnd, HBITMAP hBmpNew, HPALETTE hPalNew)
  281. {
  282. HBITMAP hBmp;
  283. HANDLE hPrev;
  284. HANDLE hPrevNew;
  285. HDC hdcMem;
  286. HDC hdcNew;
  287. HDC hDC;
  288. RECT rFrame;
  289. int dx, dy;
  290. BITMAP bmp;
  291. /* Cleanup any existing bitmap & palette
  292. */
  293. hBmp = (HBITMAP)DF_GET_BMPHANDLE;
  294. if (hBmp)
  295. DeleteObject(hBmp);
  296. DF_SET_BMPHANDLE(0);
  297. DF_SET_BMPPAL(0);
  298. InvalidateRect(hWnd, NULL, TRUE);
  299. /* Copy the displayable portion of the bitmap into a private copy.
  300. */
  301. if (hBmpNew)
  302. {
  303. /* get all the req'd DC's etc.
  304. */
  305. hDC = GetDC(hWnd);
  306. hdcMem = CreateCompatibleDC(hDC);
  307. if (!hdcMem)
  308. {
  309. ReleaseDC(hWnd, hDC);
  310. return -1L;
  311. }
  312. hdcNew = CreateCompatibleDC(hDC);
  313. if (!hdcNew)
  314. {
  315. ReleaseDC(hWnd, hDC);
  316. return -1L;
  317. }
  318. GetObject(hBmpNew, sizeof(BITMAP), (LPTSTR)&bmp);
  319. hBmp = CreateCompatibleBitmap(hDC, bmp.bmWidth, bmp.bmHeight);
  320. if (!hBmp)
  321. {
  322. DeleteDC(hdcMem);
  323. DeleteDC(hdcNew);
  324. ReleaseDC(hWnd, hDC);
  325. return -1L;
  326. }
  327. hPrevNew = SelectObject(hdcNew, hBmpNew);
  328. hPrev = SelectObject(hdcMem, hBmp);
  329. /* figure out how much of the bitmap we need to copy.
  330. */
  331. GetClientRect(hWnd, &rFrame);
  332. if (bmp.bmWidth > (rFrame.right-rFrame.left))
  333. dx = rFrame.right-rFrame.left;
  334. else
  335. dx = bmp.bmWidth;
  336. if (bmp.bmHeight > (rFrame.bottom-rFrame.top))
  337. dy = rFrame.bottom-rFrame.top;
  338. else
  339. dy = bmp.bmHeight;
  340. /* copy the bitmap.
  341. */
  342. BitBlt(hdcMem, 0, 0, dx, dy, hdcNew, 0 , 0, SRCCOPY);
  343. /* cleanup
  344. */
  345. hBmp = SelectObject(hdcMem, hPrev);
  346. DF_SET_BMPHANDLE(hBmp);
  347. DeleteDC(hdcMem);
  348. SelectObject(hdcNew, hPrevNew);
  349. DeleteDC(hdcNew);
  350. ReleaseDC(hWnd, hDC);
  351. /* if a palette is handed in, store it too.
  352. */
  353. DF_SET_BMPPAL(hPalNew);
  354. }
  355. return 0L;
  356. }