PageRenderTime 62ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap14/Sketch/Sketch.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 233 lines | 176 code | 53 blank | 4 comment | 11 complexity | bb8ff7836a51137639af41d08f651da2 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Sketch;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.algorithm;
  10. import std.conv;
  11. import std.math;
  12. import std.range;
  13. import std.string;
  14. import std.utf;
  15. auto toUTF16z(S)(S s)
  16. {
  17. return toUTFz!(const(wchar)*)(s);
  18. }
  19. pragma(lib, "gdi32.lib");
  20. import core.sys.windows.windef;
  21. import core.sys.windows.winuser;
  22. import core.sys.windows.wingdi;
  23. import core.sys.windows.winbase;
  24. string appName = "Sketch";
  25. string description = "Sketch";
  26. HINSTANCE hinst;
  27. extern (Windows)
  28. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  29. {
  30. int result;
  31. try
  32. {
  33. Runtime.initialize();
  34. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  35. Runtime.terminate();
  36. }
  37. catch (Throwable o)
  38. {
  39. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  40. result = 0;
  41. }
  42. return result;
  43. }
  44. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  45. {
  46. hinst = hInstance;
  47. HACCEL hAccel;
  48. HWND hwnd;
  49. MSG msg;
  50. WNDCLASS wndclass;
  51. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  52. wndclass.lpfnWndProc = &WndProc;
  53. wndclass.cbClsExtra = 0;
  54. wndclass.cbWndExtra = 0;
  55. wndclass.hInstance = hInstance;
  56. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  57. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  58. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  59. wndclass.lpszMenuName = appName.toUTF16z;
  60. wndclass.lpszClassName = appName.toUTF16z;
  61. if (!RegisterClass(&wndclass))
  62. {
  63. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  64. return 0;
  65. }
  66. hwnd = CreateWindow(appName.toUTF16z, // window class name
  67. description.toUTF16z, // window caption
  68. WS_OVERLAPPEDWINDOW, // window style
  69. CW_USEDEFAULT, // initial x position
  70. CW_USEDEFAULT, // initial y position
  71. CW_USEDEFAULT, // initial x size
  72. CW_USEDEFAULT, // initial y size
  73. NULL, // parent window handle
  74. NULL, // window menu handle
  75. hInstance, // program instance handle
  76. NULL); // creation parameters
  77. ShowWindow(hwnd, iCmdShow);
  78. UpdateWindow(hwnd);
  79. while (GetMessage(&msg, NULL, 0, 0))
  80. {
  81. TranslateMessage(&msg);
  82. DispatchMessage(&msg);
  83. }
  84. return msg.wParam;
  85. }
  86. void GetLargestDisplayMode(out int pcxBitmap, out int pcyBitmap)
  87. {
  88. DEVMODE devmode;
  89. int iModeNum = 0;
  90. devmode.dmSize = (DEVMODE.sizeof);
  91. while (EnumDisplaySettings(NULL, iModeNum++, &devmode))
  92. {
  93. pcxBitmap = max(pcxBitmap, cast(int)devmode.dmPelsWidth);
  94. pcyBitmap = max(pcyBitmap, cast(int)devmode.dmPelsHeight);
  95. }
  96. }
  97. extern (Windows)
  98. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  99. {
  100. scope (failure) assert(0);
  101. static BOOL fLeftButtonDown, fRightButtonDown;
  102. static HBITMAP hBitmap;
  103. static HDC hdcMem;
  104. static int cxBitmap, cyBitmap, cxClient, cyClient, xMouse, yMouse;
  105. HDC hdc;
  106. PAINTSTRUCT ps;
  107. switch (message)
  108. {
  109. case WM_CREATE:
  110. GetLargestDisplayMode(cxBitmap, cyBitmap);
  111. hdc = GetDC(hwnd);
  112. hBitmap = CreateCompatibleBitmap(hdc, cxBitmap, cyBitmap);
  113. hdcMem = CreateCompatibleDC(hdc);
  114. ReleaseDC(hwnd, hdc);
  115. if (!hBitmap) // no memory for bitmap
  116. {
  117. DeleteDC(hdcMem);
  118. return -1;
  119. }
  120. SelectObject(hdcMem, hBitmap);
  121. PatBlt(hdcMem, 0, 0, cxBitmap, cyBitmap, WHITENESS);
  122. return 0;
  123. case WM_SIZE:
  124. cxClient = LOWORD(lParam);
  125. cyClient = HIWORD(lParam);
  126. return 0;
  127. case WM_LBUTTONDOWN:
  128. if (!fRightButtonDown)
  129. SetCapture(hwnd);
  130. xMouse = LOWORD(lParam);
  131. yMouse = HIWORD(lParam);
  132. fLeftButtonDown = TRUE;
  133. return 0;
  134. case WM_LBUTTONUP:
  135. if (fLeftButtonDown)
  136. SetCapture(NULL);
  137. fLeftButtonDown = FALSE;
  138. return 0;
  139. case WM_RBUTTONDOWN:
  140. if (!fLeftButtonDown)
  141. SetCapture(hwnd);
  142. xMouse = LOWORD(lParam);
  143. yMouse = HIWORD(lParam);
  144. fRightButtonDown = TRUE;
  145. return 0;
  146. case WM_RBUTTONUP:
  147. if (fRightButtonDown)
  148. SetCapture(NULL);
  149. fRightButtonDown = FALSE;
  150. return 0;
  151. case WM_MOUSEMOVE:
  152. if (!fLeftButtonDown && !fRightButtonDown)
  153. return 0;
  154. hdc = GetDC(hwnd);
  155. SelectObject(hdc,
  156. GetStockObject(fLeftButtonDown ? BLACK_PEN : WHITE_PEN));
  157. SelectObject(hdcMem,
  158. GetStockObject(fLeftButtonDown ? BLACK_PEN : WHITE_PEN));
  159. MoveToEx(hdc, xMouse, yMouse, NULL);
  160. MoveToEx(hdcMem, xMouse, yMouse, NULL);
  161. xMouse = cast(short)LOWORD(lParam);
  162. yMouse = cast(short)HIWORD(lParam);
  163. LineTo(hdc, xMouse, yMouse);
  164. LineTo(hdcMem, xMouse, yMouse);
  165. ReleaseDC(hwnd, hdc);
  166. return 0;
  167. case WM_PAINT:
  168. hdc = BeginPaint(hwnd, &ps);
  169. BitBlt(hdc, 0, 0, cxClient, cyClient, hdcMem, 0, 0, SRCCOPY);
  170. EndPaint(hwnd, &ps);
  171. return 0;
  172. case WM_DESTROY:
  173. DeleteDC(hdcMem);
  174. DeleteObject(hBitmap);
  175. PostQuitMessage(0);
  176. return 0;
  177. default:
  178. }
  179. return DefWindowProc(hwnd, message, wParam, lParam);
  180. }