更改值时禁用 .NET 进度条动画? [英] Disabling .NET progressbar animation when changing value?

查看:25
本文介绍了更改值时禁用 .NET 进度条动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到关于动画和进度条的 SO 还有其他问题,但它们似乎围绕摆脱绘制在进度条顶部的动画,即.穿过它的亮点.

I realize there are other questions on SO regarding animations and progressbars, but they seem to revolve around getting rid of the animation drawn on top of the progress bar, ie. the highlight that travels over it.

我想要做的是去掉我设置进度条新值时使用的动画.我现在遇到的问题是正在运行的操作完成,然后进度条在操作完成后继续增加到最大位置.

What I want to do is to get rid of the animation that is used when I set the new value of the progress bar. The problem I have now is that the action that is running completes and then the progress bar continues to increase up to their max position after the action has completed.

换句话说,如果我将进度条的 Value 属性设置为 50,我希望它立即移动到中间位置(如果最大值为 100),而不是像现在那样慢慢地将进度条建立到那个位置.

In other words, if I set the Value property of the progressbar to 50, I want it to travel to the halfway position (if max is 100) immediately, not slowly build up the progressbar to that position as it does now.

如果确实有关于 SO 的问题已经解决了这个问题,只需将其关闭为重复项,我很乐意将其删除,但我找不到任何问题.

If there is indeed a question on SO that already deals with this, just close as duplicate and I'll happily delete it, but I could not find any.

这是我找到的:禁用 WinForms ProgressBar 动画,它处理突出显示动画,这不是我要说的.

This is the one I found: Disabling WinForms ProgressBar animation, and it deals with the highlight that is animated, and that's not what I'm talking about.

这是一个简单的 LINQPad 演示,显示了问题:

Here's a simple LINQPad demo that shows the problem:

void Main()
{
    using (var fm = new Form())
    {
        var bt = new Button
        {
            Text = "Start",
            Location = new Point(8, 8),
            Parent = fm,
        };
        var pb = new ProgressBar
        {
            Top = bt.Top + bt.Height + 8,
            Width = fm.ClientRectangle.Width - 16,
            Left = 8,
            Parent = fm
        };

        bt.Click += (s, e) =>
        {
            bt.Enabled = false;
            Thread t = new Thread(new ThreadStart(() =>
            {
                Thread.Sleep(1000);
                bt.BeginInvoke(new Action(() => { pb.Value = 50; }));
                Thread.Sleep(1000);
                bt.BeginInvoke(new Action(() => { pb.Value = 100; }));
                bt.BeginInvoke(new Action(() => { bt.Enabled = true; }));
            }));
            t.Start();
        };
        fm.ShowDialog();
    }
}

编辑 1:这是 Windows 7 玻璃主题,所以是的,我敢打赌这是特定于 7 或可能也是 Vista.

Edit 1: This is Windows 7, Glass theme, so yes, I bet this is specific to 7 or possibly also Vista.

这是显示问题的 GIF 动画,即上面的项目.您可以看到,一旦按钮变为启用状态,在设置中途标记后 1 秒,进度条将动画显示为 100%, 按钮已启用.

Here's a GIF-animation that shows the problem, the project from above. You can see that as soon as the button becomes enabled, 1 second after the halfway mark has been set, the progressbar animates up to 100%, after the button has become enabled.

如上所示,将按钮设置回启用状态并将进度条设置为 100 是同时"完成的.基本上,我不想要进度条的渐进式构建,我希望它直接跳到 50%,然后在按钮启用的同时跳到 100%.

As you can see above, setting the button back to enabled and setting the progressbar to 100 is done "at the same time". Basically, I don't want the progressive buildup of the progressbar, I want it to jump directly to 50% and then to 100% at the same time as the button becomes enabled.

编辑 2: 为了回应 David Heffernan 的回答,我是这样更改上述代码的:

Edit 2: In response to David Heffernan's answer, this is how I changed the above code:

bt.BeginInvoke(new Action(() => { pb.Value = 51; pb.Value = 50; }));
Thread.Sleep(1000);
bt.BeginInvoke(new Action(() => { pb.Maximum = 101; pb.Value = 101;
                                  pb.Maximum = 100; pb.Value = 100; }));

推荐答案

这个动画功能是在 Vista 中引入的,带有 Aero 主题.

This animation feature was introduced in Vista with the Aero theme.

不过有一个解决方法.如果向后移动进度,则不会显示动画.因此,如果您希望它立即增加 50,请将 Value 增加 51,然后立即减少 1.

There is a workaround though. If you move the progress backwards, the animation is not shown. So if you want it to advance by 50 instantly, increment Value by 51, then immediately decrement by 1.

当接近 100% 时,您会陷入冲突,因为您无法将值设置为 101(我假设最大值设置为 100).而是将最大值设置为 1000,例如,增加到 1000,减少到 999,然后返回到 1000.

You get into strife when close to 100% because you can't set Value to 101 (I'm assuming Maximum is set to 100). Instead set Maximum to 1000, say, increase to 1000, decrease to 999, and then move back to 1000.

无论如何,这有点奇怪,但它确实有给你想要的效果的好处!

Anyway, it's kind of weird, but it does have the benefit of giving you the desired effect!

这篇关于更改值时禁用 .NET 进度条动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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