PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Extra/ThemedWakeUp/VisualStyles.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 213 lines | 153 code | 35 blank | 25 comment | 11 complexity | ae84c682772f5c5919533b8ce0b64b27 MD5 | raw file
  1. /+
  2. + Taken from DFL. See license.txt
  3. + DFL is Copyright (C) 2004-2010 Christopher E. Miller
  4. + Official DFL website: http://www.dprogramming.com/dfl.php
  5. +
  6. + Init function for visual styles.
  7. +/
  8. module VisualStyles;
  9. import core.memory;
  10. import core.runtime;
  11. import core.thread;
  12. import std.conv;
  13. import std.math;
  14. import std.exception;
  15. import std.range;
  16. import std.string;
  17. import std.utf;
  18. import std.stdio;
  19. pragma(lib, "gdi32.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.aclapi;
  25. import core.sys.windows.commctrl;
  26. pragma(lib, "comctl32.lib");
  27. pragma(lib, "advapi32.lib");
  28. UINT DWP_Msg;
  29. static this()
  30. {
  31. DWP_Msg = RegisterWindowMessageA("WM_DWP");
  32. if (!DWP_Msg) DWP_Msg = WM_USER + 0x7CD;
  33. }
  34. private extern (Windows)
  35. {
  36. alias UINT function(LPCWSTR lpPathName, LPCWSTR lpPrefixString, UINT uUnique,
  37. LPWSTR lpTempFileName) GetTempFileNameWProc;
  38. alias DWORD function(DWORD nBufferLength, LPWSTR lpBuffer) GetTempPathWProc;
  39. alias HANDLE function(PACTCTXW pActCtx) CreateActCtxWProc;
  40. alias BOOL function(HANDLE hActCtx, ULONG_PTR* lpCookie) ActivateActCtxProc;
  41. alias BOOL function(LPINITCOMMONCONTROLSEX lpInitCtrls) InitCommonControlsExProc;
  42. }
  43. void _initCommonControls(DWORD dwControls)
  44. {
  45. version (SUPPORTS_COMMON_CONTROLS_EX)
  46. {
  47. pragma(msg, "DFL: extended common controls supported at compile time");
  48. alias InitCommonControlsEx initProc;
  49. }
  50. else
  51. {
  52. // Make sure InitCommonControlsEx() is in comctl32.dll,
  53. // otherwise use the old InitCommonControls().
  54. HMODULE hmodCommonControls;
  55. InitCommonControlsExProc initProc;
  56. hmodCommonControls = LoadLibraryA("comctl32.dll");
  57. if (!hmodCommonControls)
  58. // throw new DflException("Unable to load 'comctl32.dll'");
  59. goto no_comctl32;
  60. initProc = cast(InitCommonControlsExProc) GetProcAddress(hmodCommonControls, "InitCommonControlsEx");
  61. if (!initProc)
  62. {
  63. //FreeLibrary(hmodCommonControls);
  64. no_comctl32:
  65. InitCommonControls();
  66. return;
  67. }
  68. }
  69. INITCOMMONCONTROLSEX icce;
  70. icce.dwSize = INITCOMMONCONTROLSEX.sizeof;
  71. icce.dwICC = dwControls;
  72. initProc(&icce);
  73. }
  74. // Taken from DFL, written by Christopher E. Miller
  75. void enableVisualStyles()
  76. {
  77. enum MANIFEST = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` "\r\n"
  78. `<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">` "\r\n"
  79. `<description>DFL manifest</description>` "\r\n"
  80. `<dependency>` "\r\n"
  81. `<dependentAssembly>` "\r\n"
  82. `<assemblyIdentity `
  83. `type="win32" `
  84. `name="Microsoft.Windows.Common-Controls" `
  85. `version="6.0.0.0" `
  86. `processorArchitecture="X86" `
  87. `publicKeyToken="6595b64144ccf1df" `
  88. `language="*" `
  89. `/>` "\r\n"
  90. `</dependentAssembly>` "\r\n"
  91. `</dependency>` "\r\n"
  92. `</assembly>` "\r\n";
  93. HMODULE kernel32;
  94. kernel32 = GetModuleHandleA("kernel32.dll");
  95. //if(kernel32)
  96. enforce(kernel32 !is null);
  97. {
  98. CreateActCtxWProc createActCtxW;
  99. createActCtxW = cast(CreateActCtxWProc) GetProcAddress(kernel32, "CreateActCtxW");
  100. if (createActCtxW)
  101. {
  102. //~ writeln("createActCtxW");
  103. GetTempPathWProc getTempPathW;
  104. GetTempFileNameWProc getTempFileNameW;
  105. ActivateActCtxProc activateActCtx;
  106. getTempPathW = cast(GetTempPathWProc) GetProcAddress(kernel32, "GetTempPathW");
  107. assert(getTempPathW !is null);
  108. getTempFileNameW = cast(GetTempFileNameWProc) GetProcAddress(kernel32, "GetTempFileNameW");
  109. assert(getTempFileNameW !is null);
  110. activateActCtx = cast(ActivateActCtxProc) GetProcAddress(kernel32, "ActivateActCtx");
  111. assert(activateActCtx !is null);
  112. DWORD pathlen;
  113. wchar[MAX_PATH] pathbuf = void;
  114. //if(pathbuf)
  115. {
  116. pathlen = getTempPathW(pathbuf.length, pathbuf.ptr);
  117. if (pathlen)
  118. {
  119. //~ writeln("pathlen");
  120. DWORD manifestlen;
  121. wchar[MAX_PATH] manifestbuf = void;
  122. //if(manifestbuf)
  123. {
  124. manifestlen = getTempFileNameW(pathbuf.ptr, "dmf", 0, manifestbuf.ptr);
  125. if (manifestlen)
  126. {
  127. //~ writeln("manifestlen");
  128. HANDLE hf;
  129. hf = CreateFileW(manifestbuf.ptr, GENERIC_WRITE, 0, null, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, HANDLE.init);
  130. if (hf != INVALID_HANDLE_VALUE)
  131. {
  132. //~ writeln("hf != INVALID_HANDLE_VALUE");
  133. DWORD written;
  134. if (WriteFile(hf, MANIFEST.ptr, MANIFEST.length, &written, null))
  135. {
  136. //~ writeln("WriteFile(hf, MANIFEST.ptr, MANIFEST.length, &written, null)");
  137. CloseHandle(hf);
  138. ACTCTXW ac;
  139. HANDLE hac;
  140. ac.cbSize = ACTCTXW.sizeof;
  141. //ac.dwFlags = 4; // ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID
  142. ac.dwFlags = 0;
  143. ac.lpSource = manifestbuf.ptr;
  144. //ac.lpAssemblyDirectory = pathbuf; // ?
  145. hac = createActCtxW(&ac);
  146. if (hac != INVALID_HANDLE_VALUE)
  147. {
  148. //~ writeln("hac != INVALID_HANDLE_VALUE");
  149. ULONG_PTR ul;
  150. activateActCtx(hac, &ul);
  151. _initCommonControls(ICC_STANDARD_CLASSES); // Yes.
  152. //InitCommonControls(); // No. Doesn't work with common controls version 6!
  153. // Ensure the actctx is actually associated with the message queue...
  154. PostMessageA(null, DWP_Msg, 0, 0);
  155. {
  156. MSG msg;
  157. PeekMessageA(&msg, null, DWP_Msg, DWP_Msg, PM_REMOVE);
  158. }
  159. }
  160. else
  161. {
  162. debug (APP_PRINT)
  163. cprintf("CreateActCtxW failed.\n");
  164. }
  165. }
  166. else
  167. {
  168. CloseHandle(hf);
  169. }
  170. }
  171. DeleteFileW(manifestbuf.ptr);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }