/src/Log2Console/Win32ApiCodePack/Core.cs

https://github.com/pmorton/Log2Console · C# · 260 lines · 155 code · 44 blank · 61 comment · 3 complexity · 22aa5931b442f158afdce9a758ef35c3 MD5 · raw file

  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Microsoft.WindowsAPICodePack.Internal
  4. {
  5. internal enum STPFLAG
  6. {
  7. STPF_NONE = 0x0,
  8. STPF_USEAPPTHUMBNAILALWAYS = 0x1,
  9. STPF_USEAPPTHUMBNAILWHENACTIVE = 0x2,
  10. STPF_USEAPPPEEKALWAYS = 0x4,
  11. STPF_USEAPPPEEKWHENACTIVE = 0x8
  12. }
  13. internal enum TBPFLAG
  14. {
  15. TBPF_NOPROGRESS = 0,
  16. TBPF_INDETERMINATE = 0x1,
  17. TBPF_NORMAL = 0x2,
  18. TBPF_ERROR = 0x4,
  19. TBPF_PAUSED = 0x8
  20. }
  21. internal enum THBMASK
  22. {
  23. THB_BITMAP = 0x1,
  24. THB_ICON = 0x2,
  25. THB_TOOLTIP = 0x4,
  26. THB_FLAGS = 0x8
  27. }
  28. [Flags]
  29. internal enum THBFLAGS
  30. {
  31. THBF_ENABLED = 0x00000000,
  32. THBF_DISABLED = 0x00000001,
  33. THBF_DISMISSONCLICK = 0x00000002,
  34. THBF_NOBACKGROUND = 0x00000004,
  35. THBF_HIDDEN = 0x00000008,
  36. THBF_NONINTERACTIVE = 0x00000010
  37. }
  38. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  39. internal struct THUMBBUTTON
  40. {
  41. /// <summary>
  42. /// WPARAM value for a THUMBBUTTON being clicked.
  43. /// </summary>
  44. internal const int THBN_CLICKED = 0x1800;
  45. [MarshalAs(UnmanagedType.U4)]
  46. internal THBMASK dwMask;
  47. internal uint iId;
  48. internal uint iBitmap;
  49. internal IntPtr hIcon;
  50. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  51. internal string szTip;
  52. [MarshalAs(UnmanagedType.U4)]
  53. internal THBFLAGS dwFlags;
  54. }
  55. /// <summary>
  56. /// HRESULT Wrapper
  57. /// This is intended for Library Internal use only.
  58. /// </summary>
  59. public enum HRESULT : uint
  60. {
  61. /// <summary>
  62. /// S_FALSE
  63. /// </summary>
  64. S_FALSE = 0x0001,
  65. /// <summary>
  66. /// S_OK
  67. /// </summary>
  68. S_OK = 0x0000,
  69. /// <summary>
  70. /// E_INVALIDARG
  71. /// </summary>
  72. E_INVALIDARG = 0x80070057,
  73. /// <summary>
  74. /// E_OUTOFMEMORY
  75. /// </summary>
  76. E_OUTOFMEMORY = 0x8007000E,
  77. /// <summary>
  78. /// E_NOINTERFACE
  79. /// </summary>
  80. E_NOINTERFACE = 0x80004002,
  81. /// <summary>
  82. /// E_FAIL
  83. /// </summary>
  84. E_FAIL = 0x80004005,
  85. /// <summary>
  86. /// E_ELEMENTNOTFOUND
  87. /// </summary>
  88. E_ELEMENTNOTFOUND = 0x80070490,
  89. /// <summary>
  90. /// TYPE_E_ELEMENTNOTFOUND
  91. /// </summary>
  92. TYPE_E_ELEMENTNOTFOUND = 0x8002802B,
  93. /// <summary>
  94. /// NO_OBJECT
  95. /// </summary>
  96. NO_OBJECT = 0x800401E5,
  97. /// <summary>
  98. /// Win32 Error code: ERROR_CANCELLED
  99. /// </summary>
  100. ERROR_CANCELLED = 1223,
  101. /// <summary>
  102. /// ERROR_CANCELLED
  103. /// </summary>
  104. E_ERROR_CANCELLED = 0x800704C7,
  105. /// <summary>
  106. /// The requested resource is in use
  107. /// </summary>
  108. RESOURCE_IN_USE = 0x800700AA,
  109. }
  110. [ComImport]
  111. [Guid("c43dc798-95d1-4bea-9030-bb99e2983a1a")]
  112. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  113. internal interface ITaskbarList4
  114. {
  115. // ITaskbarList
  116. [PreserveSig]
  117. void HrInit();
  118. [PreserveSig]
  119. void AddTab(IntPtr hwnd);
  120. [PreserveSig]
  121. void DeleteTab(IntPtr hwnd);
  122. [PreserveSig]
  123. void ActivateTab(IntPtr hwnd);
  124. [PreserveSig]
  125. void SetActiveAlt(IntPtr hwnd);
  126. // ITaskbarList2
  127. [PreserveSig]
  128. void MarkFullscreenWindow(IntPtr hwnd, [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
  129. // ITaskbarList3
  130. [PreserveSig]
  131. void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
  132. [PreserveSig]
  133. void SetProgressState(IntPtr hwnd, TBPFLAG tbpFlags);
  134. [PreserveSig]
  135. void RegisterTab(IntPtr hwndTab, IntPtr hwndMDI);
  136. [PreserveSig]
  137. void UnregisterTab(IntPtr hwndTab);
  138. [PreserveSig]
  139. void SetTabOrder(IntPtr hwndTab, IntPtr hwndInsertBefore);
  140. [PreserveSig]
  141. void SetTabActive(IntPtr hwndTab, IntPtr hwndInsertBefore, uint dwReserved);
  142. [PreserveSig]
  143. HRESULT ThumbBarAddButtons(IntPtr hwnd, uint cButtons, [MarshalAs(UnmanagedType.LPArray)] THUMBBUTTON[] pButtons);
  144. [PreserveSig]
  145. HRESULT ThumbBarUpdateButtons(IntPtr hwnd, uint cButtons, [MarshalAs(UnmanagedType.LPArray)] THUMBBUTTON[] pButtons);
  146. [PreserveSig]
  147. void ThumbBarSetImageList(IntPtr hwnd, IntPtr himl);
  148. [PreserveSig]
  149. void SetOverlayIcon(IntPtr hwnd, IntPtr hIcon, [MarshalAs(UnmanagedType.LPWStr)] string pszDescription);
  150. [PreserveSig]
  151. void SetThumbnailTooltip(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)] string pszTip);
  152. [PreserveSig]
  153. void SetThumbnailClip(IntPtr hwnd, IntPtr prcClip);
  154. // ITaskbarList4
  155. void SetTabProperties(IntPtr hwndTab, STPFLAG stpFlags);
  156. }
  157. [Guid("56FDF344-FD6D-11d0-958A-006097C9A090")]
  158. [ClassInterface(ClassInterfaceType.None)]
  159. [ComImport]
  160. internal class CTaskbarList { }
  161. internal static class CoreErrorHelper
  162. {
  163. /// <summary>
  164. /// This is intended for Library Internal use only.
  165. /// </summary>
  166. /// <param name="hresult">The error code.</param>
  167. /// <returns>True if the error code indicates success.</returns>
  168. public static bool Succeeded(int hresult)
  169. {
  170. return (hresult >= 0);
  171. }
  172. }
  173. internal class CoreHelpers
  174. {
  175. /// <summary>
  176. /// Determines if the application is running on Windows Vista or later 7
  177. /// </summary>
  178. public static bool RunningOnVistaOrLater
  179. {
  180. get
  181. {
  182. return Environment.OSVersion.Version.Major >= 6;
  183. }
  184. }
  185. /// <summary>
  186. /// Determines if the application is running on Windows 7
  187. /// </summary>
  188. public static bool RunningOnWin7
  189. {
  190. get
  191. {
  192. return (Environment.OSVersion.Version.Major > 6) ||
  193. (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 1);
  194. }
  195. }
  196. /// <summary>
  197. /// Throws PlatformNotSupportedException if the application is not running on Windows 7
  198. /// </summary>
  199. public static void ThrowIfNotWin7()
  200. {
  201. if (!RunningOnWin7)
  202. throw new PlatformNotSupportedException("Only supported on Windows 7 or newer.");
  203. }
  204. }
  205. internal static class CoreNativeMethods
  206. {
  207. public static int LOWORD(long dword)
  208. {
  209. return (short)(dword & 0xFFFF);
  210. }
  211. public static int HIWORD(long dword, int size)
  212. {
  213. return (short)(dword >> size);
  214. }
  215. }
  216. }