C# 更改 GroupBox 组件中的边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14643174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the border in a GroupBox component
提问by Lahib
Possible Duplicate:
How can I change the border thickness of a Groupbox on a windows form in C#?
Guys
伙计们
I am using groupbox' in Visual studio. But the Border in the groupbox is to small. I was wondering if there is a way to edit the border by giving it a color or maybe a thicker edge?
我在 Visual Studio 中使用 groupbox'。但是 groupbox 中的 Border 太小了。我想知道是否有办法通过给它一个颜色或者一个更厚的边缘来编辑边框?
As u can see in the image it is very hard to see the border around it.
正如您在图像中看到的那样,很难看到它周围的边框。
Can anyone help me with this ?
谁能帮我这个 ?
采纳答案by Mohammadreza
You can Use This:
你可以使用这个:
using System.Drawing;
using System.Drawing.Drawing2D;
使用 System.Drawing.Drawing2D;
private void Form1_Load(object sender, EventArgs e)
{
myGroupBox myGroupBox = new myGroupBox();
myGroupBox.Text = "GroupBox1";
this.Controls.Add(myGroupBox);
}
public static GraphicsPath CreatePath(float x, float y, float width, float height,
float radius, bool RoundTopLeft, bool RoundTopRight, bool RoundBottomRight, bool RoundBottomLeft)
{
float xw = x + width;
float yh = y + height;
float xwr = xw - radius;
float yhr = yh - radius;
float xr = x + radius;
float yr = y + radius;
float r2 = radius * 2;
float xwr2 = xw - r2;
float yhr2 = yh - r2;
GraphicsPath p = new GraphicsPath();
p.StartFigure();
//Top Left Corner
if (RoundTopLeft)
{
p.AddArc(x, y, r2, r2, 180, 90);
}
else
{
p.AddLine(x, yr, x, y);
p.AddLine(x, y, xr, y);
}
//Top Edge
p.AddLine(xr, y, xwr, y);
//Top Right Corner
if (RoundTopRight)
{
p.AddArc(xwr2, y, r2, r2, 270, 90);
}
else
{
p.AddLine(xwr, y, xw, y);
p.AddLine(xw, y, xw, yr);
}
//Right Edge
p.AddLine(xw, yr, xw, yhr);
//Bottom Right Corner
if (RoundBottomRight)
{
p.AddArc(xwr2, yhr2, r2, r2, 0, 90);
}
else
{
p.AddLine(xw, yhr, xw, yh);
p.AddLine(xw, yh, xwr, yh);
}
//Bottom Edge
p.AddLine(xwr, yh, xr, yh);
//Bottom Left Corner
if (RoundBottomLeft)
{
p.AddArc(x, yhr2, r2, r2, 90, 90);
}
else
{
p.AddLine(xr, yh, x, yh);
p.AddLine(x, yh, x, yhr);
}
//Left Edge
p.AddLine(x, yhr, x, yr);
p.CloseFigure();
return p;
}
class myGroupBox : GroupBox
{
public myGroupBox()
{
base.BackColor = Color.Transparent;
}
[Browsable(false)]
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}
private Color backColor = Color.Transparent;
public Color ActualBackColor
{
get { return this.backColor; }
set { this.backColor = value; }
}
protected override void OnPaint(PaintEventArgs e)
{
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
GraphicsPath gPath = CreatePath(0, borderRect.Y, (float)(this.Width - 1), borderRect.Height - 1, 5, true, true, true, true);
e.Graphics.FillPath(new SolidBrush(ActualBackColor), gPath);
e.Graphics.DrawPath(new Pen(Color.Red), gPath);
borderRect.X += 6;
borderRect.Y -= 7;
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), borderRect);
}
}