PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap05/AltWind/AltWind.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 199 lines | 153 code | 41 blank | 5 comment | 2 complexity | 3496ae1252f2f21bb3e094558ae454bb MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module AltWind;
  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. pragma(lib, "gdi32.lib");
  14. pragma(lib, "winmm.lib");
  15. import core.sys.windows.mmsystem;
  16. import core.sys.windows.windef;
  17. import core.sys.windows.winuser;
  18. import core.sys.windows.wingdi;
  19. extern(Windows)
  20. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  21. {
  22. int result;
  23. try
  24. {
  25. Runtime.initialize();
  26. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  27. Runtime.terminate();
  28. }
  29. catch(Throwable o)
  30. {
  31. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  32. result = 0;
  33. }
  34. return result;
  35. }
  36. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  37. {
  38. string appName = "AltWind";
  39. HWND hwnd;
  40. MSG msg;
  41. WNDCLASS wndclass;
  42. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  43. wndclass.lpfnWndProc = &WndProc;
  44. wndclass.cbClsExtra = 0;
  45. wndclass.cbWndExtra = 0;
  46. wndclass.hInstance = hInstance;
  47. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  48. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  49. wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH);
  50. wndclass.lpszMenuName = NULL;
  51. wndclass.lpszClassName = appName.toUTF16z;
  52. if(!RegisterClass(&wndclass))
  53. {
  54. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  55. return 0;
  56. }
  57. hwnd = CreateWindow(appName.toUTF16z, // window class name
  58. "Alternate and Winding Fill Modes", // window caption
  59. WS_OVERLAPPEDWINDOW, // window style
  60. CW_USEDEFAULT, // initial x position
  61. CW_USEDEFAULT, // initial y position
  62. CW_USEDEFAULT, // initial x size
  63. CW_USEDEFAULT, // initial y size
  64. NULL, // parent window handle
  65. NULL, // window menu handle
  66. hInstance, // program instance handle
  67. NULL); // creation parameters
  68. ShowWindow(hwnd, iCmdShow);
  69. UpdateWindow(hwnd);
  70. while (GetMessage(&msg, NULL, 0, 0))
  71. {
  72. TranslateMessage(&msg);
  73. DispatchMessage(&msg);
  74. }
  75. return msg.wParam;
  76. }
  77. extern(Windows)
  78. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  79. {
  80. scope (failure) assert(0);
  81. static int cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth;
  82. HDC hdc;
  83. int i, x, y, iVertPos, iHorzPos, iPaintStart, iPaintEnd;
  84. PAINTSTRUCT ps;
  85. TEXTMETRIC tm;
  86. enum aptFigure =
  87. [
  88. POINT(10,70), POINT(50,70), POINT(50,10), POINT(90,10), POINT(90,50),
  89. POINT(30,50), POINT(30,90), POINT(70,90), POINT(70,30), POINT(10,30)
  90. ];
  91. POINT[10] apt;
  92. switch(message)
  93. {
  94. case WM_CREATE:
  95. {
  96. hdc = GetDC(hwnd);
  97. scope(exit)
  98. ReleaseDC(hwnd, hdc);
  99. GetTextMetrics(hdc, &tm);
  100. cxChar = tm.tmAveCharWidth;
  101. cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
  102. cyChar = tm.tmHeight + tm.tmExternalLeading;
  103. // Save the width of the three columns
  104. iMaxWidth = 40 * cxChar + 22 * cxCaps;
  105. return 0;
  106. }
  107. case WM_SIZE:
  108. {
  109. cxClient = LOWORD(lParam);
  110. cyClient = HIWORD(lParam);
  111. apt[0].x = cxClient / 4;
  112. apt[0].y = cyClient / 2;
  113. apt[1].x = cxClient / 2;
  114. apt[1].y = cyClient / 4;
  115. apt[2].x = cxClient / 2;
  116. apt[2].y = 3 * cyClient / 4;
  117. apt[3].x = 3 * cxClient / 4;
  118. apt[3].y = cyClient / 2;
  119. return 0;
  120. }
  121. case WM_PAINT:
  122. {
  123. hdc = BeginPaint(hwnd, &ps);
  124. scope(exit)
  125. {
  126. EndPaint(hwnd, &ps);
  127. }
  128. SelectObject(hdc, GetStockObject(GRAY_BRUSH));
  129. foreach (index; 0 .. 10)
  130. {
  131. apt[index].x = cxClient * aptFigure[index].x / 200;
  132. apt[index].y = cyClient * aptFigure[index].y / 100;
  133. }
  134. SetPolyFillMode(hdc, ALTERNATE);
  135. Polygon(hdc, apt.ptr, apt.length);
  136. foreach (index; 0 .. 10)
  137. {
  138. apt[index].x += cxClient / 2;
  139. }
  140. SetPolyFillMode(hdc, WINDING);
  141. Polygon(hdc, apt.ptr, apt.length);
  142. return 0;
  143. }
  144. case WM_DESTROY:
  145. PostQuitMessage(0);
  146. return 0;
  147. default:
  148. }
  149. return DefWindowProc(hwnd, message, wParam, lParam);
  150. }
  151. void DrawBezier(HDC hdc, POINT[4] apt)
  152. {
  153. PolyBezier(hdc, apt.ptr, apt.length);
  154. MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
  155. LineTo(hdc, apt[1].x, apt[1].y);
  156. MoveToEx(hdc, apt[2].x, apt[2].y, NULL);
  157. LineTo(hdc, apt[3].x, apt[3].y);
  158. }