PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Extra/VisualStyles/VisualStyles.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 246 lines | 183 code | 45 blank | 18 comment | 6 complexity | b783ed04cadcac9426eed52f9867bff3 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 VisualStyles to draw themed controls.
  8. + If there is no active theme (i.e. Windows Classic style is active),
  9. + we use a custom paint routine.
  10. +/
  11. module VisualStyles;
  12. import core.memory;
  13. import core.runtime;
  14. import core.thread;
  15. import std.conv;
  16. import std.math;
  17. import std.range;
  18. import std.string;
  19. import std.utf;
  20. auto toUTF16z(S)(S s)
  21. {
  22. return toUTFz!(const(wchar)*)(s);
  23. }
  24. pragma(lib, "gdi32.lib");
  25. import core.sys.windows.windef;
  26. import core.sys.windows.winuser;
  27. import core.sys.windows.wingdi;
  28. import core.sys.windows.winbase;
  29. import uxSchema;
  30. import uxTheme;
  31. string appName = "VisualStyles";
  32. string description = "VisualStyles 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. enum Control
  95. {
  96. Button
  97. }
  98. extern (Windows)
  99. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  100. {
  101. scope (failure) assert(0);
  102. static int cxClient, cyClient, cxSource, cySource;
  103. int x, y;
  104. HDC hdc;
  105. PAINTSTRUCT ps;
  106. static HDC hdcMem;
  107. static HBITMAP hbmMem;
  108. static HANDLE hOld;
  109. RECT rect;
  110. switch (message)
  111. {
  112. case WM_SIZE:
  113. cxClient = LOWORD(lParam);
  114. cyClient = HIWORD(lParam);
  115. return 0;
  116. // When you set 'hbrBackground = null' it prevents
  117. // the WM_ERASEBKND message to be sent.
  118. case WM_ERASEBKGND:
  119. return 1;
  120. case WM_PAINT:
  121. {
  122. // Get DC for window
  123. hdc = BeginPaint(hwnd, &ps);
  124. // Create an off-screen DC for double-buffering
  125. hdcMem = CreateCompatibleDC(hdc);
  126. hbmMem = CreateCompatibleBitmap(hdc, cxClient, cyClient);
  127. hOld = SelectObject(hdcMem, hbmMem);
  128. auto buttonRect = RECT(100, 100, 190, 130);
  129. DrawControl(hwnd, hdcMem, buttonRect, Control.Button);
  130. // Transfer the off-screen DC to the screen
  131. BitBlt(hdc, 0, 0, cxClient, cyClient, hdcMem, 0, 0, SRCCOPY);
  132. // Free-up the off-screen DC
  133. SelectObject(hdcMem, hOld);
  134. DeleteObject(hbmMem);
  135. DeleteDC (hdcMem);
  136. EndPaint(hwnd, &ps);
  137. return 0;
  138. }
  139. case WM_DESTROY:
  140. PostQuitMessage(0);
  141. return 0;
  142. default:
  143. }
  144. return DefWindowProc(hwnd, message, wParam, lParam);
  145. }
  146. void DrawControl(HWND hwnd, HDC hdcMem, RECT rect, Control control)
  147. {
  148. HTHEME hTheme = OpenThemeData(hwnd, "Button");
  149. scope(exit)
  150. {
  151. if (hTheme !is null)
  152. CloseThemeData(hTheme);
  153. }
  154. final switch (control)
  155. {
  156. case Control.Button:
  157. {
  158. if (hTheme is null)
  159. {
  160. DrawCustomButton(hdcMem, rect);
  161. }
  162. else
  163. {
  164. DrawThemedButton(hTheme, hdcMem, rect);
  165. }
  166. break;
  167. }
  168. }
  169. }
  170. import std.utf : count;
  171. import std.exception;
  172. import std.stdio;
  173. void DrawCustomButton(HDC hDC, RECT rc)
  174. {
  175. SIZE size;
  176. auto text = "My button";
  177. FillRect(hDC, &rc, cast(HBRUSH)GetStockObject(GRAY_BRUSH));
  178. FrameRect(hDC, &rc, cast(HBRUSH)GetStockObject(LTGRAY_BRUSH));
  179. // calculate center
  180. GetTextExtentPoint32(hDC, text.toUTF16z, text.count, &size);
  181. auto rectWidth = (rc.right - rc.left);
  182. auto textWidth = (size.cx);
  183. auto xPos = rc.left + ((rectWidth - textWidth) / 2);
  184. auto rectHeight = (rc.bottom - rc.top);
  185. auto textHeight = (size.cy);
  186. auto yPos = rc.top + ((rectHeight - textHeight) / 2);
  187. TextOut(hDC, xPos, yPos, text.toUTF16z, text.count);
  188. }
  189. void DrawThemedButton(HTHEME hTheme, HDC hDC, RECT rc)
  190. {
  191. RECT rcContent;
  192. HRESULT hr;
  193. auto text = "My button";
  194. bool iState = 0;
  195. hr = DrawThemeBackground(hTheme, hDC, BP_PUSHBUTTON, iState, &rc, null);
  196. hr = GetThemeBackgroundContentRect(hTheme, hDC, BP_PUSHBUTTON, iState, &rc, &rcContent);
  197. hr = DrawThemeText(hTheme, hDC, BP_PUSHBUTTON, iState,
  198. text.toUTF16z, text.count,
  199. DT_CENTER | DT_VCENTER | DT_SINGLELINE,
  200. 0, &rcContent);
  201. }