/win32_roundRect.d

http://github.com/AndrejMitrovic/cairoDSamples · D · 212 lines · 167 code · 41 blank · 4 comment · 5 complexity · bd0a0cc5b5504617261c9bd67788b792 MD5 · raw file

  1. module win32test;
  2. /+
  3. + This tutorial is derived from: http://cairographics.org/cookbook/win32quickstart/
  4. + Translated to D2 by Andrej Mitrovic, 2011.
  5. +/
  6. import core.runtime;
  7. import std.utf;
  8. import std.traits;
  9. pragma(lib, "gdi32.lib");
  10. import win32.windef;
  11. import win32.winuser;
  12. import win32.wingdi;
  13. string appName = "CairoWindow";
  14. string description = "Rounded Rectangles";
  15. HINSTANCE hinst;
  16. import cairo.cairo;
  17. import cairo.win32;
  18. alias cairo.cairo.RGB RGB; // conflicts with win32.wingdi.RGB
  19. import rounded_rectangle;
  20. extern (Windows)
  21. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  22. {
  23. int result;
  24. void exceptionHandler(Throwable e) { throw e; }
  25. try
  26. {
  27. Runtime.initialize(&exceptionHandler);
  28. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  29. Runtime.terminate(&exceptionHandler);
  30. }
  31. catch (Throwable o)
  32. {
  33. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  34. result = 0;
  35. }
  36. return result;
  37. }
  38. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  39. {
  40. hinst = hInstance;
  41. HACCEL hAccel;
  42. HWND hwnd;
  43. MSG msg;
  44. WNDCLASS wndclass;
  45. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  46. wndclass.lpfnWndProc = &WndProc;
  47. wndclass.cbClsExtra = 0;
  48. wndclass.cbWndExtra = 0;
  49. wndclass.hInstance = hInstance;
  50. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  51. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  52. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  53. wndclass.lpszMenuName = appName.toUTF16z;
  54. wndclass.lpszClassName = appName.toUTF16z;
  55. if (!RegisterClass(&wndclass))
  56. {
  57. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  58. return 0;
  59. }
  60. hwnd = CreateWindow(appName.toUTF16z, // window class name
  61. description.toUTF16z, // window caption
  62. WS_OVERLAPPEDWINDOW, // window style
  63. CW_USEDEFAULT, // initial x position
  64. CW_USEDEFAULT, // initial y position
  65. 650, // initial x size
  66. 250, // initial y size
  67. NULL, // parent window handle
  68. NULL, // window menu handle
  69. hInstance, // program instance handle
  70. NULL); // creation parameters
  71. ShowWindow(hwnd, iCmdShow);
  72. UpdateWindow(hwnd);
  73. while (GetMessage(&msg, NULL, 0, 0))
  74. {
  75. TranslateMessage(&msg);
  76. DispatchMessage(&msg);
  77. }
  78. return msg.wParam;
  79. }
  80. extern (Windows)
  81. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  82. {
  83. switch (message)
  84. {
  85. case WM_CREATE:
  86. {
  87. window = new Window(hwnd);
  88. return 0;
  89. }
  90. default:
  91. }
  92. if (window)
  93. return window.process(hwnd, message, wParam, lParam);
  94. else
  95. return DefWindowProc(hwnd, message, wParam, lParam);
  96. }
  97. Window window;
  98. class Window
  99. {
  100. int x, y;
  101. HWND hwnd;
  102. HDC hdc;
  103. PAINTSTRUCT ps;
  104. RECT rc;
  105. HDC _buffer;
  106. HBITMAP hBitmap;
  107. HBITMAP hOldBitmap;
  108. this(HWND hwnd)
  109. {
  110. this.hwnd = hwnd;
  111. }
  112. LRESULT process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  113. {
  114. switch (message)
  115. {
  116. case WM_DESTROY:
  117. return OnDestroy(hwnd, message, wParam, lParam);
  118. case WM_PAINT:
  119. return OnPaint(hwnd, message, wParam, lParam);
  120. case WM_ERASEBKGND:
  121. return 0;
  122. default:
  123. }
  124. return DefWindowProc(hwnd, message, wParam, lParam);
  125. }
  126. auto OnPaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  127. {
  128. hdc = BeginPaint(hwnd, &ps);
  129. GetClientRect(hwnd, &rc);
  130. auto left = rc.left;
  131. auto top = rc.top;
  132. auto right = rc.right;
  133. auto bottom = rc.bottom;
  134. auto width = right - left;
  135. auto height = bottom - top;
  136. x = left;
  137. y = top;
  138. _buffer = CreateCompatibleDC(hdc);
  139. hBitmap = CreateCompatibleBitmap(hdc, width, height);
  140. hOldBitmap = SelectObject(_buffer, hBitmap);
  141. auto surf = new Win32Surface(_buffer);
  142. auto ctx = Context(surf);
  143. ctx.setSourceRGB(1, 1, 1);
  144. ctx.paint();
  145. ctx.translate(50, 40);
  146. int xPos = 150;
  147. int yPos = 10;
  148. foreach (index, method; EnumMembers!RoundMethod)
  149. {
  150. roundedRectangle!(method)(ctx, index * xPos, yPos, 100, 100, 10, 10);
  151. auto clr = RGB(0.9411764705882353, 0.996078431372549, 0.9137254901960784);
  152. ctx.setSourceRGB(clr);
  153. ctx.fillPreserve();
  154. clr = RGB(0.7019607843137254, 1.0, 0.5529411764705883);
  155. ctx.setSourceRGB(clr);
  156. ctx.stroke();
  157. }
  158. surf.finish();
  159. BitBlt(hdc, 0, 0, width, height, _buffer, x, y, SRCCOPY);
  160. SelectObject(_buffer, hOldBitmap);
  161. DeleteObject(hBitmap);
  162. DeleteDC(_buffer);
  163. EndPaint(hwnd, &ps);
  164. return 0;
  165. }
  166. auto OnDestroy(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  167. {
  168. PostQuitMessage(0);
  169. return 0;
  170. }
  171. }