PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Samples/Chap07/Checker1/Checker1.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 172 lines | 132 code | 34 blank | 6 comment | 8 complexity | cd6d33f328b2ee99a9fe9ee84f6439cd MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Checker1;
  6. import core.runtime;
  7. import core.thread;
  8. import std.conv;
  9. import std.math;
  10. import std.range;
  11. import std.string;
  12. import std.utf;
  13. auto toUTF16z(S)(S s)
  14. {
  15. return toUTFz!(const(wchar)*)(s);
  16. }
  17. pragma(lib, "gdi32.lib");
  18. import core.sys.windows.windef;
  19. import core.sys.windows.winuser;
  20. import core.sys.windows.wingdi;
  21. extern (Windows)
  22. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  23. {
  24. int result;
  25. try
  26. {
  27. Runtime.initialize();
  28. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  29. Runtime.terminate();
  30. }
  31. catch (Throwable o)
  32. {
  33. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  34. result = 0;
  35. }
  36. return result;
  37. }
  38. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  39. {
  40. string appName = "Checker1";
  41. HWND hwnd;
  42. MSG msg;
  43. WNDCLASS wndclass;
  44. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  45. wndclass.lpfnWndProc = &WndProc;
  46. wndclass.cbClsExtra = 0;
  47. wndclass.cbWndExtra = 0;
  48. wndclass.hInstance = hInstance;
  49. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  50. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  51. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  52. wndclass.lpszMenuName = NULL;
  53. wndclass.lpszClassName = appName.toUTF16z;
  54. if (!RegisterClass(&wndclass))
  55. {
  56. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  57. return 0;
  58. }
  59. hwnd = CreateWindow(appName.toUTF16z, // window class name
  60. "Checker1 Mouse Hit-Test Demo", // window caption
  61. WS_OVERLAPPEDWINDOW, // window style
  62. CW_USEDEFAULT, // initial x position
  63. CW_USEDEFAULT, // initial y position
  64. CW_USEDEFAULT, // initial x size
  65. CW_USEDEFAULT, // initial y size
  66. NULL, // parent window handle
  67. NULL, // window menu handle
  68. hInstance, // program instance handle
  69. NULL); // creation parameters
  70. ShowWindow(hwnd, iCmdShow);
  71. UpdateWindow(hwnd);
  72. while (GetMessage(&msg, NULL, 0, 0))
  73. {
  74. TranslateMessage(&msg);
  75. DispatchMessage(&msg);
  76. }
  77. return msg.wParam;
  78. }
  79. extern (Windows)
  80. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  81. {
  82. scope (failure) assert(0);
  83. enum DIVISIONS = 5;
  84. static BOOL[DIVISIONS][DIVISIONS] fState;
  85. static int cxBlock, cyBlock;
  86. HDC hdc;
  87. int x, y; // id of selected block (mouse pixel index / block width)
  88. PAINTSTRUCT ps;
  89. RECT rect;
  90. switch (message)
  91. {
  92. case WM_SIZE:
  93. cxBlock = LOWORD(lParam) / DIVISIONS;
  94. cyBlock = HIWORD(lParam) / DIVISIONS;
  95. return 0;
  96. case WM_LBUTTONDOWN:
  97. x = LOWORD(lParam) / cxBlock;
  98. y = HIWORD(lParam) / cyBlock;
  99. if (x < DIVISIONS && y < DIVISIONS)
  100. {
  101. fState[x][y] ^= 1;
  102. rect.left = x * cxBlock;
  103. rect.top = y * cyBlock;
  104. rect.right = (x + 1) * cxBlock;
  105. rect.bottom = (y + 1) * cyBlock;
  106. InvalidateRect(hwnd, &rect, FALSE);
  107. }
  108. else
  109. MessageBeep(0);
  110. return 0;
  111. case WM_PAINT:
  112. hdc = BeginPaint(hwnd, &ps);
  113. for (x = 0; x < DIVISIONS; x++)
  114. {
  115. for (y = 0; y < DIVISIONS; y++)
  116. {
  117. Rectangle(hdc, x * cxBlock, y * cyBlock,
  118. (x + 1) * cxBlock, (y + 1) * cyBlock);
  119. if (fState[x][y])
  120. {
  121. // Draw '\'
  122. MoveToEx(hdc, x * cxBlock, y * cyBlock, NULL);
  123. LineTo(hdc, (x + 1) * cxBlock, (y + 1) * cyBlock);
  124. // Draw '/'
  125. MoveToEx(hdc, x * cxBlock, (y + 1) * cyBlock, NULL);
  126. LineTo(hdc, (x + 1) * cxBlock, y * cyBlock);
  127. }
  128. }
  129. }
  130. EndPaint(hwnd, &ps);
  131. return 0;
  132. case WM_DESTROY:
  133. PostQuitMessage(0);
  134. return 0;
  135. default:
  136. }
  137. return DefWindowProc(hwnd, message, wParam, lParam);
  138. }