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.

253 lines
5.9 KiB

  1. /*
  2. * ICONBOX.C
  3. *
  4. * Implemenatation of an IconBox control for OLE 2.0 UI dialogs that we'll
  5. * use wherever a dialog needs an icon/label display. Through the control's
  6. * interface we can change the image or control label visibility.
  7. *
  8. * The IconBox discusses images in CF_METAFILEPICT format. When drawing
  9. * such a metafile, the entire aspect is centered in the IconBox, so long
  10. * labels are chopped at either end.
  11. *
  12. * Copyright (c)1992 Microsoft Corporation, All Right Reserved
  13. */
  14. #define STRICT 1
  15. #include "ole2ui.h"
  16. #include "iconbox.h"
  17. //Flag indicating if we've registered the class
  18. static BOOL fRegistered=FALSE;
  19. /*
  20. * FIconBoxInitialize
  21. *
  22. * Purpose:
  23. * Registers the IconBox control class.
  24. *
  25. * Parameters:
  26. * hInst HINSTANCE instance of the DLL.
  27. *
  28. * hPrevInst HINSTANCE of the previous instance. Used to
  29. * determine whether to register window classes or not.
  30. *
  31. * lpszClassName LPSTR containing the class name to register the
  32. * IconBox control class with.
  33. *
  34. * Return Value:
  35. * BOOL TRUE if all initialization succeeded, FALSE otherwise.
  36. */
  37. BOOL FIconBoxInitialize(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR lpszClassName)
  38. {
  39. WNDCLASS wc;
  40. // Only register class if we're the first instance
  41. if (hPrevInst)
  42. fRegistered = TRUE;
  43. else
  44. {
  45. // Static flag fRegistered guards against calling this function more
  46. // than once
  47. if (!fRegistered)
  48. {
  49. wc.lpfnWndProc =IconBoxWndProc;
  50. wc.cbClsExtra =0;
  51. wc.cbWndExtra =CBICONBOXWNDEXTRA;
  52. wc.hInstance =hInst;
  53. wc.hIcon =NULL;
  54. wc.hCursor =LoadCursor(NULL, IDC_ARROW);
  55. wc.hbrBackground =(HBRUSH)NULL;
  56. wc.lpszMenuName =NULL;
  57. wc.lpszClassName =lpszClassName;
  58. wc.style =CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
  59. fRegistered=RegisterClass(&wc);
  60. }
  61. }
  62. return fRegistered;
  63. }
  64. /*
  65. * IconBoxUninitialize
  66. *
  67. * Purpose:
  68. * Cleans up anything done in FIconBoxInitialize. Currently there is
  69. * nothing, but we do this for symmetry.
  70. *
  71. * Parameters:
  72. * None
  73. *
  74. * Return Value:
  75. * None
  76. */
  77. void IconBoxUninitialize(void)
  78. {
  79. //Nothing to do.
  80. return;
  81. }
  82. /*
  83. * IconBoxWndProc
  84. *
  85. * Purpose:
  86. * Window Procedure for the IconBox custom control. Only handles
  87. * WM_CREATE, WM_PAINT, and private messages to manipulate the image.
  88. *
  89. * Parameters:
  90. * Standard
  91. *
  92. * Return Value:
  93. * Standard
  94. */
  95. LONG CALLBACK EXPORT IconBoxWndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  96. {
  97. HGLOBAL hMF=NULL;
  98. PAINTSTRUCT ps;
  99. HDC hDC;
  100. RECT rc;
  101. //Handle standard Windows messages.
  102. switch (iMsg)
  103. {
  104. case WM_CREATE:
  105. SetWindowLong(hWnd, IBWW_HIMAGE, 0);
  106. SetWindowWord(hWnd, IBWW_FLABEL, TRUE);
  107. return 0L;
  108. case WM_ERASEBKGND:
  109. {
  110. HBRUSH hBrush;
  111. RECT Rect;
  112. #if defined( WIN32 )
  113. POINT point;
  114. #endif
  115. GetClientRect(hWnd, &Rect);
  116. #if defined( WIN32 )
  117. hBrush = (HBRUSH)SendMessage(GetParent(hWnd),
  118. WM_CTLCOLORDLG,
  119. wParam,
  120. (LPARAM)GetParent(hWnd));
  121. #else
  122. hBrush = (HBRUSH)SendMessage(GetParent(hWnd),
  123. WM_CTLCOLOR,
  124. wParam,
  125. MAKELPARAM(GetParent(hWnd), CTLCOLOR_DLG));
  126. #endif
  127. if (!hBrush)
  128. return FALSE;
  129. UnrealizeObject(hBrush);
  130. #if defined( WIN32 )
  131. SetBrushOrgEx((HDC)wParam, 0, 0, &point);
  132. #else
  133. SetBrushOrg((HDC)wParam, 0, 0);
  134. #endif
  135. FillRect((HDC)wParam, &Rect, hBrush);
  136. return TRUE;
  137. }
  138. case WM_PAINT:
  139. hMF=(HGLOBAL)GetWindowLong(hWnd, IBWW_HIMAGE);
  140. //BeginPaint and EndPaint clear us even if hMF is NULL.
  141. hDC=BeginPaint(hWnd, &ps);
  142. if (NULL!=hMF)
  143. {
  144. BOOL fLabel;
  145. //Now we get to paint the metafile, centered in our rect.
  146. GetClientRect(hWnd, &rc);
  147. /*
  148. * If we're doing icon only, then place the metafile
  149. * at the center of our box minus half the icon width.
  150. * Top is top.
  151. */
  152. fLabel=GetWindowWord(hWnd, IBWW_FLABEL);
  153. //Go draw where we decided to place it.
  154. OleUIMetafilePictIconDraw(hDC, &rc, hMF, !fLabel);
  155. }
  156. EndPaint(hWnd, &ps);
  157. break;
  158. case IBXM_IMAGESET:
  159. /*
  160. * wParam contains the new handle.
  161. * lParam is a flag to delete the old or not.
  162. */
  163. hMF=(HGLOBAL)SetWindowLong(hWnd, IBWW_HIMAGE, wParam);
  164. InvalidateRect(hWnd, NULL, TRUE);
  165. UpdateWindow(hWnd);
  166. //Delete the old handle if requested
  167. if (0L!=lParam)
  168. {
  169. OleUIMetafilePictIconFree(hMF);
  170. hMF=NULL;
  171. }
  172. return (LONG)(UINT)hMF;
  173. case IBXM_IMAGEGET:
  174. //Return the current index.
  175. hMF=(HGLOBAL)GetWindowLong(hWnd, IBWW_HIMAGE);
  176. return (LONG)(UINT)hMF;
  177. case IBXM_IMAGEFREE:
  178. //Free up whatever we're holding.
  179. hMF=(HGLOBAL)GetWindowLong(hWnd, IBWW_HIMAGE);
  180. OleUIMetafilePictIconFree(hMF);
  181. return 1L;
  182. case IBXM_LABELENABLE:
  183. //wParam has the new flag, returns the previous flag.
  184. return (LONG)SetWindowWord(hWnd, IBWW_FLABEL, (WORD)wParam);
  185. default:
  186. return DefWindowProc(hWnd, iMsg, wParam, lParam);
  187. }
  188. return 0L;
  189. }