我想改变groupBox的边框颜色,于是我照网上说的将groupBox重绘了,结果实现了改变边框颜色的效果,但是也有一个重大的bug,就是如果有弹框出现在groupBox的前面,将groupBox覆盖时,等弹框消失后,groupBox边框线就变形了,非常难看!!
请问怎么才能解决这个问题?附上代码private void groupBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Transparent, e.ClipRectangle);
Rectangle Rtg_LT = new Rectangle();
Rectangle Rtg_RT = new Rectangle();
Rectangle Rtg_LB = new Rectangle();
Rectangle Rtg_RB = new Rectangle();
Rtg_LT.X = 0; Rtg_LT.Y = 7; Rtg_LT.Width = 10; Rtg_LT.Height = 10;
Rtg_RT.X = e.ClipRectangle.Width - 11; Rtg_RT.Y = 7; Rtg_RT.Width = 10; Rtg_RT.Height = 10;
Rtg_LB.X = 0; Rtg_LB.Y = e.ClipRectangle.Height - 11; Rtg_LB.Width = 10; Rtg_LB.Height = 10;
Rtg_RB.X = e.ClipRectangle.Width - 11; Rtg_RB.Y = e.ClipRectangle.Height - 11; Rtg_RB.Width = 10; Rtg_RB.Height = 10;
Color color = BorderColor;
Pen Pen_AL = new Pen(color, 1);
Pen_AL.Color = color;
e.Graphics.DrawArc(Pen_AL, Rtg_LT, 180, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_RT, 270, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_LB, 90, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_RB, 0, 90);
e.Graphics.DrawLine(Pen_AL, 5, 7, 6, 7);
e.Graphics.DrawLine(Pen_AL, e.Graphics.MeasureString(groupBox1.Text, groupBox1.Font).Width + 3, 7, e.ClipRectangle.Width - 7, 7);
e.Graphics.DrawLine(Pen_AL, 0, 13, 0, e.ClipRectangle.Height - 7);
e.Graphics.DrawLine(Pen_AL, 6, e.ClipRectangle.Height - 1, e.ClipRectangle.Width - 7, e.ClipRectangle.Height - 1);
e.Graphics.DrawLine(Pen_AL, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 7, e.ClipRectangle.Width - 1, 13);
}
请问怎么才能解决这个问题?附上代码private void groupBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Transparent, e.ClipRectangle);
Rectangle Rtg_LT = new Rectangle();
Rectangle Rtg_RT = new Rectangle();
Rectangle Rtg_LB = new Rectangle();
Rectangle Rtg_RB = new Rectangle();
Rtg_LT.X = 0; Rtg_LT.Y = 7; Rtg_LT.Width = 10; Rtg_LT.Height = 10;
Rtg_RT.X = e.ClipRectangle.Width - 11; Rtg_RT.Y = 7; Rtg_RT.Width = 10; Rtg_RT.Height = 10;
Rtg_LB.X = 0; Rtg_LB.Y = e.ClipRectangle.Height - 11; Rtg_LB.Width = 10; Rtg_LB.Height = 10;
Rtg_RB.X = e.ClipRectangle.Width - 11; Rtg_RB.Y = e.ClipRectangle.Height - 11; Rtg_RB.Width = 10; Rtg_RB.Height = 10;
Color color = BorderColor;
Pen Pen_AL = new Pen(color, 1);
Pen_AL.Color = color;
e.Graphics.DrawArc(Pen_AL, Rtg_LT, 180, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_RT, 270, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_LB, 90, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_RB, 0, 90);
e.Graphics.DrawLine(Pen_AL, 5, 7, 6, 7);
e.Graphics.DrawLine(Pen_AL, e.Graphics.MeasureString(groupBox1.Text, groupBox1.Font).Width + 3, 7, e.ClipRectangle.Width - 7, 7);
e.Graphics.DrawLine(Pen_AL, 0, 13, 0, e.ClipRectangle.Height - 7);
e.Graphics.DrawLine(Pen_AL, 6, e.ClipRectangle.Height - 1, e.ClipRectangle.Width - 7, e.ClipRectangle.Height - 1);
e.Graphics.DrawLine(Pen_AL, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 7, e.ClipRectangle.Width - 1, 13);
}
{
e.Graphics.Clear(groupBox4.BackColor);
e.Graphics.DrawString(groupBox4.Text, groupBox4.Font, Brushes.Blue, 10, 1);
e.Graphics.DrawLine(Pens.Red, 1, 7, 8, 7);
e.Graphics.DrawLine(Pens.Red, e.Graphics.MeasureString(groupBox4.Text, groupBox4.Font).Width + 8, 7, groupBox4.Width - 2, 7);
e.Graphics.DrawLine(Pens.Red, 1, 7, 1, groupBox4.Height - 2);
e.Graphics.DrawLine(Pens.Red, 1, groupBox4.Height - 2, groupBox4.Width - 2, groupBox4.Height - 2);
e.Graphics.DrawLine(Pens.Red, groupBox4.Width - 2, 7, groupBox4.Width - 2, groupBox4.Height - 2);
}
建议你写成控件,颜色,圆角半径之类的,方便扩展
class MyGroupBox:GroupBox
{
protected override void OnPaint(PaintEventArgs e)
{
using (Pen pen = new Pen(Color.SteelBlue))
using (GraphicsPath path = new GraphicsPath())
{
Rectangle arc = new Rectangle(0, 7, 10, 10);
path.AddArc(arc, 180, 90);
arc.X = this.Width - arc.Width - 1;
path.AddArc(arc, 270, 90);
arc.Y = this.Height - arc.Height - 1;
path.AddArc(arc, 0, 90);
arc.X = 0;
path.AddArc(arc, 90, 90); path.CloseFigure(); e.Graphics.DrawPath(pen, path);
} TextRenderer.DrawText(e.Graphics, this.Text, this.Font, new Point(7, 1), this.ForeColor, this.BackColor);
}
}
参考:Control.Update 方法:使控件重绘其工作区内的无效区域。
Control.Refresh 方法:强制控件使其工作区无效并立即重绘自己和任何子控件;
等效于将 Invalidate 方法设置为 true 并将该方法与 Update 一起使用。那么既然有了Update,为何还要存在Invalidate呢?
Invalidate有重载的版本例如:Invalidate(Rectangle, Boolean) 使控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向控件发送绘制消息。还可以使分配给该控件的子控件无效