用户控制进度条

时间:2012-03-14 13:03:47

标签: c# winforms c#-4.0

我试图让用户控件充当进度条。据我所知,我需要在旧的上面绘制一个新的栏,并根据完成的百分比增加其大小。我有以下代码,但遗憾的是,即使我在初始化控件时将百分比属性设置为0%,绿色条仍为100%。我假设我做了一个愚蠢的疏忽但我无法看到它,任何帮助将不胜感激。感谢。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    namespace CustomPbar
    {
        public partial class Pbar : UserControl
        {
            private int PercentageValue;
            public int Percentage
            {
                get { return PercentageValue;}
                set 
                {   
                    PercentageValue = value;
                    this.Invalidate();
                }
            }


            public Pbar()
            {
                InitializeComponent();

                Percentage = 0;

                    using(GraphicsPath path = new GraphicsPath())
                    {
                    path.StartFigure();

                    // top left arc 
                    path.AddArc(0, 0, (10), (10), 180, 90);
                    //rect, 180, 90);

                    // top right arc 
                    path.AddArc(((this.Width) - (10)), 0, (10), (10), 270, 90);

                    // bottom right arc 
                    path.AddArc(((this.Width) - (10)), ((this.Height) - (10)), (10), (10), 0, 90);

                    // bottom left arc
                    path.AddArc(0, ((this.Height) - (10)), (10), (10), 90, 90);

                    path.CloseFigure();

                    this.Region = new Region(path);

                    this.BackColor = SystemColors.ControlLight;
                    this.BackgroundImage = new Bitmap(@"c:\users\FrazMan\Desktop\pb1.bmp");
                    this.BackgroundImageLayout = ImageLayout.Stretch;
                }
            }


            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);

                Rectangle rect = new Rectangle(0, 0, ((this.Width)*((Percentage)/100)), this.Height);

                using (GraphicsPath gp = new GraphicsPath())
                {
                    gp.StartFigure();

                    // top left arc 
                    gp.AddArc(0, 0, (10), (10), 180, 90);

                    // top right arc 
                    gp.AddArc(((rect.Width) - (10)), 0, (10), (10), 270, 90);

                    // bottom right arc 
                    gp.AddArc(((rect.Width) - (10)), ((rect.Height) - (10)), (10), (10), 0, 90);

                    // bottom left arc
                    gp.AddArc(0, ((rect.Height) - (10)), (10), (10), 90, 90);

                    gp.CloseFigure();

                    SolidBrush greenBrush = new SolidBrush(Color.Green);

                    e.Graphics.FillPath(greenBrush, gp);

                    greenBrush.Dispose();
                }

                using(Graphics Draw = this.CreateGraphics())
                {
                    Draw.DrawString(Percentage.ToString() + "%", ProgressBar.DefaultFont, Brushes.Black, new PointF((this.Width / 2) - ((Draw.MeasureString(Percentage.ToString() + "%", ProgressBar.DefaultFont)).Width / 2.0F), 
                        (this.Height / 2) - ((Draw.MeasureString(Percentage.ToString() + "%", ProgressBar.DefaultFont)).Height / 2.0F)));
                }

            }


            protected override void OnResize(EventArgs e)
            {
                base.OnResize(e);
                this.Refresh();
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

您创建Rectangle的几个地方,但从不使用它。我想你想使用rect宽度和高度而不是this宽度和高度。

您还应使用e.Graphics代替this.CreateGraphics()来绘制百分比字符串。

有大量重复的代码,我建议您将所有绘图代码保留在OnPaint中,当您想重绘时,请致电this.Refresh()。这对维护很有帮助。

答案 1 :(得分:0)

OnPaint方法中,您使用的是Graphics.FillPath,它将填充您正在创建的GraphicsPath内的整个空间。如果您只想绘制形状轮廓,请尝试使用Graphics.DrawPath

答案 2 :(得分:0)

这是我对c#

中自定义进度条的看法

我也有%凹槽,但你可以轻松摆脱它们。效果很好

<强> ProgressBarExtended.cs

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

namespace PressdMonitorSrvMod.Custom_Control
{
    public partial class ProgressBarExtended : UserControl
    {
        //constructor
        public ProgressBarExtended()
        {
            InitializeComponent();
        }

        #region Properties

        // Create a Value property for the Progress bar
        public float Value
        {
            get { return percent; }
            set
            {
                // Maintain the Value between 0 and 100
                if (value < 0) value = 0;
                else if (value > 100) value = 100;
                percent = value;

                percentage.Text = string.Format("{0}%", value);

                //change the notch color when overdrawn
                if (value.Equals(25f))
                    notch25.BackColor = Color.White;
                else if (value.Equals(50f))
                    notch50.BackColor = Color.White;
                else if (value.Equals(75f))
                    notch75.BackColor = Color.White;
                else if (value > 0 && value < 25)
                    notch25.BackColor = notch50.BackColor = notch75.BackColor = ProgressBarColor;
                else if (value > 25 && value < 50)
                    notch50.BackColor = notch75.BackColor = ProgressBarColor;
                else if (value > 50 && value < 75)
                    notch75.BackColor = ProgressBarColor;
                //move the percentage text to the center from start
                if (percentage.Location.X < ((Width - percentage.Width)/2))
                {
                    percentage.Location = new Point((int) ((percent/100)*Width) - percentage.Width, 0);
                    percentage.Margin = new Padding(0);
                    percentage.Dock = DockStyle.None;
                    percentage.BorderStyle = BorderStyle.None;
                    percentage.AutoSize = true;
                    percentage.TextAlign = ContentAlignment.BottomCenter;
                }
                else
                {
                    //already in center, keep it in center
                    ProgressBarExtendedSizeChanged();
                }
                //redraw after changes
                Refresh();
            }
        }

        public Color ProgressBarColor { get; set; }

        public Color HighlightColor { get; set; }

        public Font LabelFont
        {
            get { return percentage.Font; }
            set { percentage.Font = value; }
        }

        public Color LabelColor
        {
            get { return percentage.ForeColor; }
            set { percentage.ForeColor = value; }
        }

        //make it readonly, hide the property
        public new BorderStyle BorderStyle;

        #endregion Properties

        #region Events

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;
            // Create a brush that will draw the background of the Progress bar

            using (var brush = new SolidBrush(ProgressBarColor))
                {
                using (var pen = new Pen(brush))
                    {
                    // Create a linear gradient that will be drawn over the background.
                    using (var lgBrush = new LinearGradientBrush(new Rectangle(-1, -1, Width, Height), 
                        Color.FromArgb(150, highlightColor), 
                        Color.FromArgb(10, ProgressBarColor), 
                        LinearGradientMode.Vertical))
                        {
                        // Calculate how much has the Progress bar to be filled for "x" %
                        var width = (int)((percent / 100) * Width);
                        //draw the progress bar
                        e.Graphics.FillRectangle(brush, 0, 0, width, Height);
                        e.Graphics.FillRectangle(lgBrush, 0, 0, width, Height);
                        //draw the border
                        e.Graphics.DrawRectangle(pen, 0, 0, Width - 1, Height - 1);
                        }
                    }
                }
        }

        private void ProgressBarExtendedSizeChanged()
        {
            // Maintain the label in the center of the Progress bar
            percentage.Location = new Point(Width/2 - percentage.Width/2, 0);
        }

        #endregion Helper Methods
    }
}