PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/win32_direct.d

http://github.com/AndrejMitrovic/cairoDSamples
D | 182 lines | 136 code | 37 blank | 9 comment | 5 complexity | d5c4193944b543121a82988782d4988d MD5 | raw file
  1. module win32_direct;
  2. /+
  3. + Copyright Andrej Mitrovic 2011.
  4. + Distributed under the Boost Software License, Version 1.0.
  5. + (See accompanying file LICENSE_1_0.txt or copy at
  6. + http://www.boost.org/LICENSE_1_0.txt)
  7. +/
  8. // note: there's some kind of bug in cairo or in my understanding of drawing directly to GDI,
  9. // this doesn't draw properly (when using DrawCairoRectangle) when a window is overlayed over our app window.
  10. import core.runtime;
  11. import std.stdio;
  12. import std.random;
  13. import std.utf;
  14. pragma(lib, "gdi32.lib");
  15. import win32.windef;
  16. import win32.winuser;
  17. import win32.wingdi;
  18. string appName = "app";
  19. string description = "";
  20. import cairo.cairo;
  21. import cairo.win32;
  22. alias cairo.cairo.RGB RGB;
  23. extern (Windows)
  24. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  25. {
  26. int result;
  27. void exceptionHandler(Throwable e) { throw e; }
  28. try
  29. {
  30. Runtime.initialize(&exceptionHandler);
  31. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  32. Runtime.terminate(&exceptionHandler);
  33. }
  34. catch (Throwable o)
  35. {
  36. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  37. result = 0;
  38. }
  39. return result;
  40. }
  41. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  42. {
  43. HWND hwnd;
  44. MSG msg;
  45. WNDCLASS wndclass;
  46. wndclass.style = CS_HREDRAW | CS_VREDRAW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  47. wndclass.lpfnWndProc = &WndProc;
  48. wndclass.cbClsExtra = 0;
  49. wndclass.cbWndExtra = 0;
  50. wndclass.hInstance = hInstance;
  51. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  52. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  53. wndclass.hbrBackground = null;
  54. wndclass.lpszMenuName = appName.toUTF16z;
  55. wndclass.lpszClassName = appName.toUTF16z;
  56. if (!RegisterClass(&wndclass))
  57. {
  58. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  59. return 0;
  60. }
  61. hwnd = CreateWindow(appName.toUTF16z, // window class name
  62. description.toUTF16z, // window caption
  63. WS_OVERLAPPEDWINDOW, // window style
  64. CW_USEDEFAULT, // initial x position
  65. CW_USEDEFAULT, // initial y position
  66. CW_USEDEFAULT, // initial x size
  67. CW_USEDEFAULT, // initial y size
  68. NULL, // parent window handle
  69. NULL, // window menu handle
  70. hInstance, // program instance handle
  71. NULL); // creation parameters
  72. ShowWindow(hwnd, iCmdShow);
  73. UpdateWindow(hwnd);
  74. while (GetMessage(&msg, NULL, 0, 0))
  75. {
  76. TranslateMessage(&msg);
  77. DispatchMessage(&msg);
  78. }
  79. return msg.wParam;
  80. }
  81. extern (Windows)
  82. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  83. {
  84. switch (message)
  85. {
  86. case WM_CREATE:
  87. {
  88. window = new Window();
  89. return 0;
  90. }
  91. default:
  92. }
  93. if (window)
  94. return window.process(hwnd, message, wParam, lParam);
  95. else
  96. return DefWindowProc(hwnd, message, wParam, lParam);
  97. }
  98. Window window;
  99. class Window
  100. {
  101. int cxClient, cyClient;
  102. LRESULT process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  103. {
  104. switch (message)
  105. {
  106. case WM_DESTROY:
  107. PostQuitMessage(0);
  108. return 0;
  109. case WM_PAINT:
  110. OnPaint(hwnd, message, wParam, lParam);
  111. return 0;
  112. case WM_SIZE:
  113. cxClient = LOWORD(lParam);
  114. cyClient = HIWORD(lParam);
  115. return 0;
  116. default:
  117. }
  118. return DefWindowProc(hwnd, message, wParam, lParam);
  119. }
  120. void OnPaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  121. {
  122. PAINTSTRUCT ps;
  123. auto hdc = BeginPaint(hwnd, &ps); // ps.rcPaint
  124. //~ DrawGDIRectangle(hdc);
  125. DrawCairoRectangle(hdc);
  126. EndPaint(hwnd, &ps);
  127. }
  128. void DrawCairoRectangle(HDC hdc)
  129. {
  130. auto surf = new Win32Surface(hdc);
  131. auto ctx = Context(surf);
  132. ctx.setSourceRGB(0, 0, 1);
  133. ctx.paint();
  134. }
  135. void DrawGDIRectangle(HDC hdc)
  136. {
  137. alias win32.wingdi.RGB RGB;
  138. HBRUSH hBrush;
  139. RECT rect;
  140. SetRect(&rect, 0, 0, cxClient, cyClient);
  141. hBrush = CreateSolidBrush(RGB(0, 0, 255));
  142. FillRect(hdc, &rect, hBrush);
  143. DeleteObject(hBrush);
  144. }
  145. }