Reputation: 8004
I am having a small issue with using Graphics.DrawArc
method. When used it's coming up short than what the actual size is. I am basing this control off another post found here
I am trying to make this into a UserControl
with some properties and expand on it. The issue is when I set the percentage per say 50% it comes up short...
This is what the UserControl
looks like at 50%... It's should be centered (blue) at the bottom of circle. I have tried adjusting everything I could, but I am at lost right now.
Here is my current code...
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) { }
}
}
Upvotes: 0
Views: 55
Reputation: 31193
You are calculating the angle with integers. When you do this:
angle = 360 / 100 * percentage;
it means
angle = 3 * percentage;
this of course leads to errors. There is a simple fix if you want to keep using ints:
angle = 360 * percentage / 100;
This way it doesn't get rounded down before multiplication. Or you can just use floats all the way:
angle = 360f / 100f * percentage;
Upvotes: 2