PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Extra/Cartesian/Cartesian.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 191 lines | 133 code | 36 blank | 22 comment | 3 complexity | 477999fd47a37a983ebee8a28247507a MD5 | raw file
  1. /+
  2. + Copyright Andrej Mitrovic 2011.
  3. + Distributed under the Boost Software License, Version 1.0.
  4. + (See accompanying file LICENSE_1_0.txt or copy at
  5. + http://www.boost.org/LICENSE_1_0.txt)
  6. +
  7. + Demonstrates using Cartesian coordinates which has
  8. + the Y axis positive values towards the top compared to GDI.
  9. +
  10. + More info found here:
  11. + http://www.functionx.com/visualc/gdi/gdicoord.htm
  12. +/
  13. module Cartesian;
  14. import core.memory;
  15. import core.runtime;
  16. import core.thread;
  17. import std.conv;
  18. import std.math;
  19. import std.range;
  20. import std.string;
  21. import std.utf;
  22. auto toUTF16z(S)(S s)
  23. {
  24. return toUTFz!(const(wchar)*)(s);
  25. }
  26. pragma(lib, "gdi32.lib");
  27. import core.sys.windows.windef;
  28. import core.sys.windows.winuser;
  29. import core.sys.windows.wingdi;
  30. import core.sys.windows.winbase;
  31. string appName = "Cartesian";
  32. string description = "Cartesian Demo";
  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.hbrBackground = null; // don't send WM_ERASEBKND messages
  67. wndclass.lpszMenuName = appName.toUTF16z;
  68. wndclass.lpszClassName = appName.toUTF16z;
  69. if (!RegisterClass(&wndclass))
  70. {
  71. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  72. return 0;
  73. }
  74. hwnd = CreateWindow(appName.toUTF16z, // window class name
  75. description.toUTF16z, // window caption
  76. WS_OVERLAPPEDWINDOW, // window style
  77. CW_USEDEFAULT, // initial x position
  78. CW_USEDEFAULT, // initial y position
  79. CW_USEDEFAULT, // initial x size
  80. CW_USEDEFAULT, // initial y size
  81. NULL, // parent window handle
  82. NULL, // window menu handle
  83. hInstance, // program instance handle
  84. NULL); // creation parameters
  85. ShowWindow(hwnd, iCmdShow);
  86. UpdateWindow(hwnd);
  87. while (GetMessage(&msg, NULL, 0, 0))
  88. {
  89. TranslateMessage(&msg);
  90. DispatchMessage(&msg);
  91. }
  92. return msg.wParam;
  93. }
  94. extern (Windows)
  95. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  96. {
  97. scope (failure) assert(0);
  98. static int cxClient, cyClient, cxSource, cySource;
  99. int x, y;
  100. HDC hdc;
  101. PAINTSTRUCT ps;
  102. static HDC hdcMem;
  103. static HBITMAP hbmMem;
  104. static HANDLE hOld;
  105. RECT rect;
  106. switch (message)
  107. {
  108. case WM_SIZE:
  109. cxClient = LOWORD(lParam);
  110. cyClient = HIWORD(lParam);
  111. return 0;
  112. // When you set 'hbrBackground = null' it prevents
  113. // the WM_ERASEBKND message to be sent.
  114. case WM_ERASEBKGND:
  115. return 1;
  116. case WM_PAINT:
  117. {
  118. // Get DC for window
  119. hdc = BeginPaint(hwnd, &ps);
  120. // Create an off-screen DC for double-buffering
  121. hdcMem = CreateCompatibleDC(hdc);
  122. hbmMem = CreateCompatibleBitmap(hdc, cxClient, cyClient);
  123. hOld = SelectObject(hdcMem, hbmMem);
  124. // Draw into hdcMem
  125. GetClientRect(hwnd, &rect);
  126. // Flip Y axis
  127. SetMapMode(hdcMem, MM_ANISOTROPIC);
  128. SetViewportOrgEx(hdcMem, 0, rect.bottom, null);
  129. SetWindowExtEx(hdcMem, rect.bottom, rect.right, null);
  130. SetViewportExtEx(hdcMem, rect.bottom, -rect.right, null);
  131. // Required for both contexts
  132. SetMapMode(hdc, MM_ANISOTROPIC);
  133. SetViewportOrgEx(hdc, 0, rect.bottom, null);
  134. SetWindowExtEx(hdc, rect.bottom, rect.right, null);
  135. SetViewportExtEx(hdc, rect.bottom, -rect.right, null);
  136. FillRect(hdcMem, &rect, GetStockObject(BLACK_BRUSH));
  137. SelectObject(hdcMem, GetStockObject(WHITE_PEN));
  138. MoveToEx(hdcMem, 50, 50, null);
  139. LineTo(hdcMem, 150, 100); // should result in a '/' line pointing toward top-right
  140. // Transfer the off-screen DC to the screen
  141. BitBlt(hdc, 0, 0, cxClient, cyClient, hdcMem, 0, 0, SRCCOPY);
  142. // Free-up the off-screen DC
  143. SelectObject(hdcMem, hOld);
  144. DeleteObject(hbmMem);
  145. DeleteDC (hdcMem);
  146. EndPaint(hwnd, &ps);
  147. return 0;
  148. }
  149. case WM_DESTROY:
  150. PostQuitMessage(0);
  151. return 0;
  152. default:
  153. }
  154. return DefWindowProc(hwnd, message, wParam, lParam);
  155. }