In the previous article, we completed the Overlay Icon development. In this article, we will study the progress bar feature. When you use IE to download a file, the taskbar icon synchronously displays the current download progress (for example ). How can this effect be achieved in applications?
Download Status
TaskbarManager. SetProgressValue Method
In the TaskbarManager class, you can use the SetProgressValue method to set the current position of the progress bar. currentValue is the parameter of the current position of the progress bar, And maximumValue is the maximum parameter:
// Set the current window public void SetProgressValue (int currentValue, int maximumValue) {CoreHelpers. ThrowIfNotWin7 (); TaskbarList. SetProgressValue (OwnerHandle, Convert. ToUInt32 (currentValue ),
Convert. ToUInt32 (maximumValue);} // you can specify the public void SetProgressValue (int currentValue, int maximumValue,
IntPtr when whandle) {CoreHelpers. ThrowIfNotWin7 (); TaskbarList. SetProgressValue (when whandle, Convert. ToUInt32 (currentValue ),
Convert. ToUInt32 (maximumValue);} // you can specify the public void SetProgressValue (int currentValue, int maximumValue,
System. windows. window window) {CoreHelpers. throwIfNotWin7 (); TaskbarList. setProgressValue (new WindowInteropHelper (window )). handle, Convert. toUInt32 (currentValue), Convert. toUInt32 (maximumValue ));}
TaskbarProgressBarState progress bar status
In addition to the common green (Normal state), you can also use other States by calling the TaskbarProgressBarState enumeration. See the following table:
Name |
Description |
NoProgress |
Do not show progress bar |
Indeterminate |
Variable value progress bar (rolling) |
Normal |
Normal (green) |
Error |
Error status (red) |
Paused |
Paused (yellow) |
TaskbarManager. SetProgressState Method
How can I set the above status for the progress bar? Of course, TaskbarManager also provides three different ways to use the SetProgressState method to set the progress bar status:
// Set the current window state public void SetProgressState (TaskbarProgressBarState state) {CoreHelpers. throwIfNotWin7 (); TaskbarList. setProgressState (OwnerHandle, (TBPFLAG) state);} // set the public void SetProgressState (TaskbarProgressBarState state, IntPtr implements whandle) {CoreHelpers. throwIfNotWin7 (); TaskbarList. setProgressState (windowHandle, (TBPFLAG) state);} // you can specify the public void SetProgressState (TaskbarProgressBarState state,
System. Windows. Window window) {CoreHelpers. ThrowIfNotWin7 (); TaskbarList. SetProgressState (new WindowInteropHelper (window). Handle, (TBPFLAG) state );}
Effect demonstration
The following code controls the progress bar by using the above method in the program. The following code uses the Slider to adjust the current value of the progress bar:
TaskbarManager.Instance.SetProgressValue((int)progressSlider.Value, 100);TaskbarManager.Instance.SetProgressState(
(TaskbarProgressBarState)progressBarStatus.SelectedItem);
You can adjust the progress bar to display three different states:
Normal Error status Paused
Flashing Effect
When you use Live Messenger (LM) to chat, if the message LM icon is sent by the other party, it will flash. Although Windows APIs do not directly control the flash effect, however, this effect may be frequently used during development. The following code is a flashing effect class:
internal sealed class FlashWindowHelper{ DispatcherTimer _timer; int _count = 0; int _maxTimes = 0; Window _window; public void Flash(int times, double millliseconds, Window window) { _timer = new DispatcherTimer(); _maxTimes = times; _timer.Interval = TimeSpan.FromMilliseconds(millliseconds); _timer.Tick += OnTick; _window = window; _timer.Start(); } void OnTick(object sender, EventArgs e) { if (++_count < _maxTimes) { Win32.FlashWindow(new WindowInteropHelper(_window).Handle, (_count % 2) == 0); } else { _timer.Stop(); } }}internal static class Win32{ [DllImport("user32.dll")] public static extern bool FlashWindow(IntPtr hwnd, bool bInvert);}
The flash?whelper class allows you to easily flash the taskbar icon:
private void flashTaskbar_Click(object sender, RoutedEventArgs e){ FlashWindowHelper helper = new FlashWindowHelper(); helper.Flash(8, 400, Application.Current.MainWindow);}
Flashing Effect
References
1. Windows 7 Taskbar Dynamic Overlay Icons and Progress Bars
Http://windowsteamblog.com/blogs/developers/archive/2009/07/28/windows-7-taskbar-dynamic-overlay-icons-and-progress-bars.aspx
2. SetProgressValue Method
The http://msdn.microsoft.com/en-us/library/dd391698 (VS.85). aspx
3. SetProgressState Method
The http://msdn.microsoft.com/en-us/library/dd391697 (VS.85). aspx