如何在 C# .NET 3.5 中更改进度条的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/778678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:47:43  来源:igfitidea点击:

How to change the color of progressbar in C# .NET 3.5?

c#.netwinforms.net-3.5

提问by Ivan Prodanov

I'd like to do two things on my progress bar.

我想在我的进度条上做两件事。

  1. Change the green colour to red.
  2. Remove the blocks and make it in one color.
  1. 将绿色更改为红色。
  2. 取下块并使其变成一种颜色。

Any information about those two things I wonder how to accomplish will be greatfuly appreaciated!

关于我想知道如何完成的这两件事的任何信息都将非常感谢!

Thanks.

谢谢。

采纳答案by Chris Persichetti

Since the previous answers don't appear to work in with Visual Styles. You'll probably need to create your own class or extend the progress bar:

由于以前的答案似乎不适用于视觉样式。您可能需要创建自己的类或扩展进度条:

public class NewProgressBar : ProgressBar
{
    public NewProgressBar()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle rec = e.ClipRectangle;

        rec.Width = (int)(rec.Width * ((double)Value / Maximum)) - 4;
        if(ProgressBarRenderer.IsSupported)
           ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle);
        rec.Height = rec.Height - 4;
        e.Graphics.FillRectangle(Brushes.Red, 2, 2, rec.Width, rec.Height);
    }
}

EDIT: Updated code to make the progress bar use the visual style for the background

编辑:更新代码以使进度条使用背景的视觉样式

回答by dustyburwell

In the designer, you just need to set the ForeColor property to whatever color you'd like. In the case of Red, there's a predefined color for it.

在设计器中,您只需将 ForeColor 属性设置为您喜欢的任何颜色。在红色的情况下,它有一个预定义的颜色。

To do it in code (C#) do this:

要在代码 (C#) 中执行此操作,请执行以下操作:

pgs.ForeColor = Color.Red;

Edit: Oh yeah, also set the Style to continuous. In code, like this:

编辑:哦,是的,还将样式设置为连续。在代码中,像这样:

pgs.Style = System.Windows.Forms.ProgressBarStyle.Continuous;

Another Edit:You'll also need to remove the line that reads Application.EnableVisualStyles()from your Program.cs(or similar). If you can't do this because you want the rest of the application to have visual styles, then I'd suggest painting the control yourself or moving on to WPF since this kind of thing is easy with WPF. You can find a tutorial on owner drawing a progress bar on codeplex

另一个编辑:您还需要删除Application.EnableVisualStyles()从您的Program.cs(或类似的)中读取的行。如果您因为希望应用程序的其余部分具有视觉样式而无法执行此操作,那么我建议您自己绘制控件或继续使用 WPF,因为使用 WPF 可以轻松完成这种操作。您可以找到有关所有者在 codeplex上绘制进度条的教程

回答by Eoin Campbell

EDIT

编辑

By the sounds of things you're using the XP Theme which has the green block based prog-bar. Try flipping your UI Style to Windows Classic and test again, but you may need to implement your own OnPaint event to get it to do what you want across all UI Styles

根据您使用的 XP 主题的声音,该主题具有基于绿色块的程序栏。尝试将您的 UI 样式翻转为 Windows Classic 并再次测试,但您可能需要实现自己的 OnPaint 事件以使其在所有 UI 样式中执行您想要的操作

Or as someone else pointed out, disable the VisualStyles for your application.

或者正如其他人指出的那样,为您的应用程序禁用 VisualStyles。

Original

原来的

As far as I know, the rendering of the Progress bar happens inline with the windows theme style that you've chosen (win2K, xp, vista)

据我所知,进度条的渲染与您选择的 Windows 主题样式(win2K、xp、vista)一致

You can change the color by setting the property

您可以通过设置属性来更改颜色

ProgressBar.ForeColor

I'm not sure that you can do much more however...

我不确定你还能做更多的事情......

does some googling

做一些谷歌搜索

Theres an article here from MS KB on creating a "Smooth" progress bar

MS KB 上有一篇关于创建“平滑”进度条的文章

http://support.microsoft.com/kb/323116

http://support.microsoft.com/kb/323116

回答by Fusspawn

Edit: in the two minuites it took me to fire up vs and check the syntax i was beaten to it with much better responses. i love this site.

编辑:在两分钟内,我启动了 vs 并检查了我被击败的语法,并得到了更好的响应。我喜欢这个网站。

        progressBar1 = new ProgressBar();
        progressBar1.ForeColor = Color.Red;

回答by Joey

Usually the progress bar is either themed or honors the user's color preferences. So for changing the color you either need to turn off visual styles and set ForeColoror draw the control yourself.

通常进度条要么有主题,要么尊重用户的颜色偏好。因此,要更改颜色,您需要关闭视觉样式并ForeColor自己设置或绘制控件。

As for the continuous style instead of blocks you can set the Styleproperty:

至于连续样式而不是块,您可以设置Style属性:

pBar.Style = ProgressBarStyle.Continuous;

回答by Aleksandar

try using messsage PBM_SETBARCOLOR, that should do the trick with SendMessage

尝试使用消息 PBM_SETBARCOLOR,这应该可以使用 SendMessage

See http://www.vbforums.com/showthread.php?t=248721for an example.

有关示例,请参见http://www.vbforums.com/showthread.php?t=248721

回答by Matt Blaine

Modificaton to dustyburwell's answer. (I don't have enough rep to edit it myself.) Like his answer, it works with "Visual Styles" enabled. You can just set the progressbar's ForeColor property in whatever form's design view.

修改dustyburwell的答案。(我没有足够的代表来自己编辑它。)就像他的回答一样,它可以在启用“视觉样式”的情况下使用。您可以在任何表单的设计视图中设置进度条的 ForeColor 属性。

using System;
using System.Windows.Forms;
using System.Drawing;

public class ProgressBarEx : ProgressBar
{
    private SolidBrush brush = null;

    public ProgressBarEx()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (brush == null || brush.Color != this.ForeColor)
            brush = new SolidBrush(this.ForeColor);

        Rectangle rec = new Rectangle(0, 0, this.Width, this.Height);
        if (ProgressBarRenderer.IsSupported)
            ProgressBarRenderer.DrawHorizontalBar(e.Graphics, rec);
        rec.Width = (int)(rec.Width * ((double)Value / Maximum)) - 4;
        rec.Height = rec.Height - 4;
        e.Graphics.FillRectangle(brush, 2, 2, rec.Width, rec.Height);
    }
}

回答by Marc

I just put this into a static class.

我只是把它放到一个静态类中。

  const int WM_USER = 0x400;
  const int PBM_SETSTATE = WM_USER + 16;
  const int PBM_GETSTATE = WM_USER + 17;

  [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

  public enum ProgressBarStateEnum : int
  {
   Normal = 1,
   Error = 2,
   Paused = 3,
  }

  public static ProgressBarStateEnum GetState(this ProgressBar pBar)
  {
   return (ProgressBarStateEnum)(int)SendMessage(pBar.Handle, PBM_GETSTATE, IntPtr.Zero, IntPtr.Zero);
  }

  public static void SetState(this ProgressBar pBar, ProgressBarStateEnum state)
  {
   SendMessage(pBar.Handle, PBM_SETSTATE, (IntPtr)state, IntPtr.Zero);
  } 

Hope it helps,

希望能帮助到你,

Marc

马克

回答by Eduardo

Just in case anyone looks for another option.... you can extend a Panel, use it as background (white or whatever), add another Panel inside it for the foreground (the moving bar). Then you have total control changing the color, etc.

以防万一有人寻找其他选项......您可以扩展面板,将其用作背景(白色或其他),在其中添加另一个面板作为前景(移动条)。然后你可以完全控制改变颜色等。

回答by Josh M.

Using Matt Blaine and Chris Persichetti's answers I've created a progress bar that looks a bit nicer while allowing infinite color choice (basically I changed one line in Matt's solution):

使用 Matt Blaine 和 Chris Persichetti 的答案,我创建了一个看起来更好的进度条,同时允许无限的颜色选择(基本上我在 Matt 的解决方案中更改了一行):

ProgressBarEx:

进度条:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace QuantumConcepts.Common.Forms.UI.Controls
{
    public class ProgressBarEx : ProgressBar
    {
        public ProgressBarEx()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            LinearGradientBrush brush = null;
            Rectangle rec = new Rectangle(0, 0, this.Width, this.Height);
            double scaleFactor = (((double)Value - (double)Minimum) / ((double)Maximum - (double)Minimum));

            if (ProgressBarRenderer.IsSupported)
                ProgressBarRenderer.DrawHorizontalBar(e.Graphics, rec);

            rec.Width = (int)((rec.Width * scaleFactor) - 4);
            rec.Height -= 4;
            brush = new LinearGradientBrush(rec, this.ForeColor, this.BackColor, LinearGradientMode.Vertical);
            e.Graphics.FillRectangle(brush, 2, 2, rec.Width, rec.Height);
        }
    }
}

Usage:

用法:

progressBar.ForeColor = Color.FromArgb(255, 0, 0);
progressBar.BackColor = Color.FromArgb(150, 0, 0);

Results

结果

You can use any gradient you like!

你可以使用任何你喜欢的渐变!

Download

下载

https://skydrive.live.com/?cid=0EDE5D21BDC5F270&id=EDE5D21BDC5F270%21160&sc=documents#

https://skydrive.live.com/?cid=0EDE5D21BDC5F270&id=EDE5D21BDC5F270%21160&sc=documents#