PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap17/EndJoin/EndJoin.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 163 lines | 130 code | 29 blank | 4 comment | 4 complexity | cf306931fa3cfae6929aa6604eb76eb0 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module EndJoin;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.utf;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. pragma(lib, "gdi32.lib");
  19. pragma(lib, "comdlg32.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. import core.sys.windows.commdlg;
  25. string appName = "EndJoin";
  26. string description = "Ends and Joins Demo";
  27. HINSTANCE hinst;
  28. extern (Windows)
  29. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  30. {
  31. int result;
  32. try
  33. {
  34. Runtime.initialize();
  35. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  36. Runtime.terminate();
  37. }
  38. catch (Throwable o)
  39. {
  40. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  41. result = 0;
  42. }
  43. return result;
  44. }
  45. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  46. {
  47. hinst = hInstance;
  48. HACCEL hAccel;
  49. HWND hwnd;
  50. MSG msg;
  51. WNDCLASS wndclass;
  52. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  53. wndclass.lpfnWndProc = &WndProc;
  54. wndclass.cbClsExtra = 0;
  55. wndclass.cbWndExtra = 0;
  56. wndclass.hInstance = hInstance;
  57. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  58. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  59. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  60. wndclass.lpszMenuName = appName.toUTF16z;
  61. wndclass.lpszClassName = appName.toUTF16z;
  62. if (!RegisterClass(&wndclass))
  63. {
  64. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  65. return 0;
  66. }
  67. hwnd = CreateWindow(appName.toUTF16z, // window class name
  68. description.toUTF16z, // window caption
  69. WS_OVERLAPPEDWINDOW, // window style
  70. CW_USEDEFAULT, // initial x position
  71. CW_USEDEFAULT, // initial y position
  72. CW_USEDEFAULT, // initial x size
  73. CW_USEDEFAULT, // initial y size
  74. NULL, // parent window handle
  75. NULL, // window menu handle
  76. hInstance, // program instance handle
  77. NULL); // creation parameters
  78. ShowWindow(hwnd, iCmdShow);
  79. UpdateWindow(hwnd);
  80. while (GetMessage(&msg, NULL, 0, 0))
  81. {
  82. TranslateMessage(&msg);
  83. DispatchMessage(&msg);
  84. }
  85. return msg.wParam;
  86. }
  87. extern (Windows)
  88. LRESULT WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  89. {
  90. static int[] iEnd = [PS_ENDCAP_ROUND, PS_ENDCAP_SQUARE, PS_ENDCAP_FLAT];
  91. static int[] iJoin = [PS_JOIN_ROUND, PS_JOIN_BEVEL, PS_JOIN_MITER];
  92. static int cxClient, cyClient;
  93. HDC hdc;
  94. int i;
  95. LOGBRUSH lb;
  96. PAINTSTRUCT ps;
  97. switch (iMsg)
  98. {
  99. case WM_SIZE:
  100. cxClient = LOWORD(lParam);
  101. cyClient = HIWORD(lParam);
  102. return 0;
  103. case WM_PAINT:
  104. hdc = BeginPaint(hwnd, &ps);
  105. SetMapMode(hdc, MM_ANISOTROPIC);
  106. SetWindowExtEx(hdc, 100, 100, NULL);
  107. SetViewportExtEx(hdc, cxClient, cyClient, NULL);
  108. lb.lbStyle = BS_SOLID;
  109. lb.lbColor = RGB(128, 128, 128);
  110. lb.lbHatch = 0;
  111. for (i = 0; i < 3; i++)
  112. {
  113. SelectObject(hdc, ExtCreatePen(PS_SOLID | PS_GEOMETRIC |
  114. iEnd[i] | iJoin[i], 10, &lb, 0, NULL));
  115. BeginPath(hdc);
  116. MoveToEx(hdc, 10 + 30 * i, 25, NULL);
  117. LineTo(hdc, 20 + 30 * i, 75);
  118. LineTo(hdc, 30 + 30 * i, 25);
  119. EndPath(hdc);
  120. StrokePath(hdc);
  121. DeleteObject(SelectObject(hdc, GetStockObject(BLACK_PEN)));
  122. MoveToEx(hdc, 10 + 30 * i, 25, NULL);
  123. LineTo(hdc, 20 + 30 * i, 75);
  124. LineTo(hdc, 30 + 30 * i, 25);
  125. }
  126. EndPaint(hwnd, &ps);
  127. return 0;
  128. case WM_DESTROY:
  129. PostQuitMessage(0);
  130. return 0;
  131. default:
  132. }
  133. return DefWindowProc(hwnd, iMsg, wParam, lParam);
  134. }