2016-11-26 6 views
0

私はGraphics.DrawArcメソッドを使用して小さな問題があります。使用すると、実際のサイズよりも短くなります。私はこのコントロールを他の投稿から外していますhereDrawArcが短くなっています

私はこれをいくつかのプロパティを持つUserControlにして展開しようとしています。問題は、1つの割合を50%と設定すると短くなります。

UserControlは50%のように表示されます...円の下に中央に配置する必要があります。私はできること全てを調整しようとしましたが、今は失われています。

enter image description here

ここに私の現在のコードは、あなたが整数で角度を計算している...

Color _ProgressCompletedColor = SystemColors.MenuHighlight; 
    Color _ProgressNotCompleted = Color.LightGray; 
    Int32 _ProgressThickness = 2; 
    Single _ProgressCompleted = 25; 

    public AttuneProgressBar() 
    { 
     InitializeComponent(); 
    } 

    public Single PercentageCompleted 
    { 
     get 
     { 
      return this._ProgressCompleted; 
     } 
     set 
     { 
      this._ProgressCompleted = value; 
      this.Invalidate(); 
     } 
    } 

    public Int32 ProgressBarThickness 
    { 
     get 
     { 
      return this._ProgressThickness; 
     } 
     set 
     { 
      this._ProgressThickness = value; 
      this.Invalidate(); 
     } 
    } 

    public Color ProgressNotCompletedColor 
    { 
     get 
     { 
      return this._ProgressNotCompleted; 
     } 
     set 
     { 
      this._ProgressNotCompleted = value; 
      this.Invalidate(); 
     } 
    } 

    public Color ProgressCompletedColor 
    { 
     get 
     { 
      return this._ProgressCompletedColor; 
     } 
     set 
     { 
      this._ProgressCompletedColor = value; 
      this.Invalidate(); 
     } 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     // Call the OnPaint method of the base class. 
     base.OnPaint(e); 

     DrawProgress(e.Graphics, new Rectangle(new Point(1,1), new Size(this.ClientSize.Width - 3, this.ClientSize.Height - 3)), PercentageCompleted); 
    } 

    private void DrawProgress(Graphics g, Rectangle rec, Single percentage) 
    { 
     Single progressAngle = (360/100 * percentage); 
     Single remainderAngle = 360 - progressAngle; 

     try 
     { 
      using (Pen progressPen = new Pen(ProgressCompletedColor, ProgressBarThickness), remainderPen = new Pen(ProgressNotCompletedColor, ProgressBarThickness)) 
      { 
       g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
       g.DrawArc(progressPen, rec, -90, progressAngle); 
       g.DrawArc(remainderPen, rec, progressAngle - 90, remainderAngle); 
      } 
     } 
     catch (Exception exc) { } 
    } 

} 

答えて

2

です。これを行うと:

angle = 360/100 * percentage; 

それはエラーにつながるこのコースの

angle = 3 * percentage; 

を意味します。

angle = 360 * percentage/100; 

それは乗算の前に切り捨てられません。この方法:あなたはint型を使用して保存しておきたい場合は、簡単な修正があります。または、浮動小数点数をすべて使用することができます。

angle = 360f/100f * percentage; 
+0

定数の浮動​​小数点数を使用する方がよいでしょう。 – MarkusEgle

+0

@ MarkusEgleこの場合は多分ですが、intのみを使用し、結果がintを変更するのが最良の方法です。もちろん、オーバーフローを排除することはできません。 –

+0

@SamKuhmonenあなたは100%正しいです、ダングタイプは私をもう一度持っています!これはすばらしいことです。今日は約1時間私を悩ませていましたが、私が間違っていたことを理解できませんでした。再度、感謝します! – Codexer

関連する問題