PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap07/BlokOut1/BlokOut1.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 216 lines | 166 code | 45 blank | 5 comment | 10 complexity | 48ca7ac75a0264646255ea2736d2973f MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module BlokOut1;
  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 = "BlokOut1";
  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. SetCursor(LoadCursor(NULL, IDC_CROSS));
  104. fBlocking = TRUE;
  105. return 0;
  106. }
  107. case WM_MOUSEMOVE:
  108. {
  109. if (fBlocking)
  110. {
  111. SetCursor(LoadCursor(NULL, IDC_CROSS));
  112. DrawBoxOutline(hwnd, ptBeg, ptEnd);
  113. ptEnd.x = cast(short)LOWORD(lParam); // lParam will be negative pair of X and Y short,
  114. ptEnd.y = cast(short)HIWORD(lParam); // however LOWORD and HIWORD return ushort. Cast is needed to
  115. // preserve sign bit.
  116. DrawBoxOutline(hwnd, ptBeg, ptEnd);
  117. }
  118. return 0;
  119. }
  120. case WM_LBUTTONUP:
  121. {
  122. if (fBlocking)
  123. {
  124. DrawBoxOutline(hwnd, ptBeg, ptEnd);
  125. ptBoxBeg = ptBeg;
  126. ptBoxEnd.x = cast(short)LOWORD(lParam);
  127. ptBoxEnd.y = cast(short)HIWORD(lParam);
  128. SetCursor(LoadCursor(NULL, IDC_ARROW));
  129. fBlocking = FALSE;
  130. fValidBox = TRUE;
  131. InvalidateRect(hwnd, NULL, TRUE);
  132. }
  133. return 0;
  134. }
  135. case WM_CHAR:
  136. {
  137. if (fBlocking && (wParam == '\x1B')) // i.e., Escape
  138. {
  139. DrawBoxOutline(hwnd, ptBeg, ptEnd);
  140. SetCursor(LoadCursor(NULL, IDC_ARROW));
  141. fBlocking = FALSE;
  142. }
  143. return 0;
  144. }
  145. case WM_PAINT:
  146. {
  147. hdc = BeginPaint(hwnd, &ps);
  148. if (fValidBox)
  149. {
  150. SelectObject(hdc, GetStockObject(BLACK_BRUSH));
  151. Rectangle(hdc, ptBoxBeg.x, ptBoxBeg.y,
  152. ptBoxEnd.x, ptBoxEnd.y);
  153. }
  154. if (fBlocking)
  155. {
  156. SetROP2(hdc, R2_NOT);
  157. SelectObject(hdc, GetStockObject(NULL_BRUSH));
  158. Rectangle(hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y);
  159. }
  160. EndPaint(hwnd, &ps);
  161. return 0;
  162. }
  163. case WM_DESTROY:
  164. {
  165. PostQuitMessage(0);
  166. return 0;
  167. }
  168. default:
  169. }
  170. return DefWindowProc(hwnd, message, wParam, lParam);
  171. }