PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap20/RndRctMT/RndRctMT.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 171 lines | 138 code | 29 blank | 4 comment | 8 complexity | e8e4f42c1c99fa87b7a2c339e025934d MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module RndRctMT;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.algorithm : min, max;
  10. import std.concurrency;
  11. import std.conv;
  12. import std.math;
  13. import std.random;
  14. import std.range;
  15. import std.string;
  16. import std.utf;
  17. auto toUTF16z(S)(S s)
  18. {
  19. return toUTFz!(const(wchar)*)(s);
  20. }
  21. pragma(lib, "gdi32.lib");
  22. pragma(lib, "comdlg32.lib");
  23. pragma(lib, "winmm.lib");
  24. import core.sys.windows.windef;
  25. import core.sys.windows.winuser;
  26. import core.sys.windows.wingdi;
  27. import core.sys.windows.winbase;
  28. import core.sys.windows.commdlg;
  29. import core.sys.windows.mmsystem;
  30. alias win32.winuser.MessageBox MessageBox;
  31. string appName = "RndRctMT";
  32. string description = "Random Rectangles";
  33. HINSTANCE hinst;
  34. extern (Windows)
  35. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  36. {
  37. int result;
  38. try
  39. {
  40. Runtime.initialize();
  41. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  42. Runtime.terminate();
  43. }
  44. catch (Throwable o)
  45. {
  46. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  47. result = 0;
  48. }
  49. return result;
  50. }
  51. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  52. {
  53. hinst = hInstance;
  54. HACCEL hAccel;
  55. HWND hwnd;
  56. MSG msg;
  57. WNDCLASS wndclass;
  58. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  59. wndclass.lpfnWndProc = &WndProc;
  60. wndclass.cbClsExtra = 0;
  61. wndclass.cbWndExtra = 0;
  62. wndclass.hInstance = hInstance;
  63. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  64. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  65. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  66. wndclass.lpszMenuName = appName.toUTF16z;
  67. wndclass.lpszClassName = appName.toUTF16z;
  68. if (!RegisterClass(&wndclass))
  69. {
  70. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  71. return 0;
  72. }
  73. hwnd = CreateWindow(appName.toUTF16z, // window class name
  74. description.toUTF16z, // window caption
  75. WS_OVERLAPPEDWINDOW, // window style
  76. CW_USEDEFAULT, // initial x position
  77. CW_USEDEFAULT, // initial y position
  78. CW_USEDEFAULT, // initial x size
  79. CW_USEDEFAULT, // initial y size
  80. NULL, // parent window handle
  81. NULL, // window menu handle
  82. hInstance, // program instance handle
  83. NULL); // creation parameters
  84. ShowWindow(hwnd, iCmdShow);
  85. UpdateWindow(hwnd);
  86. while (GetMessage(&msg, NULL, 0, 0))
  87. {
  88. TranslateMessage(&msg);
  89. DispatchMessage(&msg);
  90. }
  91. return msg.wParam;
  92. }
  93. __gshared HWND hwndGlob;
  94. __gshared int cxClient, cyClient;
  95. void ThreadFunc()
  96. {
  97. HBRUSH hBrush;
  98. HDC hdc;
  99. int xLeft, xRight, yTop, yBottom, iRed, iGreen, iBlue;
  100. while (true)
  101. {
  102. if (cxClient != 0 || cyClient != 0)
  103. {
  104. xLeft = uniform(0, cxClient);
  105. xRight = uniform(0, cxClient);
  106. yTop = uniform(0, cyClient);
  107. yBottom = uniform(0, cyClient);
  108. iRed = uniform(0, 255);
  109. iGreen = uniform(0, 255);
  110. iBlue = uniform(0, 255);
  111. hdc = GetDC(hwndGlob);
  112. hBrush = CreateSolidBrush(RGB(cast(ubyte)iRed, cast(ubyte)iGreen, cast(ubyte)iBlue));
  113. SelectObject(hdc, hBrush);
  114. Rectangle(hdc, min(xLeft, xRight), min(yTop, yBottom), max(xLeft, xRight), max(yTop, yBottom));
  115. ReleaseDC(hwndGlob, hdc);
  116. DeleteObject(hBrush);
  117. }
  118. Thread.sleep(dur!"msecs"(70));
  119. }
  120. }
  121. extern (Windows)
  122. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  123. {
  124. scope (failure) assert(0);
  125. hwndGlob = hwnd;
  126. switch (message)
  127. {
  128. case WM_CREATE:
  129. spawn(&ThreadFunc);
  130. return 0;
  131. case WM_SIZE:
  132. cxClient = LOWORD(lParam);
  133. cyClient = HIWORD(lParam);
  134. return 0;
  135. case WM_DESTROY:
  136. PostQuitMessage(0);
  137. ExitProcess(0);
  138. return 0;
  139. default:
  140. }
  141. return DefWindowProc(hwnd, message, wParam, lParam);
  142. }