Win7及以上系统支持任务栏进度条,为有进度类应用显示进度于任务栏,甚为方便。

以c#之WinForm实现其,大多采用Windows API Code Pack这个方案,加多引用,比较繁琐,而我总也打不开了其页面。

鄙人不喜欢多引用东西,即寻求方法抽取其相关代码,简化其应用。费些工夫,实现效果。

一、TaskbarManager

此为抽取必要代码而组成同名类,全部代码如下:

  1. //抽取TaskBar代码,用其设置任务栏进度部分
  2. //Copyright (c) Microsoft Corporation. All rights reserved.
  3.  
  4. using System;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7.  
  8. namespace wApp
  9. {
  10. /// <summary>
  11. /// Represents an instance of the Windows taskbar
  12. /// </summary>
  13. public class TaskbarManager
  14. {
  15. // Hide the default constructor
  16. private TaskbarManager()
  17. {
  18. }
  19.  
  20. // Best practice recommends defining a private object to lock on
  21. private static object _syncLock = new object();
  22.  
  23. private static TaskbarManager _instance;
  24. /// <summary>
  25. /// Represents an instance of the Windows Taskbar
  26. /// </summary>
  27. public static TaskbarManager Instance
  28. {
  29. get
  30. {
  31. if (_instance == null)
  32. {
  33. lock (_syncLock)
  34. {
  35. if (_instance == null)
  36. _instance = new TaskbarManager();
  37. }
  38. }
  39.  
  40. return _instance;
  41. }
  42. }
  43.  
  44. /// <summary>
  45. /// Indicates whether this feature is supported on the current platform.
  46. /// </summary>
  47. public static bool IsPlatformSupported
  48. {
  49. get { return Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.CompareTo(new Version(, )) >= ; }
  50. }
  51.  
  52. /// <summary>
  53. /// Displays or updates a progress bar hosted in a taskbar button of the main application window
  54. /// to show the specific percentage completed of the full operation.
  55. /// </summary>
  56. /// <param name="currentValue">An application-defined value that indicates the proportion of the operation that has been completed at the time the method is called.</param>
  57. /// <param name="maximumValue">An application-defined value that specifies the value currentValue will have when the operation is complete.</param>
  58. public void SetProgressValue(int currentValue, int maximumValue)
  59. {
  60. if (IsPlatformSupported)
  61. TaskbarList.Instance.SetProgressValue(
  62. OwnerHandle,
  63. Convert.ToUInt32(currentValue),
  64. Convert.ToUInt32(maximumValue));
  65. }
  66.  
  67. /// <summary>
  68. /// Displays or updates a progress bar hosted in a taskbar button of the given window handle
  69. /// to show the specific percentage completed of the full operation.
  70. /// </summary>
  71. /// <param name="windowHandle">The handle of the window whose associated taskbar button is being used as a progress indicator.
  72. /// This window belong to a calling process associated with the button's application and must be already loaded.</param>
  73. /// <param name="currentValue">An application-defined value that indicates the proportion of the operation that has been completed at the time the method is called.</param>
  74. /// <param name="maximumValue">An application-defined value that specifies the value currentValue will have when the operation is complete.</param>
  75. public void SetProgressValue(int currentValue, int maximumValue, IntPtr windowHandle)
  76. {
  77. if (IsPlatformSupported)
  78. TaskbarList.Instance.SetProgressValue(
  79. windowHandle,
  80. Convert.ToUInt32(currentValue),
  81. Convert.ToUInt32(maximumValue));
  82. }
  83.  
  84. /// <summary>
  85. /// Sets the type and state of the progress indicator displayed on a taskbar button of the main application window.
  86. /// </summary>
  87. /// <param name="state">Progress state of the progress button</param>
  88. public void SetProgressState(TaskbarProgressBarState state)
  89. {
  90. if (IsPlatformSupported)
  91. TaskbarList.Instance.SetProgressState(OwnerHandle, (TaskbarProgressBarStatus)state);
  92. }
  93.  
  94. /// <summary>
  95. /// Sets the type and state of the progress indicator displayed on a taskbar button
  96. /// of the given window handle
  97. /// </summary>
  98. /// <param name="windowHandle">The handle of the window whose associated taskbar button is being used as a progress indicator.
  99. /// This window belong to a calling process associated with the button's application and must be already loaded.</param>
  100. /// <param name="state">Progress state of the progress button</param>
  101. public void SetProgressState(TaskbarProgressBarState state, IntPtr windowHandle)
  102. {
  103. if (IsPlatformSupported)
  104. TaskbarList.Instance.SetProgressState(windowHandle, (TaskbarProgressBarStatus)state);
  105. }
  106.  
  107. private IntPtr _ownerHandle;
  108. /// <summary>
  109. /// Sets the handle of the window whose taskbar button will be used
  110. /// to display progress.
  111. /// </summary>
  112. internal IntPtr OwnerHandle
  113. {
  114. get
  115. {
  116. if (_ownerHandle == IntPtr.Zero)
  117. {
  118. Process currentProcess = Process.GetCurrentProcess();
  119.  
  120. if (currentProcess != null && currentProcess.MainWindowHandle != IntPtr.Zero)
  121. _ownerHandle = currentProcess.MainWindowHandle;
  122. }
  123.  
  124. return _ownerHandle;
  125. }
  126. }
  127. }
  128.  
  129. /// <summary>
  130. /// Represents the thumbnail progress bar state.
  131. /// </summary>
  132. public enum TaskbarProgressBarState
  133. {
  134. /// <summary>
  135. /// No progress is displayed.
  136. /// </summary>
  137. NoProgress = ,
  138.  
  139. /// <summary>
  140. /// The progress is indeterminate (marquee).
  141. /// </summary>
  142. Indeterminate = 0x1,
  143.  
  144. /// <summary>
  145. /// Normal progress is displayed.
  146. /// </summary>
  147. Normal = 0x2,
  148.  
  149. /// <summary>
  150. /// An error occurred (red).
  151. /// </summary>
  152. Error = 0x4,
  153.  
  154. /// <summary>
  155. /// The operation is paused (yellow).
  156. /// </summary>
  157. Paused = 0x8
  158. }
  159.  
  160. /// <summary>
  161. /// Provides internal access to the functions provided by the ITaskbarList4 interface,
  162. /// without being forced to refer to it through another singleton.
  163. /// </summary>
  164. internal static class TaskbarList
  165. {
  166. private static object _syncLock = new object();
  167.  
  168. private static ITaskbarList4 _taskbarList;
  169. internal static ITaskbarList4 Instance
  170. {
  171. get
  172. {
  173. if (_taskbarList == null)
  174. {
  175. lock (_syncLock)
  176. {
  177. if (_taskbarList == null)
  178. {
  179. _taskbarList = (ITaskbarList4)new CTaskbarList();
  180. _taskbarList.HrInit();
  181. }
  182. }
  183. }
  184.  
  185. return _taskbarList;
  186. }
  187. }
  188. }
  189.  
  190. [GuidAttribute("56FDF344-FD6D-11d0-958A-006097C9A090")]
  191. [ClassInterfaceAttribute(ClassInterfaceType.None)]
  192. [ComImportAttribute()]
  193. internal class CTaskbarList { }
  194.  
  195. [ComImportAttribute()]
  196. [GuidAttribute("c43dc798-95d1-4bea-9030-bb99e2983a1a")]
  197. [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
  198. internal interface ITaskbarList4
  199. {
  200. // ITaskbarList
  201. [PreserveSig]
  202. void HrInit();
  203. [PreserveSig]
  204. void AddTab(IntPtr hwnd);
  205. [PreserveSig]
  206. void DeleteTab(IntPtr hwnd);
  207. [PreserveSig]
  208. void ActivateTab(IntPtr hwnd);
  209. [PreserveSig]
  210. void SetActiveAlt(IntPtr hwnd);
  211.  
  212. // ITaskbarList2
  213. [PreserveSig]
  214. void MarkFullscreenWindow(
  215. IntPtr hwnd,
  216. [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
  217.  
  218. // ITaskbarList3
  219. [PreserveSig]
  220. void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
  221. [PreserveSig]
  222. void SetProgressState(IntPtr hwnd, TaskbarProgressBarStatus tbpFlags);
  223. [PreserveSig]
  224. void RegisterTab(IntPtr hwndTab, IntPtr hwndMDI);
  225. [PreserveSig]
  226. void UnregisterTab(IntPtr hwndTab);
  227. [PreserveSig]
  228. void SetTabOrder(IntPtr hwndTab, IntPtr hwndInsertBefore);
  229. [PreserveSig]
  230. void SetTabActive(IntPtr hwndTab, IntPtr hwndInsertBefore, uint dwReserved);
  231. [PreserveSig]
  232. HResult ThumbBarAddButtons(
  233. IntPtr hwnd,
  234. uint cButtons,
  235. [MarshalAs(UnmanagedType.LPArray)] ThumbButton[] pButtons);
  236. [PreserveSig]
  237. HResult ThumbBarUpdateButtons(
  238. IntPtr hwnd,
  239. uint cButtons,
  240. [MarshalAs(UnmanagedType.LPArray)] ThumbButton[] pButtons);
  241. [PreserveSig]
  242. void ThumbBarSetImageList(IntPtr hwnd, IntPtr himl);
  243. [PreserveSig]
  244. void SetOverlayIcon(
  245. IntPtr hwnd,
  246. IntPtr hIcon,
  247. [MarshalAs(UnmanagedType.LPWStr)] string pszDescription);
  248. [PreserveSig]
  249. void SetThumbnailTooltip(
  250. IntPtr hwnd,
  251. [MarshalAs(UnmanagedType.LPWStr)] string pszTip);
  252. [PreserveSig]
  253. void SetThumbnailClip(
  254. IntPtr hwnd,
  255. IntPtr prcClip);
  256.  
  257. // ITaskbarList4
  258. void SetTabProperties(IntPtr hwndTab, SetTabPropertiesOption stpFlags);
  259. }
  260.  
  261. internal enum TaskbarProgressBarStatus
  262. {
  263. NoProgress = ,
  264. Indeterminate = 0x1,
  265. Normal = 0x2,
  266. Error = 0x4,
  267. Paused = 0x8
  268. }
  269.  
  270. internal enum ThumbButtonMask
  271. {
  272. Bitmap = 0x1,
  273. Icon = 0x2,
  274. Tooltip = 0x4,
  275. THB_FLAGS = 0x8
  276. }
  277.  
  278. [Flags]
  279. internal enum ThumbButtonOptions
  280. {
  281. Enabled = 0x00000000,
  282. Disabled = 0x00000001,
  283. DismissOnClick = 0x00000002,
  284. NoBackground = 0x00000004,
  285. Hidden = 0x00000008,
  286. NonInteractive = 0x00000010
  287. }
  288.  
  289. internal enum SetTabPropertiesOption
  290. {
  291. None = 0x0,
  292. UseAppThumbnailAlways = 0x1,
  293. UseAppThumbnailWhenActive = 0x2,
  294. UseAppPeekAlways = 0x4,
  295. UseAppPeekWhenActive = 0x8
  296. }
  297.  
  298. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  299. internal struct ThumbButton
  300. {
  301. /// <summary>
  302. /// WPARAM value for a THUMBBUTTON being clicked.
  303. /// </summary>
  304. internal const int Clicked = 0x1800;
  305.  
  306. [MarshalAs(UnmanagedType.U4)]
  307. internal ThumbButtonMask Mask;
  308. internal uint Id;
  309. internal uint Bitmap;
  310. internal IntPtr Icon;
  311. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
  312. internal string Tip;
  313. [MarshalAs(UnmanagedType.U4)]
  314. internal ThumbButtonOptions Flags;
  315. }
  316.  
  317. /// <summary>
  318. /// HRESULT Wrapper
  319. /// </summary>
  320. public enum HResult
  321. {
  322. /// <summary>
  323. /// S_OK
  324. /// </summary>
  325. Ok = 0x0000,
  326.  
  327. /// <summary>
  328. /// S_FALSE
  329. /// </summary>
  330. False = 0x0001,
  331.  
  332. /// <summary>
  333. /// E_INVALIDARG
  334. /// </summary>
  335. InvalidArguments = unchecked((int)0x80070057),
  336.  
  337. /// <summary>
  338. /// E_OUTOFMEMORY
  339. /// </summary>
  340. OutOfMemory = unchecked((int)0x8007000E),
  341.  
  342. /// <summary>
  343. /// E_NOINTERFACE
  344. /// </summary>
  345. NoInterface = unchecked((int)0x80004002),
  346.  
  347. /// <summary>
  348. /// E_FAIL
  349. /// </summary>
  350. Fail = unchecked((int)0x80004005),
  351.  
  352. /// <summary>
  353. /// E_ELEMENTNOTFOUND
  354. /// </summary>
  355. ElementNotFound = unchecked((int)0x80070490),
  356.  
  357. /// <summary>
  358. /// TYPE_E_ELEMENTNOTFOUND
  359. /// </summary>
  360. TypeElementNotFound = unchecked((int)0x8002802B),
  361.  
  362. /// <summary>
  363. /// NO_OBJECT
  364. /// </summary>
  365. NoObject = unchecked((int)0x800401E5),
  366.  
  367. /// <summary>
  368. /// Win32 Error code: ERROR_CANCELLED
  369. /// </summary>
  370. Win32ErrorCanceled = ,
  371.  
  372. /// <summary>
  373. /// ERROR_CANCELLED
  374. /// </summary>
  375. Canceled = unchecked((int)0x800704C7),
  376.  
  377. /// <summary>
  378. /// The requested resource is in use
  379. /// </summary>
  380. ResourceInUse = unchecked((int)0x800700AA),
  381.  
  382. /// <summary>
  383. /// The requested resources is read-only.
  384. /// </summary>
  385. AccessDenied = unchecked((int)0x80030005)
  386. }
  387. }

静态类实现方法(推荐使用这个):

  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace wApp
  6. {
  7. /// <summary>
  8. /// Represents an instance of the Windows taskbar
  9. /// </summary>
  10. public static class TaskbarManager
  11. {
  12. /// <summary>
  13. /// Sets the handle of the window whose taskbar button will be used
  14. /// to display progress.
  15. /// </summary>
  16. private static IntPtr ownerHandle = IntPtr.Zero;
  17.  
  18. static TaskbarManager()
  19. {
  20. var currentProcess = Process.GetCurrentProcess();
  21. if (currentProcess != null && currentProcess.MainWindowHandle != IntPtr.Zero)
  22. ownerHandle = currentProcess.MainWindowHandle;
  23. }
  24.  
  25. /// <summary>
  26. /// Indicates whether this feature is supported on the current platform.
  27. /// </summary>
  28. private static bool IsPlatformSupported
  29. {
  30. get { return Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.CompareTo(new Version(, )) >= ; }
  31. }
  32.  
  33. /// <summary>
  34. /// Displays or updates a progress bar hosted in a taskbar button of the main application window
  35. /// to show the specific percentage completed of the full operation.
  36. /// </summary>
  37. /// <param name="currentValue">An application-defined value that indicates the proportion of the operation that has been completed at the time the method is called.</param>
  38. /// <param name="maximumValue">An application-defined value that specifies the value currentValue will have when the operation is complete.</param>
  39. public static void SetProgressValue(int currentValue, int maximumValue)
  40. {
  41. if (IsPlatformSupported && ownerHandle != IntPtr.Zero)
  42. TaskbarList.Instance.SetProgressValue(
  43. ownerHandle,
  44. Convert.ToUInt32(currentValue),
  45. Convert.ToUInt32(maximumValue));
  46. }
  47.  
  48. /// <summary>
  49. /// Displays or updates a progress bar hosted in a taskbar button of the given window handle
  50. /// to show the specific percentage completed of the full operation.
  51. /// </summary>
  52. /// <param name="windowHandle">The handle of the window whose associated taskbar button is being used as a progress indicator.
  53. /// This window belong to a calling process associated with the button's application and must be already loaded.</param>
  54. /// <param name="currentValue">An application-defined value that indicates the proportion of the operation that has been completed at the time the method is called.</param>
  55. /// <param name="maximumValue">An application-defined value that specifies the value currentValue will have when the operation is complete.</param>
  56. public static void SetProgressValue(int currentValue, int maximumValue, IntPtr windowHandle)
  57. {
  58. if (IsPlatformSupported)
  59. TaskbarList.Instance.SetProgressValue(
  60. windowHandle,
  61. Convert.ToUInt32(currentValue),
  62. Convert.ToUInt32(maximumValue));
  63. }
  64.  
  65. /// <summary>
  66. /// Sets the type and state of the progress indicator displayed on a taskbar button of the main application window.
  67. /// </summary>
  68. /// <param name="state">Progress state of the progress button</param>
  69. public static void SetProgressState(TaskbarProgressBarState state)
  70. {
  71. if (IsPlatformSupported && ownerHandle != IntPtr.Zero)
  72. TaskbarList.Instance.SetProgressState(ownerHandle, (TaskbarProgressBarStatus)state);
  73. }
  74.  
  75. /// <summary>
  76. /// Sets the type and state of the progress indicator displayed on a taskbar button
  77. /// of the given window handle
  78. /// </summary>
  79. /// <param name="windowHandle">The handle of the window whose associated taskbar button is being used as a progress indicator.
  80. /// This window belong to a calling process associated with the button's application and must be already loaded.</param>
  81. /// <param name="state">Progress state of the progress button</param>
  82. public static void SetProgressState(TaskbarProgressBarState state, IntPtr windowHandle)
  83. {
  84. if (IsPlatformSupported)
  85. TaskbarList.Instance.SetProgressState(windowHandle, (TaskbarProgressBarStatus)state);
  86. }
  87. }
  88.  
  89. /// <summary>
  90. /// Represents the thumbnail progress bar state.
  91. /// </summary>
  92. public enum TaskbarProgressBarState
  93. {
  94. /// <summary>
  95. /// No progress is displayed.
  96. /// </summary>
  97. NoProgress = ,
  98.  
  99. /// <summary>
  100. /// The progress is indeterminate (marquee).
  101. /// </summary>
  102. Indeterminate = 0x1,
  103.  
  104. /// <summary>
  105. /// Normal progress is displayed.
  106. /// </summary>
  107. Normal = 0x2,
  108.  
  109. /// <summary>
  110. /// An error occurred (red).
  111. /// </summary>
  112. Error = 0x4,
  113.  
  114. /// <summary>
  115. /// The operation is paused (yellow).
  116. /// </summary>
  117. Paused = 0x8
  118. }
  119.  
  120. /// <summary>
  121. /// Provides internal access to the functions provided by the ITaskbarList4 interface,
  122. /// without being forced to refer to it through another singleton.
  123. /// </summary>
  124. internal static class TaskbarList
  125. {
  126. private static object _syncLock = new object();
  127.  
  128. private static ITaskbarList4 _taskbarList;
  129. internal static ITaskbarList4 Instance
  130. {
  131. get
  132. {
  133. if (_taskbarList == null)
  134. {
  135. lock (_syncLock)
  136. {
  137. if (_taskbarList == null)
  138. {
  139. _taskbarList = (ITaskbarList4)new CTaskbarList();
  140. _taskbarList.HrInit();
  141. }
  142. }
  143. }
  144.  
  145. return _taskbarList;
  146. }
  147. }
  148. }
  149.  
  150. [GuidAttribute("56FDF344-FD6D-11d0-958A-006097C9A090")]
  151. [ClassInterfaceAttribute(ClassInterfaceType.None)]
  152. [ComImportAttribute()]
  153. internal class CTaskbarList { }
  154.  
  155. [ComImportAttribute()]
  156. [GuidAttribute("c43dc798-95d1-4bea-9030-bb99e2983a1a")]
  157. [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
  158. internal interface ITaskbarList4
  159. {
  160. // ITaskbarList
  161. [PreserveSig]
  162. void HrInit();
  163. [PreserveSig]
  164. void AddTab(IntPtr hwnd);
  165. [PreserveSig]
  166. void DeleteTab(IntPtr hwnd);
  167. [PreserveSig]
  168. void ActivateTab(IntPtr hwnd);
  169. [PreserveSig]
  170. void SetActiveAlt(IntPtr hwnd);
  171.  
  172. // ITaskbarList2
  173. [PreserveSig]
  174. void MarkFullscreenWindow(
  175. IntPtr hwnd,
  176. [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
  177.  
  178. // ITaskbarList3
  179. [PreserveSig]
  180. void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
  181. [PreserveSig]
  182. void SetProgressState(IntPtr hwnd, TaskbarProgressBarStatus tbpFlags);
  183. [PreserveSig]
  184. void RegisterTab(IntPtr hwndTab, IntPtr hwndMDI);
  185. [PreserveSig]
  186. void UnregisterTab(IntPtr hwndTab);
  187. [PreserveSig]
  188. void SetTabOrder(IntPtr hwndTab, IntPtr hwndInsertBefore);
  189. [PreserveSig]
  190. void SetTabActive(IntPtr hwndTab, IntPtr hwndInsertBefore, uint dwReserved);
  191. [PreserveSig]
  192. HResult ThumbBarAddButtons(
  193. IntPtr hwnd,
  194. uint cButtons,
  195. [MarshalAs(UnmanagedType.LPArray)] ThumbButton[] pButtons);
  196. [PreserveSig]
  197. HResult ThumbBarUpdateButtons(
  198. IntPtr hwnd,
  199. uint cButtons,
  200. [MarshalAs(UnmanagedType.LPArray)] ThumbButton[] pButtons);
  201. [PreserveSig]
  202. void ThumbBarSetImageList(IntPtr hwnd, IntPtr himl);
  203. [PreserveSig]
  204. void SetOverlayIcon(
  205. IntPtr hwnd,
  206. IntPtr hIcon,
  207. [MarshalAs(UnmanagedType.LPWStr)] string pszDescription);
  208. [PreserveSig]
  209. void SetThumbnailTooltip(
  210. IntPtr hwnd,
  211. [MarshalAs(UnmanagedType.LPWStr)] string pszTip);
  212. [PreserveSig]
  213. void SetThumbnailClip(
  214. IntPtr hwnd,
  215. IntPtr prcClip);
  216.  
  217. // ITaskbarList4
  218. void SetTabProperties(IntPtr hwndTab, SetTabPropertiesOption stpFlags);
  219. }
  220.  
  221. internal enum TaskbarProgressBarStatus
  222. {
  223. NoProgress = ,
  224. Indeterminate = 0x1,
  225. Normal = 0x2,
  226. Error = 0x4,
  227. Paused = 0x8
  228. }
  229.  
  230. internal enum ThumbButtonMask
  231. {
  232. Bitmap = 0x1,
  233. Icon = 0x2,
  234. Tooltip = 0x4,
  235. THB_FLAGS = 0x8
  236. }
  237.  
  238. [Flags]
  239. internal enum ThumbButtonOptions
  240. {
  241. Enabled = 0x00000000,
  242. Disabled = 0x00000001,
  243. DismissOnClick = 0x00000002,
  244. NoBackground = 0x00000004,
  245. Hidden = 0x00000008,
  246. NonInteractive = 0x00000010
  247. }
  248.  
  249. internal enum SetTabPropertiesOption
  250. {
  251. None = 0x0,
  252. UseAppThumbnailAlways = 0x1,
  253. UseAppThumbnailWhenActive = 0x2,
  254. UseAppPeekAlways = 0x4,
  255. UseAppPeekWhenActive = 0x8
  256. }
  257.  
  258. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  259. internal struct ThumbButton
  260. {
  261. /// <summary>
  262. /// WPARAM value for a THUMBBUTTON being clicked.
  263. /// </summary>
  264. internal const int Clicked = 0x1800;
  265.  
  266. [MarshalAs(UnmanagedType.U4)]
  267. internal ThumbButtonMask Mask;
  268. internal uint Id;
  269. internal uint Bitmap;
  270. internal IntPtr Icon;
  271. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
  272. internal string Tip;
  273. [MarshalAs(UnmanagedType.U4)]
  274. internal ThumbButtonOptions Flags;
  275. }
  276.  
  277. /// <summary>
  278. /// HRESULT Wrapper
  279. /// </summary>
  280. public enum HResult
  281. {
  282. /// <summary>
  283. /// S_OK
  284. /// </summary>
  285. Ok = 0x0000,
  286.  
  287. /// <summary>
  288. /// S_FALSE
  289. /// </summary>
  290. False = 0x0001,
  291.  
  292. /// <summary>
  293. /// E_INVALIDARG
  294. /// </summary>
  295. InvalidArguments = unchecked((int)0x80070057),
  296.  
  297. /// <summary>
  298. /// E_OUTOFMEMORY
  299. /// </summary>
  300. OutOfMemory = unchecked((int)0x8007000E),
  301.  
  302. /// <summary>
  303. /// E_NOINTERFACE
  304. /// </summary>
  305. NoInterface = unchecked((int)0x80004002),
  306.  
  307. /// <summary>
  308. /// E_FAIL
  309. /// </summary>
  310. Fail = unchecked((int)0x80004005),
  311.  
  312. /// <summary>
  313. /// E_ELEMENTNOTFOUND
  314. /// </summary>
  315. ElementNotFound = unchecked((int)0x80070490),
  316.  
  317. /// <summary>
  318. /// TYPE_E_ELEMENTNOTFOUND
  319. /// </summary>
  320. TypeElementNotFound = unchecked((int)0x8002802B),
  321.  
  322. /// <summary>
  323. /// NO_OBJECT
  324. /// </summary>
  325. NoObject = unchecked((int)0x800401E5),
  326.  
  327. /// <summary>
  328. /// Win32 Error code: ERROR_CANCELLED
  329. /// </summary>
  330. Win32ErrorCanceled = ,
  331.  
  332. /// <summary>
  333. /// ERROR_CANCELLED
  334. /// </summary>
  335. Canceled = unchecked((int)0x800704C7),
  336.  
  337. /// <summary>
  338. /// The requested resource is in use
  339. /// </summary>
  340. ResourceInUse = unchecked((int)0x800700AA),
  341.  
  342. /// <summary>
  343. /// The requested resources is read-only.
  344. /// </summary>
  345. AccessDenied = unchecked((int)0x80030005)
  346. }
  347. }

二、使用方法

它有一静态公用变量Instance,直引用即可;或以静态类直接引用,而不再加以.Instance。以引用静态类为例:

  1. private void trackBar_ValueChanged(object sender, EventArgs e)
  2. {
  3. TaskbarManager.SetProgressValue(trackBar.Value, trackBar.Maximum);
  4. }
  5.  
  6. private void btnNoProgress_Click(object sender, EventArgs e)
  7. {
  8. TaskbarManager.SetProgressState(TaskbarProgressBarState.NoProgress);
  9. }
  10.  
  11. private void btnIndeterminate_Click(object sender, EventArgs e)
  12. {
  13. TaskbarManager.SetProgressState(TaskbarProgressBarState.Indeterminate);
  14. }
  15.  
  16. private void btnNormal_Click(object sender, EventArgs e)
  17. {
  18. TaskbarManager.SetProgressState(TaskbarProgressBarState.Normal);
  19. TaskbarManager.SetProgressValue(trackBar.Value, trackBar.Maximum);
  20. }
  21.  
  22. private void btn_Click(object sender, EventArgs e)
  23. {
  24. TaskbarManager.SetProgressState(TaskbarProgressBarState.Error);
  25. TaskbarManager.SetProgressValue(trackBar.Value, trackBar.Maximum);
  26. }
  27.  
  28. private void btnPaused_Click(object sender, EventArgs e)
  29. {
  30. TaskbarManager.SetProgressState(TaskbarProgressBarState.Paused);
  31. TaskbarManager.SetProgressValue(trackBar.Value, trackBar.Maximum);
  32. }

三、效果如下图示:

参考资料:

Windows 7 progress bar in taskbar in C#? - Stack Overflow

dbarros/WindowsAPICodePack: A fork of the Windows API Code Pack with additional fixes and features by yours truly.

c#: 任务栏进度显示(TaskbarManager)的更多相关文章

  1. Qt创建任务栏进度条

    一.正文 任务栏进度条是Windows7就引入的一种UI形式,通常用于显示软件当前正在执行的任务的进度(如编译程序的进度.下载任务的进度).如下: 在Qt中使用任务栏进度条也是非常容易的一件事情.Qt ...

  2. TaskBarProgress(任务栏进度条)

    原文:TaskBarProgress(任务栏进度条) </Grid> { { InitializeComponent(); Loaded += } { BackgroundWorker w ...

  3. html5 图片上传,支持图片预览、压缩、及进度显示,兼容IE6+及标准浏览器

    以前写过上传组件,见 打造 html5 文件上传组件,实现进度显示及拖拽上传,兼容IE6+及其它标准浏览器,对付一般的上传没有问题,不过如果是上传图片,且需要预览的话,就力有不逮了,趁着闲暇时间,给上 ...

  4. Retrofit2文件上传下载及其进度显示

    序 前面一篇文章介绍了Retrofit2的基本使用,这篇文章接着介绍使用Retrofit2实现文件上传和文件下载,以及上传下载过程中如何实现进度的显示. 文件上传 定义接口 1 2 3 @Multip ...

  5. [cocos2d]场景切换以及切换进度显示

    本文主要分两个部分叙述,第一是场景切换,第二是场景切换的进度显示. 一.场景切换 参考learn-iphone-and-ipad-cocos2d-game-development 第五章内容 coco ...

  6. Python多线程同步命令行模拟进度显示

    最近在一个Python(3.5)的小项目中需要用到多线程加快处理速度,同时需要显示进度,于是查了些资料找到几个实现方法:线程池的map-reduce和Queue结合线程的实现.这里简单的实例介绍一下Q ...

  7. nginx上传模块nginx_upload_module和nginx_uploadprogress_module模块进度显示,如何传递GET参数等。

    ownload:http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gzconfigure and make : . ...

  8. 超赞的CSS3进度条 可以随进度显示不同颜色

    原文:超赞的CSS3进度条 可以随进度显示不同颜色 现在的WEB已经不是以前的WEB了,传输更大的数据量,有着更加复杂的计算,这就需要利用进度条来提高用户体验,必要时可以让用户耐心等待,不至于因操作卡 ...

  9. WPF中任务栏只显示主窗口

    我们在用WPF开发的时候,常常会遇到在主窗口打开的情况下,去显示子窗口,而此时任务栏同时显示主窗口与子窗口.这样看起来很不美观.所以在弹出子窗口之前,设置它的几个相应属性,便不会出现这种问题了. // ...

随机推荐

  1. 排序算法练习--JAVA(:内部排序:插入、选择、冒泡、快速排序)

    排序算法是数据结构中的经典算法知识点,也是笔试面试中经常考察的问题,平常学的不扎实笔试时候容易出洋相,回来恶补,尤其是碰到递归很可能被问到怎么用非递归实现... 内部排序: 插入排序:直接插入排序 选 ...

  2. Vue创建头部组件示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...

  3. 记一次php脚本memory exhausted

    表象报错如下: Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16651985 bytes) 出 ...

  4. 3.3-1933 problem A

    #include <stdio.h> int main(void){ int h; while(scanf("%d", &h) != EOF){ * (h-); ...

  5. JAVA AES CBC 加密 解密

    AES 256 , KEY 的长度为 32字节(32*8=256bit). AES 128 , KEY 的长度为 16字节(16*8=128bit) CBC 模式需要IV, IV的值是固定写死,还是当 ...

  6. C++类中this指针的理解

    先要理解class的意思.class应该理解为一种类型,象int,char一样,是用户自定义的类型.用这个类型可以来声明一个变量,比如int x, myclass my等等.这样就像变量x具有int类 ...

  7. Entity Framework Code first 可能会导致循环或多个级联路径.

    用code first映射数据库报错 Introducing FOREIGN KEY constraint 'FK_dbo.Roles_dbo.SubSystems_SubSystemID' on t ...

  8. 前端-JavaScript1-1——JavaScript简介

    1.1 JavaScript用途 前端三层: 结构层   HTML           从语义的角度描述页面的结构 样式层   CSS               从审美的角度装饰页面 行为层   J ...

  9. 测试WCF遇到的一些问题

    win7+iis7 1.localhost访问bad request错误. 主机地址不要指定为127.0.0.1.设置为”全部未分配“. 2.错误 500.19(由于权限不足而无法读取配置文件)的问题 ...

  10. js基础系列之【作用域】

    声明:形成本文的出发点仅仅是个人总结记录,避免遗忘,并非详实的教程:文中引用了经过个人加工的其它作者的内容,并非原创.学海无涯 什么是作用域? 作用域就是一套规则,用于确定在何处以及如何查找变量(标识 ...