自定义ProgressBar控件问题

时间:2013-08-12 20:44:35

标签: c# winforms

我使用教程创建了一个简单酷的ProgressBar控件。但是,我正面临着一个问题。这是我的代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Revarz
{
    class GraphicsHelper
    {
        public GraphicsPath Createround(int X, int Y, int Width, int Height, int CornerRadius)
        {
            GraphicsPath gfxPath = new GraphicsPath();
            try
            {
                gfxPath.AddArc(X, Y, CornerRadius, CornerRadius, 180, 90);
                gfxPath.AddArc(X + Width - CornerRadius, Y, CornerRadius, CornerRadius, 270, 90);
                gfxPath.AddArc(X + Width - CornerRadius, Y + Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
                gfxPath.AddArc(X, Y + Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
                gfxPath.CloseAllFigures(); return gfxPath;
            }
            catch (Exception) { return null; }
        }
    }

    public class CustomProgressbar : Control
    {

        public int Value { get; set; }
        private int _Maximum = 100;
        public int Maximum
        {
            get { return _Maximum; }
            set { if (value > 0) { _Maximum = value; } else { throw new Exception("Maximum should be bigger than zero!"); }; }
        }

        public CustomProgressbar()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.ResizeRedraw, true);
            DoubleBuffered = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.Clear(BackColor);
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;


            GraphicsPath barPath = new GraphicsHelper().Createround(0, 0, Width - 1, Height - 1, 3);
            e.Graphics.DrawPath(new Pen(Color.FromArgb(50, Color.Black)), barPath);
            e.Graphics.SetClip(barPath);
            LinearGradientBrush LGB = new LinearGradientBrush(new Rectangle(0, 2, Width - 1, Height - 3), Color.FromArgb(241, 229, 201), Color.FromArgb(237, 218, 202), 90F);
            e.Graphics.FillRectangle(LGB, LGB.Rectangle);
            e.Graphics.ResetClip();

            int DrawWidth = (int)(((double)Value / (double)_Maximum) * (double)(Width - 1));
            if (DrawWidth > 1)
            {
                GraphicsPath FilledPart = new GraphicsHelper().Createround(0, 0, DrawWidth, Height - 1, 3);
                LinearGradientBrush LGB2 = new LinearGradientBrush(new Rectangle(0, 1, DrawWidth, Height - 2), Color.FromArgb(232, 119, 9), Color.FromArgb(255, 171, 3), 90F);
                e.Graphics.FillRectangle(LGB2, LGB2.Rectangle);
                e.Graphics.DrawPath(new Pen(Color.FromArgb(146, 101, 11)), FilledPart);
            }

            base.OnPaint(e);
        }



    }
}

问题在于,当我增加该值时,该值不会实际应用(条本身,不会增加)。我的冷静告诉我,当它改变时我必须使值无效,但我不知道该怎么做!

我想要一些帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

您需要调用Invalidate()方法强制重绘:

int _value;
public int Value {
   get { return _value;}
   set {
       if(_value != value) {
           _value = value;
           Invalidate();
       }
   }
}