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);
        }



    }
}

問題は、値を増やしても値が実際には適用されないことです (バー自体は増加しません)。私の友人は、値が変更されたときに値を無効にする必要があると言いましたが、その方法がわかりません!

助けてください、ありがとう!

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 に答える