GroupBox 重绘圆角边框和文字

时间:2023-03-09 21:10:30
GroupBox 重绘圆角边框和文字
        private void GroupBox_Paint(object sender, PaintEventArgs e)
{
if (sender != null && sender is GroupBox)
{
GroupBox gbx = sender as GroupBox;
e.Graphics.Clear(gbx.BackColor);
Color color = Color.Black;
Pen p = new Pen(color, 1);
int w = gbx.Width;
int h = gbx.Height;
Brush b = null;
if (gbx.Parent != null)
b = new SolidBrush(gbx.Parent.BackColor);
else
b = new SolidBrush(this.BackColor);
//绘制直线
e.Graphics.DrawLine(p, 3, h - 1, w - 4, h - 1); //bottom
e.Graphics.DrawLine(p, 0, h - 4, 0, 12); //left
e.Graphics.DrawLine(p, w - 1, h - 4, w - 1, 12); //right
e.Graphics.FillRectangle(b, 0, 0, w, 8);
e.Graphics.DrawLine(p, 3, 8, 10, 8); //lefg top
e.Graphics.DrawLine(p, //right top
e.Graphics.MeasureString(gbx.Text,
gbx.Font).Width + 8, 8, w - 4, 8);
//绘制文字
e.Graphics.DrawString(gbx.Text, gbx.Font, Brushes.Blue, 10, 0); //title
//绘制弧线
e.Graphics.DrawArc(p, new Rectangle(0, 8, 10, 10), 180, 90); //left top
e.Graphics.DrawArc(p, new Rectangle(w - 11, 8, 10, 10), 270, 90); //right top
e.Graphics.DrawArc(p, new Rectangle(0, h - 11, 10, 10), 90, 90); //left bottom
e.Graphics.DrawArc(p, new Rectangle(w - 11, h - 11, 10, 10), 0, 90);//right bottom
}
}

因为 GroupBox 自带的灰色边框太过难看,所以尝试着对其重新绘制了一个新的边框。最开始不带圆角,感觉不太舒服,然后又添加了圆角,不过四个圆角的外侧还是与GroupBox外的颜色不太一样。试着用过一次路径来做,但是GroupBox内侧会有污点,不知道是怎么回事。

上面是没有污点,但是四个圆角外有细微颜色不同的,下面是对四个圆角外侧也做了着色,但是 GroupBox 内却出现污点的。如果有哪位高人能够帮忙解释一下污点的问题和提出一些解决方法,本人感激不尽

        private void GroupBox_Paint(object sender, PaintEventArgs e)
{
if (sender != null && sender is GroupBox)
{
GroupBox gbx = sender as GroupBox;
e.Graphics.Clear(gbx.BackColor);
Color color = Color.Black;
Pen p = new Pen(color, 1);
int w = gbx.Width;
int h = gbx.Height;
Brush b = null;
if (gbx.Parent != null)
b = new SolidBrush(gbx.Parent.BackColor);
else
b = new SolidBrush(this.BackColor);
e.Graphics.FillRectangle(Brushes.PapayaWhip, new Rectangle(0, 0, w, h));
GraphicsPath gp = new GraphicsPath();
gp.AddArc(new Rectangle(0, 8, 10, 10), 180, 90); //left top
gp.AddLine(0, h - 4, 0, 12); //left
gp.AddArc(new Rectangle(0, h - 11, 10, 10), 90, 90); //left bottom
gp.AddLine(3, h - 1, w - 4, h - 1); //bottom
gp.AddArc(new Rectangle(w - 11, h - 11, 10, 10), 0, 90); //right bottom
gp.AddLine(w - 1, h - 4, w - 1, 12); //right
gp.AddArc(new Rectangle(w - 11, 8, 10, 10), 270, 90); //right top
gp.AddLine(3, 8, w - 4, 8); //top
e.Graphics.FillRegion(Brushes.White, new Region(gp));
//绘制直线
e.Graphics.DrawLine(p, 3, h - 1, w - 4, h - 1); //bottom
e.Graphics.DrawLine(p, 0, h - 4, 0, 12); //left
e.Graphics.DrawLine(p, w - 1, h - 4, w - 1, 12); //right
e.Graphics.FillRectangle(b, 0, 0, w, 8);
e.Graphics.DrawLine(p, 3, 8, 10, 8); //lefg top
e.Graphics.DrawLine(p, //right top
e.Graphics.MeasureString(gbx.Text,
gbx.Font).Width + 8, 8, w - 4, 8);
//绘制文字
e.Graphics.DrawString(gbx.Text, gbx.Font, Brushes.Blue, 10, 0); //title
//绘制弧线
e.Graphics.DrawArc(p, new Rectangle(0, 8, 10, 10), 180, 90); //left top
e.Graphics.DrawArc(p, new Rectangle(w - 11, 8, 10, 10), 270, 90); //right top
e.Graphics.DrawArc(p, new Rectangle(0, h - 11, 10, 10), 90, 90); //left bottom
e.Graphics.DrawArc(p, new Rectangle(w - 11, h - 11, 10, 10), 0, 90);//right bottom
}
}