1

我使用教程创建了一个简单酷炫的 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);
        }



    }
}

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

我需要一些帮助,谢谢!

4

1 回答 1

0

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

int _value;
public int Value {
   get { return _value;}
   set {
       if(_value != value) {
           _value = value;
           Invalidate();
       }
   }
}
于 2013-08-12T20:45:48.277 回答