PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap07/BlokOut2/BlokOut2.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 219 lines | 169 code | 45 blank | 5 comment | 10 complexity | d9ebc245eaa5d27f56490a74922c36ac MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module BlokOut2;
  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 = "BlokOut2";
  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. "Mouse Button 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. void DrawBoxOutline(HWND hwnd, POINT ptBeg, POINT ptEnd)
  80. {
  81. HDC hdc;
  82. hdc = GetDC(hwnd);
  83. SetROP2(hdc, R2_NOT);
  84. SelectObject(hdc, GetStockObject(NULL_BRUSH));
  85. Rectangle(hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y);
  86. ReleaseDC(hwnd, hdc);
  87. }
  88. extern(Windows)
  89. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  90. {
  91. scope (failure) assert(0);
  92. static BOOL fBlocking, fValidBox;
  93. static POINT ptBeg, ptEnd, ptBoxBeg, ptBoxEnd;
  94. HDC hdc;
  95. PAINTSTRUCT ps;
  96. switch (message)
  97. {
  98. case WM_LBUTTONDOWN:
  99. {
  100. ptBeg.x = ptEnd.x = cast(short)LOWORD(lParam);
  101. ptBeg.y = ptEnd.y = cast(short)HIWORD(lParam);
  102. DrawBoxOutline(hwnd, ptBeg, ptEnd);
  103. SetCapture(hwnd);
  104. SetCursor(LoadCursor(NULL, IDC_CROSS));
  105. fBlocking = TRUE;
  106. return 0;
  107. }
  108. case WM_MOUSEMOVE:
  109. {
  110. if (fBlocking)
  111. {
  112. SetCursor(LoadCursor(NULL, IDC_CROSS));
  113. DrawBoxOutline(hwnd, ptBeg, ptEnd);
  114. ptEnd.x = cast(short)LOWORD(lParam); // lParam will be negative pair of X and Y short,
  115. ptEnd.y = cast(short)HIWORD(lParam); // however LOWORD and HIWORD return ushort. Cast is needed to
  116. // preserve sign bit.
  117. DrawBoxOutline(hwnd, ptBeg, ptEnd);
  118. }
  119. return 0;
  120. }
  121. case WM_LBUTTONUP:
  122. {
  123. if (fBlocking)
  124. {
  125. DrawBoxOutline(hwnd, ptBeg, ptEnd);
  126. ptBoxBeg = ptBeg;
  127. ptBoxEnd.x = cast(short)LOWORD(lParam);
  128. ptBoxEnd.y = cast(short)HIWORD(lParam);
  129. ReleaseCapture();
  130. SetCursor(LoadCursor(NULL, IDC_ARROW));
  131. fBlocking = FALSE;
  132. fValidBox = TRUE;
  133. InvalidateRect(hwnd, NULL, TRUE);
  134. }
  135. return 0;
  136. }
  137. case WM_CHAR:
  138. {
  139. if (fBlocking && (wParam == '\x1B')) // i.e., Escape
  140. {
  141. DrawBoxOutline(hwnd, ptBeg, ptEnd);
  142. ReleaseCapture();
  143. SetCursor(LoadCursor(NULL, IDC_ARROW));
  144. fBlocking = FALSE;
  145. }
  146. return 0;
  147. }
  148. case WM_PAINT:
  149. {
  150. hdc = BeginPaint(hwnd, &ps);
  151. if (fValidBox)
  152. {
  153. SelectObject(hdc, GetStockObject(BLACK_BRUSH));
  154. Rectangle(hdc, ptBoxBeg.x, ptBoxBeg.y,
  155. ptBoxEnd.x, ptBoxEnd.y);
  156. }
  157. if (fBlocking)
  158. {
  159. SetROP2(hdc, R2_NOT);
  160. SelectObject(hdc, GetStockObject(NULL_BRUSH));
  161. Rectangle(hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y);
  162. }
  163. EndPaint(hwnd, &ps);
  164. return 0;
  165. }
  166. case WM_DESTROY:
  167. {
  168. PostQuitMessage(0);
  169. return 0;
  170. }
  171. default:
  172. }
  173. return DefWindowProc(hwnd, message, wParam, lParam);
  174. }