如何使用 Winform C# 4.5 在任务栏中显示进度 [英] How do you show progress in the Taskbar with Winform C# 4.5

查看:34
本文介绍了如何使用 Winform C# 4.5 在任务栏中显示进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不希望它更新、更改或消失.我希望任务栏处于 40% 以启动程序,保持这种状态直到它关闭.

I don't want it to update, change or go away. I ONLY want the taskbar to be at 40% to start the program, stay that way till it closes.

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

I spent a lot of hours and tried many examples...but no luck.

为简单起见,您如何以错误颜色显示 40% 已完成?

To keep it simple, how do you show 40% done in Error color?

此代码运行但在屏幕上什么也不做,没有错误,直接运行:

This code runs but does nothing on the screen, no errors, just runs right by:

public TaskbarItemInfo taskBar = new TaskbarItemInfo();

然后在一个方法中:

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

如果你在下一行断点并查看,它已经设置了值,它们只是在屏幕上做任何事情......

If you breakpoint on the next line and look, it has set the values, they just don't do anything on the screen...

推荐答案

以下是一个简短示例,您应该可以使用它来满足您的需求:

Here's a short example that you should be able to use to tailor to your needs:

    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<Object> 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);

要完成上述工作,您需要在顶部 using System.Threading; 并添加以下引用:PresentationCorePresentationFrameworkSystemXamlWindowsBase.

To make the above work you need to have using System.Threading; at top and also add references of: PresentationCore, PresentationFramework, SystemXaml, WindowsBase.

这篇关于如何使用 Winform C# 4.5 在任务栏中显示进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆