PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap05/RandRect/RandRect.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 151 lines | 118 code | 29 blank | 4 comment | 10 complexity | 19d5b424b413e7561b5f11efddd7e64f MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module RandRect;
  6. import core.runtime;
  7. import core.thread;
  8. import std.string;
  9. import std.utf;
  10. auto toUTF16z(S)(S s)
  11. {
  12. return toUTFz!(const(wchar)*)(s);
  13. }
  14. import std.math;
  15. import std.random;
  16. pragma(lib, "gdi32.lib");
  17. import core.sys.windows.windef;
  18. import core.sys.windows.winuser;
  19. import core.sys.windows.wingdi;
  20. extern (Windows)
  21. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  22. {
  23. int result;
  24. try
  25. {
  26. Runtime.initialize();
  27. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  28. Runtime.terminate();
  29. }
  30. catch (Throwable o)
  31. {
  32. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  33. result = 0;
  34. }
  35. return result;
  36. }
  37. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  38. {
  39. string appName = "RandRect";
  40. HWND hwnd;
  41. MSG msg;
  42. WNDCLASS wndclass;
  43. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  44. wndclass.lpfnWndProc = &WndProc;
  45. wndclass.cbClsExtra = 0;
  46. wndclass.cbWndExtra = 0;
  47. wndclass.hInstance = hInstance;
  48. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  49. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  50. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  51. wndclass.lpszMenuName = NULL;
  52. wndclass.lpszClassName = appName.toUTF16z;
  53. if (!RegisterClass(&wndclass))
  54. {
  55. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  56. return 0;
  57. }
  58. hwnd = CreateWindow(appName.toUTF16z, // window class name
  59. "Random Rectangles", // window caption
  60. WS_OVERLAPPEDWINDOW, // window style
  61. CW_USEDEFAULT, // initial x position
  62. CW_USEDEFAULT, // initial y position
  63. CW_USEDEFAULT, // initial x size
  64. CW_USEDEFAULT, // initial y size
  65. NULL, // parent window handle
  66. NULL, // window menu handle
  67. hInstance, // program instance handle
  68. NULL); // creation parameters
  69. ShowWindow(hwnd, iCmdShow);
  70. UpdateWindow(hwnd);
  71. while (true)
  72. {
  73. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  74. {
  75. if (msg.message == WM_QUIT)
  76. break;
  77. TranslateMessage(&msg);
  78. DispatchMessage(&msg);
  79. }
  80. else
  81. {
  82. DrawRectangle(hwnd);
  83. Thread.sleep(dur!"msecs"(80)); // necessary on modern hardware to slow things down
  84. }
  85. }
  86. return msg.wParam;
  87. }
  88. int cxClient, cyClient;
  89. extern (Windows)
  90. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  91. {
  92. scope (failure) assert(0);
  93. switch (message)
  94. {
  95. case WM_SIZE:
  96. cxClient = LOWORD(lParam);
  97. cyClient = HIWORD(lParam);
  98. return 0;
  99. case WM_DESTROY:
  100. PostQuitMessage(0);
  101. return 0;
  102. default:
  103. }
  104. return DefWindowProc(hwnd, message, wParam, lParam);
  105. }
  106. void DrawRectangle(HWND hwnd)
  107. {
  108. HBRUSH hBrush;
  109. HDC hdc;
  110. RECT rect;
  111. if (cxClient == 0 || cyClient == 0)
  112. return;
  113. SetRect (&rect, uniform(0, cxClient), uniform(0, cyClient),
  114. uniform(0, cxClient), uniform(0, cyClient));
  115. hBrush = CreateSolidBrush(RGB(cast(byte)uniform(0, 256),
  116. cast(byte)uniform(0, 256),
  117. cast(byte)uniform(0, 256)));
  118. hdc = GetDC(hwnd);
  119. FillRect(hdc, &rect, hBrush);
  120. ReleaseDC(hwnd, hdc);
  121. DeleteObject(hBrush);
  122. }