I made a custom button with rounded edges, i use CreateRoundRectRgn
for this, in the paint event this is called to round all the edges, when i run my program everything works fine until after about one minute, then i get the following exception(the value of p = 0):
one thing to add is that the button is undergoing about 50 paint events per second because of a color fade (blinking)
this is my paint event:
protected override void OnPaint(PaintEventArgs e)
{
this.SuspendLayout();
this.Region = GetRoundedRegion(this.Width, this.Height);
base.OnPaint(e);
if (!this.Enabled)
e.Graphics.Clear(this.DisabledColor);
else if (Blinking)
e.Graphics.Clear(Color.FromArgb(blinkingIntensity, this.BlinkColor));
else if (this.Pressed)
e.Graphics.Clear(this.PressedColor);
else if (this.Selected)
e.Graphics.Clear(this.SelectedColor);
else
e.Graphics.Clear(this.Color);
using (Pen blackPen = new Pen(Color.FromArgb(150, Color.Black), 2))
using (Pen whitePen = new Pen(Color.FromArgb(150, Color.Black), 2))
{
if (Pressed)
{
e.Graphics.DrawLines(blackPen, new[] { new Point(0, this.Height), new Point(0, 0), new Point(this.Width, 0) });
e.Graphics.DrawArc(blackPen, new Rectangle(0, 0, _Radius, _Radius), 180, 90);
e.Graphics.DrawLines(whitePen, new[] { new Point(0, this.Height - 2), new Point(this.Width - 2, this.Height - 2), new Point(this.Width - 2, 0) });
e.Graphics.DrawArc(whitePen, new Rectangle(this.Width - 2 - _Radius, this.Height - 2 - _Radius, _Radius, _Radius), 0, 90);
using (LinearGradientBrush lb = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), Color.FromArgb(150, Color.Black), Color.FromArgb(100, Color.White)))
{
e.Graphics.FillRectangle(lb, 0, 0, this.Width, this.Height);
}
}
else
{
e.Graphics.DrawLines(whitePen, new[] { new Point(0, this.Height), new Point(0, 0), new Point(this.Width, 0) });
e.Graphics.DrawArc(whitePen, new Rectangle(0, 0, _Radius, _Radius), 180, 90);
e.Graphics.DrawLines(blackPen, new[] { new Point(0, this.Height - 2), new Point(this.Width - 2, this.Height - 2), new Point(this.Width - 2, 0) });
e.Graphics.DrawArc(blackPen, new Rectangle(this.Width - 2 - _Radius, this.Height - 2 - _Radius, _Radius, _Radius), 0, 90);
using (LinearGradientBrush lb = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), Color.FromArgb(100, Color.White), Color.FromArgb(150, Color.Black)))
{
e.Graphics.FillRectangle(lb, 0, 0, this.Width, this.Height);
}
}
}
int pressedoffset = 0;
if (Pressed)
pressedoffset = 2;
int maxWidth = this.Width - 20;
// Set up string.
string measureString = Text;
Font stringFont = Font;
// Set maximum width of string.
int stringWidth = this.Width - 20;
SizeF stringSize = new SizeF();
// Set string format.
using (StringFormat newStringFormat = new StringFormat())
{
newStringFormat.FormatFlags = StringFormatFlags.DisplayFormatControl;
stringSize = e.Graphics.MeasureString(measureString, stringFont, stringWidth, newStringFormat);
}
// Draw string to screen.
if (CenterText)
e.Graphics.DrawString(measureString, stringFont, Brushes.White, new PointF(((this.Width / 2) - (stringSize.Width / 2)) + pressedoffset, ((this.Height / 2) - (stringSize.Height / 2)) + pressedoffset));
else
e.Graphics.DrawString(measureString, stringFont, Brushes.White, new PointF(10 + pressedoffset, ((this.Height / 2) - (stringSize.Height / 2)) + pressedoffset));
this.ResumeLayout();
}