如何使用Winform C#4.5在任务栏中显示进度

编辑:我不希望它更新,更改或消失。 我希望任务栏在40%时启动程序,保持这种状态直到它关闭。

我花了很多时间尝试了许多例子……但没有运气。

为了简单起见,你如何在Error颜色中显示40%?

此代码运行但在屏幕上不执行任何操作,没有错误,只是通过以下方式运行:

public TaskbarItemInfo taskBar = new TaskbarItemInfo(); 

然后在一个方法中:

 taskBar.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Error; taskBar.ProgressValue = 0.40; 

如果您在下一行断点并查看,它已设置值,它们只是在屏幕上没有做任何事情……

这是一个简短的例子,您应该可以根据自己的需要进行定制:

  System.Windows.Window w = new System.Windows.Window(); w.TaskbarItemInfo = new System.Windows.Shell.TaskbarItemInfo() { ProgressState = System.Windows.Shell.TaskbarItemProgressState.Normal }; w.Loaded += delegate { Action callUpdateProgress = (o) => { w.TaskbarItemInfo.ProgressValue = (double) o; }; Thread t = new Thread(() => { for (int i = 1; i <= 10; i++) { w.Dispatcher.BeginInvoke(callUpdateProgress, 1.0 * i / 10); Thread.Sleep(1000); } }); t.Start(); }; System.Windows.Application app = new System.Windows.Application(); app.Run(w); 

TaskbarItemInfo本身不做任何事情。 它需要一个在任务栏上显示的窗口。 请注意,通常从WPF Window的实例获取TaskbarItemInfo的实例。 即该类旨在用于WPF程序,而不是Winforms。

对于Winforms程序,您可能会发现使用Windows API Codepack更实用,如果我没记错的话 ,它会支持这个Shellfunction。

您可以使用WindowsAPICodePack.TaskbarTaskbarManager类来设置窗体窗口的任务栏进度,如下所示:

 using Microsoft.WindowsAPICodePack.Taskbar; ... private void Form1_Load(object sender, EventArgs e) { TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Error, Handle); TaskbarManager.Instance.SetProgressValue(40, 100, Handle); } 

使用当前Form的.Handle告诉管理器应该向哪个窗口提供此function。 如果您希望在同一位置处理其进度,也可以使用来自另一个表单的公共静态指针引用。

不幸的是,由于某些原因,微软不再为此托管下载,尽管该库仍然存在相关性。 但这里有一个StackOverflow Q&A,其中有许多其他链接用于同一个库: Windows API Code Pack:它在哪里? 。 请注意,有两个版本,1.0和1.1。 一般来说,你可能更喜欢1.1版本; 它有许多错误修复,增加的function,以及更好的Fxcop合规性。 我提供的链接是1.1,但在SO文章上也有下载1.0的链接。

我通过从主框架线程创建一个单独的线程来执行此操作来执行发送进度更新的代码,您可以在进度条上调用setProgress,但是您必须创建一个委托方法,否则您将获得一个运行时exception,你的线程正在访问主线程上的控件,这是我要做的,

如果你有进度条,在你的类中声明一个委托方法,

 public delegate void SetProgressDelg(int level); 

然后实现此方法来更新进度条,

 public void SetProgress(int level) { if (this.InvokeRequired) { SetProgressDelg dlg = new SetProgressDelg(this.SetProgress); this.Invoke(dlg, level); return; } progressBar.Value = level; } 

希望这适用于,我在几个应用程序中使用它,它工作得很好。

以下是构建进度条的方法,

  ToolStripContainer = toolStripContainer1 = new System.Windows.Forms.ToolStripContainer(); // StatusBar // ToolStripStatusLabel StatusBar = new System.Windows.Forms.ToolStripStatusLabel(); StatusBar.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; StatusBar.ForeColor = System.Drawing.Color.Blue; StatusBar.LinkColor = System.Drawing.Color.Navy; StatusBar.Name = "StatusBar"; StatusBar.Size = new System.Drawing.Size(732, 20); StatusBar.Spring = true; StatusBar.Text = "Status Messages Go Here"; StatusBar.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // ProgressBar // ToolStripProgressBar ProgressBar = new System.Windows.Forms.ToolStripProgressBar(); ProgressBar.ForeColor = System.Drawing.Color.Yellow; ProgressBar.Name = "ProgressBar"; ProgressBar.Size = new System.Drawing.Size(150, 19); // // StatusStrip // StatusStrip StatusStrip = new System.Windows.Forms.StatusStrip(); StatusStrip.Dock = System.Windows.Forms.DockStyle.None; StatusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { StatusBar, this.ProgressBar}); StatusStrip.Location = new System.Drawing.Point(0, 0); StatusStrip.Name = "StatusStrip"; StatusStrip.Size = new System.Drawing.Size(899, 25); StatusStrip.TabIndex = 0; toolStripContainer1.BottomToolStripPanel.Controls.Add(this.StatusStrip); 

然后你想将toolStripContainer添加到主面板的控件。

你想从正在处理你的任务的线程调用SetProgress,这是你如何启动一个线程,

  //* from your class of your main frame //* this is where SetStatus is defined //* start a thread to process whatever task //* is being done Thread t = new Thread(StartProc); t.Start(); public void StartProc() { //* start processing something, //*let's assume your are processing a bunch of files List fileNames; for (int i = 0; i < fileNames.Count; i++) { //* process file here //* ........... //* then update progress bar SetProgress((int)((i + 1) * 100 / fileNames.Length)); } //* thread will exit here } 

如果您需要其他东西,请告诉我,希望这有帮助,