怎么改变groupbox 控件的边框线条的宽度 20philistine

2025-02-14 04:09:36
推荐回答(2个)
回答1:

自绘OnPaint
groupBox1.Paint += PaintBorderlessGroupBox;

private void PaintBorderlessGroupBox(object sender, PaintEventArgs p)
{
GroupBox box = (GroupBox)sender;
p.Graphics.Clear(SystemColors.Control);
p.Graphics.DrawString(box.Text, box.Font, Brushes.Black, 0, 0);
}

回答2:

public partial class GroupBoxEx : GroupBox
{
private Color mBorderColor = Color.White;
private float nThickness = 1;
public Color BorderColor
{
get { return mBorderColor; }
set { mBorderColor = value; }
}
public float Thickness
{
get { return nThickness; }
set { nThickness = value; }
}
public GroupBoxEx()
{
InitializeComponent();
}
// 重写
protected override void OnPaint(PaintEventArgs e)
{
SizeF vSize = e.Graphics.MeasureString(this.Text, this.Font);
e.Graphics.Clear(this.BackColor);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), 10, 1);
Pen vPen = new Pen(this.mBorderColor,nThickness); // 用属性颜色来画边框颜色
e.Graphics.DrawLine(vPen, 1, vSize.Height / 2, 8, vSize.Height / 2);
e.Graphics.DrawLine(vPen, vSize.Width + 8, vSize.Height / 2, this.Width - 2, vSize.Height / 2);
e.Graphics.DrawLine(vPen, 1, vSize.Height / 2, 1, this.Height - 2);
e.Graphics.DrawLine(vPen, 1, this.Height - 2, this.Width - 2, this.Height - 2);
e.Graphics.DrawLine(vPen, this.Width - 2, vSize.Height / 2, this.Width - 2, this.Height - 2);
}
}