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

/Samples/Chap05/Clover/Clover.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 172 lines | 134 code | 34 blank | 4 comment | 5 complexity | 04f0873c7d9e4ab21a5cd9648b2bcdf8 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Clover;
  6. import core.runtime;
  7. import std.string;
  8. import std.utf;
  9. auto toUTF16z(S)(S s)
  10. {
  11. return toUTFz!(const(wchar)*)(s);
  12. }
  13. import std.math;
  14. pragma(lib, "gdi32.lib");
  15. pragma(lib, "winmm.lib");
  16. import core.sys.windows.mmsystem;
  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 = "Clover";
  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. "Draw a Clover", // 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 (GetMessage(&msg, NULL, 0, 0))
  72. {
  73. TranslateMessage(&msg);
  74. DispatchMessage(&msg);
  75. }
  76. return msg.wParam;
  77. }
  78. extern (Windows)
  79. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  80. {
  81. scope (failure) assert(0);
  82. static HRGN hRgnClip;
  83. static int cxClient, cyClient;
  84. double fAngle, fRadius;
  85. HCURSOR hCursor;
  86. HDC hdc;
  87. HRGN[6] hRgnTemp;
  88. PAINTSTRUCT ps;
  89. switch (message)
  90. {
  91. case WM_SIZE:
  92. {
  93. cxClient = LOWORD(lParam);
  94. cyClient = HIWORD(lParam);
  95. hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  96. ShowCursor(TRUE);
  97. if (hRgnClip)
  98. DeleteObject(hRgnClip);
  99. hRgnTemp[0] = CreateEllipticRgn(0, cyClient / 3, cxClient / 2, 2 * cyClient / 3);
  100. hRgnTemp[1] = CreateEllipticRgn(cxClient / 2, cyClient / 3, cxClient, 2 * cyClient / 3);
  101. hRgnTemp[2] = CreateEllipticRgn(cxClient / 3, 0, 2 * cxClient / 3, cyClient / 2);
  102. hRgnTemp[3] = CreateEllipticRgn(cxClient / 3, cyClient / 2, 2 * cxClient / 3, cyClient);
  103. hRgnTemp[4] = CreateRectRgn(0, 0, 1, 1);
  104. hRgnTemp[5] = CreateRectRgn(0, 0, 1, 1);
  105. hRgnClip = CreateRectRgn(0, 0, 1, 1);
  106. CombineRgn(hRgnTemp[4], hRgnTemp[0], hRgnTemp[1], RGN_OR);
  107. CombineRgn(hRgnTemp[5], hRgnTemp[2], hRgnTemp[3], RGN_OR);
  108. CombineRgn(hRgnClip, hRgnTemp[4], hRgnTemp[5], RGN_XOR);
  109. foreach (i; 0 .. 6)
  110. DeleteObject(hRgnTemp[i]);
  111. SetCursor(hCursor);
  112. ShowCursor(FALSE);
  113. return 0;
  114. }
  115. case WM_PAINT:
  116. {
  117. hdc = BeginPaint(hwnd, &ps);
  118. scope(exit) EndPaint(hwnd, &ps);
  119. SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);
  120. SelectClipRgn(hdc, hRgnClip);
  121. fRadius = hypot(cxClient / 2.0, cyClient / 2.0);
  122. for (fAngle = 0.0; fAngle < PI*2; fAngle += PI*2 / 360)
  123. {
  124. MoveToEx(hdc, 0, 0, NULL);
  125. LineTo(hdc, cast(int)(fRadius * cos(fAngle) + 0.5), cast(int)(-fRadius * sin(fAngle) + 0.5));
  126. }
  127. return 0;
  128. }
  129. case WM_DESTROY:
  130. {
  131. DeleteObject(hRgnClip);
  132. PostQuitMessage(0);
  133. return 0;
  134. }
  135. default:
  136. }
  137. return DefWindowProc(hwnd, message, wParam, lParam);
  138. }