- 欢迎访问起航教学!
Windows 7 任务栏开发:进度条(Progress Bar)
12-14 18:36:14 分类:Win7技巧 浏览次数: 628次 本篇我们将对进度条特性进行研究。在使用IE 下载文件时,任务栏图标会同步显示当前下载进度(如下图)。那么在应用程序中如何实现这个效果呢?
下载状态
TaskbarManager.SetProgressValue 方法
在TaskbarManager 类中有三种不同方式使用SetProgressValue 方法来设置进度条当前位置,其中currentValue 为进度条当前位置的参数,maximumValue 为最大参数:
//设置当前窗口
public void SetProgressValue(int currentValue, int maximumValue)
{
CoreHelpers.ThrowIfNotWin7();
TaskbarList.SetProgressValue(OwnerHandle, Convert.ToUInt32(currentValue),
Convert.ToUInt32(maximumValue));
}
//设置指定窗口
public void SetProgressValue(int currentValue, int maximumValue,
IntPtr windowHandle)
{
CoreHelpers.ThrowIfNotWin7();
TaskbarList.SetProgressValue(windowHandle, Convert.ToUInt32(currentValue),
Convert.ToUInt32(maximumValue));
}
//设置指定WPF窗口
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 进度条状态
除了常见的绿色(Normal 正常状态)外,还可以通过调用枚举TaskbarProgressBarState 使用其他几种状态,请参看下表:
名称 描述 NoProgress 不显示进度条 Indeterminate 不定值进度条(处于滚动状态) Normal 正常状态(绿色) Error 错误状态(红色) Paused 暂停状态(黄色)
TaskbarManager.SetProgressState 方法
如何为进度条设置以上状态呢?当然TaskbarManager 也同样提供了三种不同的方式使用SetProgressState 方法,对进度条状态进行相应的设置:
//设置当前窗口状态
public void SetProgressState(TaskbarProgressBarState state)
{
CoreHelpers.ThrowIfNotWin7();
TaskbarList.SetProgressState(OwnerHandle, (TBPFLAG)state);
}
//设置指定窗口状态
public void SetProgressState(TaskbarProgressBarState state, IntPtr windowHandle)
{
CoreHelpers.ThrowIfNotWin7();
TaskbarList.SetProgressState(windowHandle, (TBPFLAG)state);
}
//设置指定WPF窗口状态
public void SetProgressState(TaskbarProgressBarState state,
System.Windows.Window window)
{
CoreHelpers.ThrowIfNotWin7();
TaskbarList.SetProgressState(
(new WindowInteropHelper(window)).Handle,
(TBPFLAG)state);
}
效果演示
下面只需要在程序中使用以上方法便可实现对进度条的控制,下面代码通过Slider 调节进度条当前数值:
TaskbarManager.Instance.SetProgressValue((int)progressSlider.Value, 100);
TaskbarManager.Instance.SetProgressState(
(TaskbarProgressBarState)progressBarStatus.SelectedItem);
通过调整进度条呈现出三种不同的状态效果:
正常状态
错误状态
暂停状态
闪动效果
使用Live Messenger(LM)聊天时,如果对方发出了信息LM 图标会闪动提示,虽然Windows API 没有直接控制闪动效果的方法,但该效果在开发中可能会经常使用,下面代码为一个闪动效果类:
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);
}
通过FlashWindowHelper 类可以轻松的使任务栏图标闪动起来:
private void flashTaskbar_Click(object sender, RoutedEventArgs e)
{
FlashWindowHelper helper = new FlashWindowHelper();
helper.Flash(8, 400, Application.Current.MainWindow);
}
闪动效果
本文来自 http://www.qihang56.com 谢谢支持
相关热词搜索:
- ·上一篇:Windows 7任务栏调节技巧
- ·下一篇:Win7解决本地连接问题
评论排行
Win7技巧 热门排行
- · Win7下如何正常安装使用AutoCAD2017
- · Windows7下安装VS2017提示“You have
- · Windows7关机速度提升
- · Ubuntu9.10\Win7双系统 重装后修复GRU
- · Windows7系统更改无线网络优先级的方法
- · 轻松玩转Win 7中“轻松访问中心”
- · Windows 7新增组合键
- · Win7系统如何隐藏或取消隐藏文件夹
- · 关闭Windows7显示器校准
- · Win 7系统怎样显示文件类型扩展名
- · Windows 7开机启动项介绍
- · Win7 eSATA接口优化
- · Windows 7轻松传送文件
- · 解决Win7忘记登入密码问题
- · Msconfig管理Win7引导配置
- · 教你硬盘安装win7系统详细图文教程
- · win7玩游戏无法全屏的一些解决方法
- · Win7下5种方式打开资源管理器
- · 教程:Windows7下硬盘安装BT4方法
- · Win7/Vista/XP下两个切换IP地址的批处
- · Windows 7主题包中的壁纸从哪儿来
- · Windows 7密码重设盘的内部原理浅析
- · Windows 7升级的八大步骤
- · 清理和关闭多余的Win7系统服务