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.

504 lines
12 KiB

  1. // sdktest.cpp : Defines the entry point for the application.
  2. //
  3. #include "sdktest.h"
  4. #include "..\gpinit.inc"
  5. #include <stdio.h>
  6. #define MAX_LOADSTRING 100
  7. // Global Variables:
  8. HINSTANCE hInst; // current instance
  9. HWND hWndMain = NULL;
  10. TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  11. TCHAR szWindowClass[MAX_LOADSTRING];// The title bar text
  12. // Foward declarations of functions included in this code module:
  13. ATOM MyRegisterClass(HINSTANCE hInstance);
  14. BOOL InitInstance(HINSTANCE, int);
  15. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  16. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  17. INT_PTR CALLBACK Settings(HWND, UINT, WPARAM, LPARAM);
  18. UINT_PTR timerID = 0;
  19. int timercount = 0;
  20. BOOL suspend = FALSE;
  21. typedef struct
  22. {
  23. HDC hdc;
  24. HBITMAP hbmpOffscreen;
  25. HBITMAP hbmpOld;
  26. BITMAPINFO bmi;
  27. void *pvBits;
  28. }
  29. OFFSCREENINFO;
  30. OFFSCREENINFO offscreenInfo = { 0 };
  31. void FreeOffscreen()
  32. {
  33. if (offscreenInfo.hdc)
  34. {
  35. SelectObject(offscreenInfo.hdc, offscreenInfo.hbmpOld);
  36. DeleteObject(offscreenInfo.hbmpOffscreen);
  37. DeleteDC(offscreenInfo.hdc);
  38. offscreenInfo.hdc = (HDC)NULL;
  39. offscreenInfo.hbmpOffscreen = (HBITMAP)NULL;
  40. offscreenInfo.hbmpOld = (HBITMAP)NULL;
  41. offscreenInfo.bmi.bmiHeader.biWidth = 0;
  42. offscreenInfo.bmi.bmiHeader.biHeight = 0;
  43. }
  44. }
  45. void ClearOffscreen()
  46. {
  47. if (offscreenInfo.hdc)
  48. {
  49. PatBlt(
  50. offscreenInfo.hdc,
  51. 0,
  52. 0,
  53. offscreenInfo.bmi.bmiHeader.biWidth,
  54. offscreenInfo.bmi.bmiHeader.biHeight,
  55. WHITENESS);
  56. }
  57. InvalidateRect(hWndMain, NULL, TRUE);
  58. }
  59. HDC GetOffscreen(HDC hDC, int width, int height)
  60. {
  61. HDC hdcResult = NULL;
  62. if (width > offscreenInfo.bmi.bmiHeader.biWidth ||
  63. height > offscreenInfo.bmi.bmiHeader.biHeight ||
  64. offscreenInfo.hdc == (HDC)NULL)
  65. {
  66. FreeOffscreen();
  67. offscreenInfo.bmi.bmiHeader.biSize = sizeof(offscreenInfo.bmi.bmiHeader);
  68. offscreenInfo.bmi.bmiHeader.biWidth = width;
  69. offscreenInfo.bmi.bmiHeader.biHeight = height;
  70. offscreenInfo.bmi.bmiHeader.biPlanes = 1;
  71. offscreenInfo.bmi.bmiHeader.biBitCount = 32;
  72. offscreenInfo.bmi.bmiHeader.biCompression = BI_RGB;
  73. offscreenInfo.bmi.bmiHeader.biSizeImage = 0;
  74. offscreenInfo.bmi.bmiHeader.biXPelsPerMeter = 10000;
  75. offscreenInfo.bmi.bmiHeader.biYPelsPerMeter = 10000;
  76. offscreenInfo.bmi.bmiHeader.biClrUsed = 0;
  77. offscreenInfo.bmi.bmiHeader.biClrImportant = 0;
  78. offscreenInfo.hbmpOffscreen = CreateDIBSection(
  79. hDC,
  80. &offscreenInfo.bmi,
  81. DIB_RGB_COLORS,
  82. &offscreenInfo.pvBits,
  83. NULL,
  84. 0);
  85. if (offscreenInfo.hbmpOffscreen)
  86. {
  87. offscreenInfo.hdc = CreateCompatibleDC(hDC);
  88. if (offscreenInfo.hdc)
  89. {
  90. offscreenInfo.hbmpOld = (HBITMAP)SelectObject(offscreenInfo.hdc, offscreenInfo.hbmpOffscreen);
  91. ClearOffscreen();
  92. }
  93. }
  94. }
  95. hdcResult = offscreenInfo.hdc;
  96. return hdcResult;
  97. }
  98. int APIENTRY WinMain(
  99. HINSTANCE hInstance,
  100. HINSTANCE hPrevInstance,
  101. LPSTR lpCmdLine,
  102. int nCmdShow)
  103. {
  104. MSG msg;
  105. HACCEL hAccelTable;
  106. if (!gGdiplusInitHelper.IsValid())
  107. return 0;
  108. // Initialize global strings
  109. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  110. LoadString(hInstance, IDC_SDKTEST, szWindowClass, MAX_LOADSTRING);
  111. MyRegisterClass(hInstance);
  112. // Perform application initialization:
  113. if (!InitInstance (hInstance, nCmdShow))
  114. {
  115. return FALSE;
  116. }
  117. hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SDKTEST);
  118. // Main message loop:
  119. while (GetMessage(&msg, NULL, 0, 0))
  120. {
  121. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  122. {
  123. TranslateMessage(&msg);
  124. DispatchMessage(&msg);
  125. }
  126. }
  127. FreeOffscreen();
  128. return (int)msg.wParam;
  129. }
  130. //
  131. // FUNCTION: MyRegisterClass()
  132. //
  133. // PURPOSE: Registers the window class.
  134. //
  135. // COMMENTS:
  136. //
  137. // This function and its usage is only necessary if you want this code
  138. // to be compatible with Win32 systems prior to the 'RegisterClassEx'
  139. // function that was added to Windows 95. It is important to call this function
  140. // so that the application will get 'well formed' small icons associated
  141. // with it.
  142. //
  143. ATOM MyRegisterClass(HINSTANCE hInstance)
  144. {
  145. WNDCLASSEX wcex;
  146. wcex.cbSize = sizeof(WNDCLASSEX);
  147. wcex.style = CS_HREDRAW | CS_VREDRAW;
  148. wcex.lpfnWndProc = WndProc;
  149. wcex.cbClsExtra = 0;
  150. wcex.cbWndExtra = 0;
  151. wcex.hInstance = hInstance;
  152. wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_SDKTEST);
  153. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  154. wcex.hbrBackground = (HBRUSH)NULL;
  155. wcex.lpszMenuName = (LPCSTR)IDC_SDKTEST;
  156. wcex.lpszClassName = szWindowClass;
  157. wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  158. return RegisterClassEx(&wcex);
  159. }
  160. //
  161. // FUNCTION: InitInstance(HANDLE, int)
  162. //
  163. // PURPOSE: Saves instance handle and creates main window
  164. //
  165. // COMMENTS:
  166. //
  167. // In this function, we save the instance handle in a global variable and
  168. // create and display the main program window.
  169. //
  170. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  171. {
  172. RECT rectDesktop;
  173. RECT rectWnd;
  174. HWND hWndDesktop = GetDesktopWindow();
  175. GetWindowRect(hWndDesktop, &rectDesktop);
  176. rectWnd = rectDesktop;
  177. rectWnd.top += 100;
  178. rectWnd.left += 100;
  179. rectWnd.right -= 100;
  180. rectWnd.bottom -= 100;
  181. hInst = hInstance; // Store instance handle in our global variable
  182. hWndMain = CreateWindow(
  183. szWindowClass,
  184. szTitle,
  185. WS_OVERLAPPEDWINDOW,
  186. rectWnd.left, rectWnd.top,
  187. rectWnd.right - rectWnd.left, rectWnd.bottom - rectWnd.top,
  188. NULL,
  189. NULL,
  190. hInstance,
  191. NULL);
  192. if (!hWndMain)
  193. {
  194. return FALSE;
  195. }
  196. ShowWindow(hWndMain, nCmdShow);
  197. UpdateWindow(hWndMain);
  198. timerID = SetTimer(hWndMain, 1, 10, NULL);
  199. return TRUE;
  200. }
  201. void DrawGraphics(HWND hWnd, HDC hDC, LPRECT lpRectDraw, LPRECT lpRectBounds)
  202. {
  203. RECT rectBounds = *lpRectDraw;
  204. Graphics *gr = NULL;
  205. gr = new Graphics(hDC);
  206. gr->ResetTransform();
  207. gr->SetPageUnit(UnitPixel);
  208. //===================================================================
  209. //
  210. // Insert your SDK code here \|/ \|/ \|/ \|/
  211. //
  212. //===================================================================
  213. //===================================================================
  214. //
  215. // Insert your SDK code here /|\ /|\ /|\ /|\
  216. //
  217. //===================================================================
  218. if (lpRectBounds)
  219. {
  220. *lpRectBounds = rectBounds;
  221. }
  222. delete gr;
  223. }
  224. LRESULT PaintWnd(HWND hWnd, HDC hDC)
  225. {
  226. RECT rectClient;
  227. RECT rectDraw;
  228. GetClientRect(hWnd, &rectClient);
  229. int width = rectClient.right - rectClient.left;
  230. int height = rectClient.bottom - rectClient.top;
  231. // Setup the drawing rectangle relative to the client
  232. rectDraw.left = 0;
  233. rectDraw.top = 0;
  234. rectDraw.right = (rectClient.right - rectClient.left);
  235. rectDraw.bottom = (rectClient.bottom - rectClient.top);
  236. // Now draw within this rectangle with GDI+ ...
  237. {
  238. // Render everything to an offscreen buffer instead of
  239. // directly to the display surface...
  240. HDC hdcOffscreen = NULL;
  241. int width, height;
  242. RECT rectOffscreen;
  243. width = rectDraw.right - rectDraw.left;
  244. height = rectDraw.bottom - rectDraw.top;
  245. rectOffscreen.left = 0;
  246. rectOffscreen.top = 0;
  247. rectOffscreen.right = width;
  248. rectOffscreen.bottom = height;
  249. hdcOffscreen = GetOffscreen(hDC, width, height);
  250. if (hdcOffscreen)
  251. {
  252. DrawGraphics(hWnd, hdcOffscreen, &rectOffscreen, NULL);
  253. StretchBlt(
  254. hDC,
  255. rectDraw.left,
  256. rectDraw.top,
  257. width,
  258. height,
  259. hdcOffscreen,
  260. 0,
  261. 0,
  262. width,
  263. height,
  264. SRCCOPY);
  265. }
  266. ReleaseDC(hWnd, hDC);
  267. }
  268. return 0;
  269. }
  270. static RECT rectLast = {0, 0, 0, 0};
  271. //
  272. // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  273. //
  274. // PURPOSE: Processes messages for the main window.
  275. //
  276. // WM_COMMAND - process the application menu
  277. // WM_PAINT - Paint the main window
  278. // WM_DESTROY - post a quit message and return
  279. //
  280. //
  281. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  282. {
  283. LRESULT lresult = 0;
  284. switch (message)
  285. {
  286. case WM_WINDOWPOSCHANGED:
  287. {
  288. timercount = 0;
  289. ClearOffscreen();
  290. lresult = DefWindowProc(hWnd, message, wParam, lParam);
  291. }
  292. break;
  293. case WM_LBUTTONDOWN:
  294. {
  295. timercount = 0;
  296. ClearOffscreen();
  297. }
  298. break;
  299. case WM_RBUTTONDOWN:
  300. {
  301. }
  302. break;
  303. case WM_TIMER:
  304. {
  305. }
  306. break;
  307. case WM_COMMAND:
  308. {
  309. int wmId, wmEvent;
  310. wmId = LOWORD(wParam);
  311. wmEvent = HIWORD(wParam);
  312. // Parse the menu selections:
  313. switch (wmId)
  314. {
  315. case IDM_SETTINGS:
  316. {
  317. suspend = TRUE;
  318. if (DialogBox(hInst, (LPCTSTR)IDD_SETTINGS, hWnd, Settings) == IDOK)
  319. {
  320. timercount = 0;
  321. ClearOffscreen();
  322. }
  323. InvalidateRect(hWnd, NULL, TRUE);
  324. suspend = FALSE;
  325. }
  326. break;
  327. case IDM_ABOUT:
  328. DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
  329. break;
  330. case IDM_EXIT:
  331. DestroyWindow(hWnd);
  332. break;
  333. default:
  334. lresult = DefWindowProc(hWnd, message, wParam, lParam);
  335. break;
  336. }
  337. }
  338. break;
  339. case WM_PAINT:
  340. {
  341. PAINTSTRUCT ps;
  342. HDC hdc;
  343. hdc = BeginPaint(hWnd, &ps);
  344. lresult = PaintWnd(hWnd, hdc);
  345. EndPaint(hWnd, &ps);
  346. }
  347. break;
  348. case WM_DESTROY:
  349. {
  350. PostQuitMessage(0);
  351. }
  352. break;
  353. default:
  354. lresult = DefWindowProc(hWnd, message, wParam, lParam);
  355. }
  356. return lresult;
  357. }
  358. // Mesage handler for about box.
  359. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  360. {
  361. LRESULT lresult = 0;
  362. switch (message)
  363. {
  364. case WM_INITDIALOG:
  365. lresult = TRUE;
  366. break;
  367. case WM_COMMAND:
  368. {
  369. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  370. {
  371. EndDialog(hDlg, LOWORD(wParam));
  372. lresult = TRUE;
  373. }
  374. }
  375. break;
  376. }
  377. return lresult;
  378. }
  379. // Message handler for settings dlg
  380. INT_PTR CALLBACK Settings(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  381. {
  382. LRESULT lresult = 0;
  383. switch (message)
  384. {
  385. case WM_INITDIALOG:
  386. {
  387. lresult = TRUE;
  388. }
  389. break;
  390. case WM_COMMAND:
  391. {
  392. switch (LOWORD(wParam))
  393. {
  394. case IDOK:
  395. {
  396. }
  397. // break; - fall through so the dialog closes!
  398. case IDCANCEL:
  399. {
  400. EndDialog(hDlg, LOWORD(wParam));
  401. lresult = TRUE;
  402. }
  403. break;
  404. }
  405. }
  406. break;
  407. }
  408. return lresult;
  409. }