更改ProgressBar的颜色 [英] Changing the color of ProgressBar

查看:99
本文介绍了更改ProgressBar的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我做了太多尝试来更改ProgressBar的颜色,但是它根本不起作用.有人有什么主意吗?


Hi,
I tried too much to change the color of my ProgressBar, but it does not works at all. Does anyone have any idea?


推荐答案

它不是单一的颜色可以看到.进度条是如此简单,以至于我建议您最好创建自己的控件,而不是由所有者绘制它,尽管我猜想从进度条派生的控件将更容易放置到现有控件中.
It''s not a single color, as you can see. A progress bar is so simple that I''d suggest you''re better off creating your own control, rather than owner drawing it, although I guess a control derived from a progress bar would be easier to drop into place of existing ones.


链接 [
This link[^] creates a progress bar in visual basic. You''ve asked this question in C#, but this might get you started.


非常感谢,
这是一个g回链接.
虽然我知道vb公平,但是我可以在C#中更改程序.

另外,由于某些问题,我更改了这两行.我不知道它是否正确,但是在控件运行时它解决了一个问题.


Thanks very much,
It was a grate link.
Although I know vb fair, but I could change the program in C#.

Also I changed these two lines because of some problems. I don''t know if it was a right work or not, but it solved a problem when the control is running.


//this.Invalidate(updateRect); removed by editor<br />
this.Invalidate();             //added by editor<br />




C#中的代码:




codes in C#:

private int min = 0;//Minimum value for progress range
private int max = 100;// Maximum value for progress range
private int val = 0;// Current progress
private Color barColor = Color.Blue;// Color of progress thister

protected override void OnResize(EventArgs e)
{
    // Invalidate the control to get a repaint.
    this.Invalidate();
}

protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;
    SolidBrush brush = new SolidBrush(barColor);
    float percent = ((float)val - min) / (max - min);
    Rectangle rect = this.ClientRectangle;

    // Calculate area for drawing the progress.
    rect.Width = (int)(rect.Width * percent);

    // Draw the progress thister.
    g.FillRectangle(brush, rect);

    // Draw a three-dithisnsional border around the control.
    Draw3DBorder(g);

    // Clean up.
    brush.Dispose();
    g.Dispose();
}

public int Minimum
{
    get
    {
        return min;
    }
    set
    {
        // Prevent a negative value.
        if (value < 0)
            min = 0;

        // Make sure that the minimum value is never set higher than the maximum value.
        if (value > max)
        {
            min = value;
        }

        // Make sure that the value is still in range.
        if (val < min)
            val = min;

        // Invalidate the control to get a repaint.
        this.Invalidate();
    }
}

public int Maximum
{
    get
    {
        return max;
    }

    set
    {
        // Make sure that the maximum value is never set lower than the minimum value.
        if (value < min)
            min = value;

        max = value;

        // Make sure that the value is still in range.
        if (val > max)
            val = max;

        // Invalidate the control to get a repaint.
        this.Invalidate();
    }
}

public int Value
{
    get
    {
        return val;
    }

    set
    {
        int oldvalue = val;

        // Make sure that the value does not stray outside the valid range.
        if (value < min)
            val = min;
        else if (value > max)
            val = max;
        else
            val = value;

        // Invalidate only the changed area.
        float percent;

        Rectangle newvalueRect = this.ClientRectangle;
        Rectangle oldvalueRect = this.ClientRectangle;

        // Use a new value to calculate the rectangle for progress.
        percent = (val - min) / (max - min);
        newvalueRect.Width = (int)(newvalueRect.Width * percent);

        // Use an old value to calculate the rectangle for progress.
        percent = (oldvalue - min) / (max - min);
        oldvalueRect.Width = (int)(oldvalueRect.Width * percent);

        Rectangle updateRect = new Rectangle();

        // Find only the part of the screen that must be updated.
        if (newvalueRect.Width > oldvalueRect.Width)
        {
            updateRect.X = oldvalueRect.Size.Width;
            updateRect.Width = newvalueRect.Width - oldvalueRect.Width;
        }
        else
        {
            updateRect.X = newvalueRect.Size.Width;
            updateRect.Width = oldvalueRect.Width - newvalueRect.Width;
        }

        updateRect.Height = this.Height;
        // Invalidate only the intersection region.

        //this.Invalidate(updateRect); removed by editor
        this.Invalidate();             //added by editor
    }
}

public Color ProgressBarColor
{
    get
    {
        return barColor;
    }
    set
    {
        barColor = value;

        // Invalidate the control to get a repaint.
        this.Invalidate();
    }
}

private void Draw3DBorder(Graphics g)
{
    int PenWidth = (int)Pens.White.Width;

    g.DrawLine(Pens.DarkGray,
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
        new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top));
    g.DrawLine(Pens.DarkGray,
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth));
    g.DrawLine(Pens.White,
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth),
        new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
    g.DrawLine(Pens.White,
        new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top),
        new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
}


这篇关于更改ProgressBar的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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