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.

439 lines
9.7 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: wndstuff.cpp
  3. *
  4. * This file contains the code to support a simple window that has
  5. * a menu with a single item called "Test". When "Test" is selected
  6. * vTest(HWND) is called.
  7. *
  8. * Created: 09-Dec-1992 10:44:31
  9. * Author: Kirk Olynyk [kirko]
  10. *
  11. * Copyright (c) 1991 Microsoft Corporation
  12. *
  13. \**************************************************************************/
  14. // for Win95 compile
  15. #undef UNICODE
  16. #undef _UNICODE
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <objbase.h>
  20. #include <tchar.h>
  21. //
  22. // Where is IStream included from?
  23. //
  24. #define IStream int
  25. #include <gdiplus.h>
  26. using namespace Gdiplus;
  27. #include "wndstuff.h"
  28. HINSTANCE ghInstance;
  29. HWND ghwndMain;
  30. HWND ghwndDebug;
  31. HWND ghwndList;
  32. HBRUSH ghbrWhite;
  33. VOID WINAPI
  34. AltDebugEvent(
  35. DebugEventLevel level,
  36. CHAR *message)
  37. {
  38. OutputDebugStringA("AltDebugEvent: ");
  39. OutputDebugStringA(message);
  40. if (level == DebugEventLevelFatal)
  41. {
  42. DebugBreak();
  43. }
  44. }
  45. GdiplusStartupOutput gGpSto;
  46. // Because we have global initializers which call GDI+, GDI+ needs to be
  47. // initialized before, and destroyed after, those constructors and destructors.
  48. // Here goes:
  49. #pragma warning( push )
  50. #pragma warning( disable : 4073 )
  51. #pragma code_seg( "MySeg" )
  52. #pragma init_seg( lib )
  53. class GdiplusInitHelper
  54. {
  55. public:
  56. GdiplusInitHelper() : gpToken(0), Valid(FALSE)
  57. {
  58. // Use the non-defaults, to test the alternative code-path.
  59. GdiplusStartupInput sti(AltDebugEvent, TRUE, TRUE);
  60. if (GdiplusStartup(&gpToken, &sti, &gGpSto) == Ok)
  61. {
  62. Valid = TRUE;
  63. }
  64. else
  65. {
  66. MessageBox(0, _T("Engine didn't initialize"), _T("Uh oh"), MB_OK);
  67. }
  68. }
  69. ~GdiplusInitHelper()
  70. {
  71. if (Valid)
  72. {
  73. GdiplusShutdown(gpToken);
  74. }
  75. }
  76. BOOL IsValid() { return Valid; }
  77. private:
  78. ULONG_PTR gpToken;
  79. BOOL Valid;
  80. };
  81. GdiplusInitHelper gGdiplusInitHelper;
  82. #pragma code_seg()
  83. #pragma warning( pop )
  84. /***************************************************************************\
  85. * lMainWindowProc(hwnd, message, wParam, lParam)
  86. *
  87. * Processes all messages for the main window.
  88. *
  89. * History:
  90. * 04-07-91 -by- KentD
  91. * Wrote it.
  92. \***************************************************************************/
  93. LONG_PTR
  94. lMainWindowProc(
  95. HWND hwnd,
  96. UINT message,
  97. WPARAM wParam,
  98. LPARAM lParam
  99. )
  100. {
  101. PAINTSTRUCT ps;
  102. switch (message)
  103. {
  104. case WM_COMMAND:
  105. switch(LOWORD(wParam))
  106. {
  107. case MM_TEST:
  108. Test(hwnd);
  109. break;
  110. default:
  111. break;
  112. }
  113. break;
  114. case WM_DESTROY:
  115. DeleteObject(ghbrWhite);
  116. PostQuitMessage(0);
  117. return(DefWindowProc(hwnd, message, wParam, lParam));
  118. default:
  119. return(DefWindowProc(hwnd, message, wParam, lParam));
  120. }
  121. return(0);
  122. }
  123. /******************************Public*Routine******************************\
  124. * DebugWndProc
  125. *
  126. * List box is maintained here.
  127. *
  128. \**************************************************************************/
  129. LONG_PTR FAR PASCAL DebugWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  130. {
  131. RECT rcl;
  132. HDC hdc;
  133. LONG_PTR lRet = 0;
  134. // Process window message.
  135. switch (message)
  136. {
  137. case WM_SIZE:
  138. lRet = DefWindowProc(ghwndList, message, wParam, lParam);
  139. GetClientRect(ghwndMain, &rcl);
  140. MoveWindow(
  141. ghwndList,
  142. rcl.left, rcl.top,
  143. (rcl.right - rcl.left), (rcl.bottom - rcl.top),
  144. TRUE
  145. );
  146. UpdateWindow(ghwndList);
  147. break;
  148. case WM_DESTROY:
  149. PostQuitMessage(0);
  150. break;
  151. default:
  152. lRet = DefWindowProc(hwnd, message, wParam, lParam);
  153. break;
  154. }
  155. return lRet;
  156. }
  157. /******************************Public*Routine******************************\
  158. * LBprintf
  159. *
  160. * ListBox printf implementation.
  161. *
  162. * History:
  163. * 15-Dec-1994 -by- Gilman Wong [gilmanw]
  164. * Wrote it.
  165. \**************************************************************************/
  166. void LBprintf(PCH msg, ...)
  167. {
  168. if (ghwndList)
  169. {
  170. va_list ap;
  171. char buffer[256];
  172. va_start(ap, msg);
  173. vsprintf(buffer, msg, ap);
  174. SendMessage(ghwndList, LB_ADDSTRING, (WPARAM) 0, (LPARAM) buffer);
  175. SendMessage(ghwndList, WM_SETREDRAW, (WPARAM) TRUE, (LPARAM) 0);
  176. InvalidateRect(ghwndList, NULL, TRUE);
  177. UpdateWindow(ghwndList);
  178. va_end(ap);
  179. }
  180. }
  181. /******************************Public*Routine******************************\
  182. * LBreset
  183. *
  184. * Reset ListBox state (clear).
  185. *
  186. * History:
  187. * 15-Dec-1994 -by- Gilman Wong [gilmanw]
  188. * Wrote it.
  189. \**************************************************************************/
  190. void LBreset()
  191. {
  192. if (ghwndList)
  193. SendMessage(ghwndList, LB_RESETCONTENT, (WPARAM) FALSE, (LPARAM) 0);
  194. }
  195. /***************************************************************************\
  196. * bInitApp()
  197. *
  198. * Initializes app.
  199. *
  200. * History:
  201. * 04-07-91 -by- KentD
  202. * Wrote it.
  203. \***************************************************************************/
  204. BOOL bInitApp(BOOL debug)
  205. {
  206. WNDCLASS wc;
  207. ghbrWhite = CreateSolidBrush(RGB(0xFF,0xFF,0xFF));
  208. wc.style = 0;
  209. wc.lpfnWndProc = lMainWindowProc;
  210. wc.cbClsExtra = 0;
  211. wc.cbWndExtra = 0;
  212. wc.hInstance = ghInstance;
  213. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  214. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  215. wc.hbrBackground = ghbrWhite;
  216. wc.lpszMenuName = _T("MainMenu");
  217. wc.lpszClassName = _T("TestClass");
  218. if (!RegisterClass(&wc))
  219. {
  220. return(FALSE);
  221. }
  222. ghwndMain =
  223. CreateWindowEx(
  224. 0,
  225. _T("TestClass"),
  226. _T("Win32 Test"),
  227. WS_OVERLAPPED |
  228. WS_CAPTION |
  229. WS_BORDER |
  230. WS_THICKFRAME |
  231. WS_MAXIMIZEBOX |
  232. WS_MINIMIZEBOX |
  233. WS_CLIPCHILDREN |
  234. WS_VISIBLE |
  235. WS_SYSMENU,
  236. 80,
  237. 70,
  238. 500,
  239. 500,
  240. NULL,
  241. NULL,
  242. ghInstance,
  243. NULL);
  244. if (ghwndMain == NULL)
  245. {
  246. return(FALSE);
  247. }
  248. if (debug)
  249. {
  250. RECT rcl;
  251. memset(&wc, 0, sizeof(wc));
  252. wc.style = 0;
  253. wc.lpfnWndProc = DebugWndProc;
  254. wc.cbClsExtra = 0;
  255. wc.cbWndExtra = 0;
  256. wc.hInstance = ghInstance;
  257. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  258. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  259. wc.hbrBackground = ghbrWhite;
  260. wc.lpszClassName = "DebugWClass";
  261. RegisterClass(&wc);
  262. ghwndDebug = CreateWindow(
  263. "DebugWClass",
  264. "Debug output",
  265. WS_OVERLAPPEDWINDOW|WS_MAXIMIZE,
  266. 600,
  267. 70,
  268. 300,
  269. 500,
  270. NULL,
  271. NULL,
  272. ghInstance,
  273. NULL
  274. );
  275. if (ghwndDebug)
  276. {
  277. ShowWindow(ghwndDebug, SW_NORMAL);
  278. UpdateWindow(ghwndDebug);
  279. // Create the list box to fill the main window.
  280. GetClientRect(ghwndDebug, &rcl);
  281. ghwndList = CreateWindow(
  282. "LISTBOX",
  283. "Debug output",
  284. WS_CHILD | WS_VISIBLE | WS_VSCROLL
  285. | WS_HSCROLL | LBS_NOINTEGRALHEIGHT,
  286. rcl.left, rcl.top,
  287. (rcl.right - rcl.left), (rcl.bottom - rcl.top),
  288. ghwndDebug,
  289. NULL,
  290. ghInstance,
  291. NULL
  292. );
  293. if (ghwndList)
  294. {
  295. SendMessage(
  296. ghwndList,
  297. WM_SETFONT,
  298. (WPARAM) GetStockObject(ANSI_FIXED_FONT),
  299. (LPARAM) FALSE
  300. );
  301. LBreset();
  302. ShowWindow(ghwndList, SW_NORMAL);
  303. UpdateWindow(ghwndList);
  304. }
  305. }
  306. }
  307. SetFocus(ghwndMain);
  308. return(TRUE);
  309. }
  310. /***************************************************************************\
  311. * main(argc, argv[])
  312. *
  313. * Sets up the message loop.
  314. *
  315. * History:
  316. * 04-07-91 -by- KentD
  317. * Wrote it.
  318. \***************************************************************************/
  319. _cdecl
  320. main(
  321. INT argc,
  322. PCHAR argv[])
  323. {
  324. MSG msg;
  325. HACCEL haccel;
  326. CHAR* pSrc;
  327. CHAR* pDst;
  328. BOOL wantDebugWindow = FALSE;
  329. if (!gGdiplusInitHelper.IsValid())
  330. {
  331. return 0;
  332. }
  333. CoInitialize(NULL);
  334. // Parse arguments
  335. for (argc--, argv++ ; argc && '-' == **argv ; argc--, argv++ )
  336. {
  337. switch ( *(++(*argv)) )
  338. {
  339. case 'd':
  340. case 'D':
  341. wantDebugWindow = TRUE;
  342. break;
  343. }
  344. }
  345. ghInstance = GetModuleHandle(NULL);
  346. if (!bInitApp(wantDebugWindow))
  347. {
  348. return(0);
  349. }
  350. haccel = LoadAccelerators(ghInstance, MAKEINTRESOURCE(1));
  351. ULONG_PTR notifyToken;
  352. gGpSto.NotificationHook(&notifyToken);
  353. while (GetMessage(&msg, NULL, 0, 0))
  354. {
  355. if (!TranslateAccelerator(msg.hwnd, haccel, &msg))
  356. {
  357. TranslateMessage(&msg);
  358. DispatchMessage(&msg);
  359. }
  360. }
  361. gGpSto.NotificationUnhook(notifyToken);
  362. CoUninitialize();
  363. return(1);
  364. }