我制作了一个带有圆角边缘的自定义按钮,我使用CreateRoundRectRgn
它,在绘制事件中调用它来圆所有边缘,当我运行我的程序时一切正常,直到大约一分钟后,然后我得到以下异常(值p = 0):
要添加的一件事是,由于颜色褪色(闪烁),按钮每秒进行大约 50 次绘制事件
这是我的绘画活动:
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();
}