2012-03-14 88 views
1

我試圖讓一個usercontrol充當進度條。據我所知,我需要在舊版本的頂部繪製一個新的條形碼,並根據完成的百分比增加它的大小。我有下面的代碼,但不幸的是,綠色的酒吧是100%,即使我設置控制初始化時百分比屬性爲0%。我假設我做了一個愚蠢的監督,但我不能看到它,任何幫助將不勝感激。謝謝。用戶控制進度條

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

你'Percentage.set'應該無效()控制 – 2012-03-14 13:08:35

+0

@HenkHolterman嗨,我改變了我的原代碼(見上文),但其仍無法正常工作,我只是得到一個堅實的綠色酒吧,沒有百分比等。 – 2012-03-14 13:14:01

回答

1

幾個地方你創建一個Rectangle,但從來沒有使用它。我想你想使用寬度和高度的rect而不是this的寬度和高度。

您還應該使用e.Graphics而不是this.CreateGraphics()來繪製百分比字符串。

有大量的重複代碼,我建議您保留OnPaint中的所有繪圖代碼,當您想重畫時,請致電this.Refresh()。這對維護有很大的幫助。

+0

@ Kendall:我假設我仍然需要包含在構造函數中的代碼,雖然正確嗎? – 2012-03-14 13:30:23

+0

你不應該在構造器中需要繪製代碼。我認爲控件會自動加載,但如果不是,請在OnLoad中調用'this.Refresh()'或其他東西。 – 2012-03-14 13:33:00

+0

我會試一試,看看它是如何發展的。一旦我點擊不同的Winform,任何想法,百分比仍然沒有顯示?謝謝你的幫助。 – 2012-03-14 13:36:01

0

在您的OnPaint方法中,您使用的是Graphics.FillPath,它會填充您正在創建的GraphicsPath內的整個空間。如果您只想繪製形狀的輪廓,請嘗試使用Graphics.DrawPath

+0

即時通訊試圖獲得一個堅實的顏色欄。謝謝你的提示。 – 2012-03-14 13:41:56

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 
    } 
} 
+0

我希望我永遠不要寫這樣的代碼。價值二傳手... *不寒而慄* – 2012-03-14 13:35:53

+0

沒問題@KendallFrey :) – Mayank 2012-03-14 13:38:27

+0

@Mayank:這看起來比什麼是最好的拼湊在一起。非常感謝分享。 – 2012-03-14 13:39:33