PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap07/Checker2/Checker2.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 231 lines | 177 code | 50 blank | 4 comment | 9 complexity | ece508ff63cd7e1d6dcdd127077e7302 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Checker2;
  6. import core.runtime;
  7. import core.thread;
  8. import std.algorithm : max, min;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.utf;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. pragma(lib, "gdi32.lib");
  19. import core.sys.windows.windef;
  20. import core.sys.windows.winuser;
  21. import core.sys.windows.wingdi;
  22. extern (Windows)
  23. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  24. {
  25. int result;
  26. try
  27. {
  28. Runtime.initialize();
  29. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  30. Runtime.terminate();
  31. }
  32. catch (Throwable o)
  33. {
  34. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  35. result = 0;
  36. }
  37. return result;
  38. }
  39. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  40. {
  41. string appName = "Checker2";
  42. HWND hwnd;
  43. MSG msg;
  44. WNDCLASS wndclass;
  45. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  46. wndclass.lpfnWndProc = &WndProc;
  47. wndclass.cbClsExtra = 0;
  48. wndclass.cbWndExtra = 0;
  49. wndclass.hInstance = hInstance;
  50. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  51. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  52. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  53. wndclass.lpszMenuName = NULL;
  54. wndclass.lpszClassName = appName.toUTF16z;
  55. if (!RegisterClass(&wndclass))
  56. {
  57. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  58. return 0;
  59. }
  60. hwnd = CreateWindow(appName.toUTF16z, // window class name
  61. "Checker2 Mouse Hit-Test Demo", // window caption
  62. WS_OVERLAPPEDWINDOW, // window style
  63. CW_USEDEFAULT, // initial x position
  64. CW_USEDEFAULT, // initial y position
  65. CW_USEDEFAULT, // initial x size
  66. CW_USEDEFAULT, // initial y size
  67. NULL, // parent window handle
  68. NULL, // window menu handle
  69. hInstance, // program instance handle
  70. NULL); // creation parameters
  71. ShowWindow(hwnd, iCmdShow);
  72. UpdateWindow(hwnd);
  73. while (GetMessage(&msg, NULL, 0, 0))
  74. {
  75. TranslateMessage(&msg);
  76. DispatchMessage(&msg);
  77. }
  78. return msg.wParam;
  79. }
  80. extern(Windows)
  81. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  82. {
  83. scope (failure) assert(0);
  84. enum DIVISIONS = 5;
  85. static BOOL[DIVISIONS][DIVISIONS] fState;
  86. static int cxBlock, cyBlock;
  87. HDC hdc;
  88. int x, y;
  89. PAINTSTRUCT ps;
  90. POINT point;
  91. RECT rect;
  92. switch (message)
  93. {
  94. case WM_SIZE:
  95. cxBlock = LOWORD(lParam) / DIVISIONS;
  96. cyBlock = HIWORD(lParam) / DIVISIONS;
  97. return 0;
  98. case WM_SETFOCUS:
  99. ShowCursor(TRUE);
  100. return 0;
  101. case WM_KILLFOCUS:
  102. ShowCursor(FALSE);
  103. return 0;
  104. case WM_KEYDOWN:
  105. GetCursorPos(&point);
  106. ScreenToClient(hwnd, &point);
  107. x = max(0, min(DIVISIONS - 1, point.x / cxBlock));
  108. y = max(0, min(DIVISIONS - 1, point.y / cyBlock));
  109. switch (wParam)
  110. {
  111. case VK_UP:
  112. y--;
  113. break;
  114. case VK_DOWN:
  115. y++;
  116. break;
  117. case VK_LEFT:
  118. x--;
  119. break;
  120. case VK_RIGHT:
  121. x++;
  122. break;
  123. case VK_HOME:
  124. x = y = 0;
  125. break;
  126. case VK_END:
  127. x = y = DIVISIONS - 1;
  128. break;
  129. case VK_RETURN:
  130. case VK_SPACE:
  131. SendMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON,
  132. MAKELONG(cast(short)(x * cxBlock), cast(short)(y * cyBlock)));
  133. break;
  134. default:
  135. }
  136. x = (x + DIVISIONS) % DIVISIONS; // if #id is off screen, reset it to the next position
  137. y = (y + DIVISIONS) % DIVISIONS;
  138. point.x = (x * cxBlock) + (cxBlock / 2); // set location to center of block #id
  139. point.y = (y * cyBlock) + (cyBlock / 2);
  140. ClientToScreen(hwnd, &point);
  141. SetCursorPos(point.x, point.y);
  142. return 0;
  143. case WM_LBUTTONDOWN:
  144. x = LOWORD(lParam) / cxBlock;
  145. y = HIWORD(lParam) / cyBlock;
  146. if (x < DIVISIONS && y < DIVISIONS)
  147. {
  148. fState[x][y] ^= 1;
  149. rect.left = x * cxBlock;
  150. rect.top = y * cyBlock;
  151. rect.right = (x + 1) * cxBlock;
  152. rect.bottom = (y + 1) * cyBlock;
  153. InvalidateRect(hwnd, &rect, FALSE);
  154. }
  155. else
  156. MessageBeep(0);
  157. return 0;
  158. case WM_PAINT:
  159. hdc = BeginPaint(hwnd, &ps);
  160. for(x = 0; x < DIVISIONS; x++)
  161. for(y = 0; y < DIVISIONS; y++)
  162. {
  163. Rectangle(hdc, x * cxBlock, y * cyBlock,
  164. (x + 1) * cxBlock, (y + 1) * cyBlock);
  165. if (fState[x][y])
  166. {
  167. MoveToEx(hdc, x * cxBlock, y * cyBlock, NULL);
  168. LineTo (hdc, (x + 1) * cxBlock, (y + 1) * cyBlock);
  169. MoveToEx(hdc, x * cxBlock, (y + 1) * cyBlock, NULL);
  170. LineTo (hdc, (x + 1) * cxBlock, y * cyBlock);
  171. }
  172. }
  173. EndPaint(hwnd, &ps);
  174. return 0;
  175. case WM_DESTROY:
  176. PostQuitMessage(0);
  177. return 0;
  178. default:
  179. }
  180. return DefWindowProc(hwnd, message, wParam, lParam);
  181. }