PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap07/Checker3/Checker3.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 203 lines | 155 code | 42 blank | 6 comment | 9 complexity | a714c92c7ecbaaf207f4a18cb33159ff MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Checker3;
  6. import core.runtime;
  7. import core.thread;
  8. import core.stdc.config;
  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. enum DIVISIONS = 5;
  40. string childClass = "Checker3_Child";
  41. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  42. {
  43. string appName = "Checker3";
  44. HWND hwnd;
  45. MSG msg;
  46. WNDCLASS wndclass;
  47. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  48. wndclass.lpfnWndProc = &WndProc;
  49. wndclass.cbClsExtra = 0;
  50. wndclass.cbWndExtra = 0;
  51. wndclass.hInstance = hInstance;
  52. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  53. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  54. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  55. wndclass.lpszMenuName = NULL;
  56. wndclass.lpszClassName = appName.toUTF16z;
  57. if(!RegisterClass(&wndclass))
  58. {
  59. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  60. return 0;
  61. }
  62. wndclass.lpfnWndProc = &ChildWndProc;
  63. wndclass.cbWndExtra = c_long.sizeof;
  64. wndclass.hIcon = NULL;
  65. wndclass.lpszClassName = childClass.toUTF16z;
  66. if(!RegisterClass(&wndclass))
  67. {
  68. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  69. return 0;
  70. }
  71. hwnd = CreateWindow(appName.toUTF16z, "Checker3 Mouse Hit-Test Demo",
  72. WS_OVERLAPPEDWINDOW,
  73. CW_USEDEFAULT, CW_USEDEFAULT,
  74. CW_USEDEFAULT, CW_USEDEFAULT,
  75. NULL, NULL, hInstance, NULL);
  76. ShowWindow(hwnd, iCmdShow);
  77. UpdateWindow(hwnd);
  78. while(GetMessage(&msg, NULL, 0, 0))
  79. {
  80. TranslateMessage(&msg);
  81. DispatchMessage(&msg);
  82. }
  83. return msg.wParam;
  84. }
  85. extern (Windows)
  86. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  87. {
  88. scope (failure) assert(0);
  89. static HWND[DIVISIONS][DIVISIONS] hwndChild; // 25 windows
  90. int cxBlock, cyBlock, x, y;
  91. switch (message)
  92. {
  93. case WM_CREATE:
  94. {
  95. for (x = 0; x < DIVISIONS; x++)
  96. for (y = 0; y < DIVISIONS; y++)
  97. hwndChild[x][y] = CreateWindow(childClass.toUTF16z, NULL,
  98. WS_CHILDWINDOW | WS_VISIBLE,
  99. 0, 0, 0, 0,
  100. hwnd,
  101. cast(HMENU)(y << 8 | x), // ID
  102. cast(HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE),
  103. NULL);
  104. return 0;
  105. }
  106. case WM_SIZE:
  107. {
  108. cxBlock = LOWORD(lParam) / DIVISIONS;
  109. cyBlock = HIWORD(lParam) / DIVISIONS;
  110. for (x = 0; x < DIVISIONS; x++)
  111. for (y = 0; y < DIVISIONS; y++)
  112. MoveWindow(hwndChild[x][y],
  113. x * cxBlock, y * cyBlock, // upper left corner relative to this client area
  114. cxBlock, cyBlock, // width, height
  115. TRUE);
  116. // needs repainting
  117. return 0;
  118. }
  119. case WM_LBUTTONDOWN:
  120. MessageBeep(0);
  121. return 0;
  122. case WM_DESTROY:
  123. PostQuitMessage(0);
  124. return 0;
  125. default:
  126. }
  127. return DefWindowProc(hwnd, message, wParam, lParam);
  128. }
  129. extern (Windows)
  130. LRESULT ChildWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  131. {
  132. HDC hdc;
  133. PAINTSTRUCT ps;
  134. RECT rect;
  135. switch (message)
  136. {
  137. case WM_CREATE:
  138. SetWindowLongPtr(hwnd, 0, 0); // on/off flag in the extra space when we registered wnd class
  139. return 0;
  140. case WM_LBUTTONDOWN:
  141. SetWindowLongPtr(hwnd, 0, 1 ^ GetWindowLongPtr(hwnd, 0)); // toggle int
  142. InvalidateRect(hwnd, NULL, FALSE); // invalidate entire child window client area
  143. return 0;
  144. case WM_PAINT:
  145. hdc = BeginPaint(hwnd, &ps);
  146. GetClientRect(hwnd, &rect);
  147. Rectangle(hdc, 0, 0, rect.right, rect.bottom); // painting is now simplified
  148. // paint diagonal lines
  149. if (GetWindowLongPtr(hwnd, 0))
  150. {
  151. MoveToEx(hdc, 0, 0, NULL);
  152. LineTo(hdc, rect.right, rect.bottom);
  153. MoveToEx(hdc, 0, rect.bottom, NULL);
  154. LineTo(hdc, rect.right, 0);
  155. }
  156. EndPaint(hwnd, &ps);
  157. return 0;
  158. default:
  159. }
  160. return DefWindowProc(hwnd, message, wParam, lParam);
  161. }