C# 如何在win表单中使分组框文本对齐居中?
我使用的是一个分组框,里面有几个控件 我的要求是将分组框标题设置为分组框的中间位置,而不是左侧C# 如何在win表单中使分组框文本对齐居中?,c#,winforms,C#,Winforms,我使用的是一个分组框,里面有几个控件 我的要求是将分组框标题设置为分组框的中间位置,而不是左侧 如何创建?尝试使用面板作为容器创建自定义控件,并在其周围绘制边框,然后可以完全控制标题的对齐方式 如果您想要一种简单的方法,可以将groupbox的标题保留为空文本,然后在groupbox的中心位置放置一个标签。您也可以将此定义为用户控件,这样您就不必重复这样做了。 不幸的是,您可以通过使用RealToLeFT属性在右边设置标题,但是没有属性在中间设置。 您可以做的是在GroupBox中设置一个空文本
如何创建?尝试使用
面板
作为容器创建自定义控件,并在其周围绘制边框,然后可以完全控制标题的对齐方式
如果您想要一种简单的方法,可以将groupbox的标题保留为空文本,然后在groupbox的中心位置放置一个
标签。您也可以将此定义为用户控件,这样您就不必重复这样做了。 不幸的是,您可以通过使用RealToLeFT属性在右边设置标题,但是没有属性在中间设置。
您可以做的是在GroupBox中设置一个空文本,创建一个带有标题的标签,并将该标签放在GroupBox上方(具有相同的父项)
您可以通过调用以下过程在表单初始化时动态执行此操作:
private void CenterGroupBoxTitle(GroupBox groupbox)
{
Label label = new Label() ;
label.Text = groupbox.Text ;
groupbox.Text = "" ;
label.Left = groupbox.Left+(groupbox.Width-label.Width)/2 ;
label.Top = groupbox.Top + 2 ; // 2 is an example : adjust the constant
label.Parent = groupbox.Parent ;
label.BringToFront() ;
}
您可以像这样扩展分组框类
public class CustomGrpBox : GroupBox
{
private string _Text = "";
public CustomGrpBox()
{
//set the base text to empty
//base class will draw empty string
//in such way we see only text what we draw
base.Text = "";
}
//create a new property a
[Browsable(true)]
[Category("Appearance")]
[DefaultValue("GroupBoxText")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public new string Text
{
get
{
return _Text;
}
set
{
_Text = value;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
//first let the base class to draw the control
base.OnPaint(e);
//create a brush with fore color
SolidBrush colorBrush = new SolidBrush(this.ForeColor);
//create a brush with back color
var backColor = new SolidBrush(this.BackColor);
//measure the text size
var size = TextRenderer.MeasureText(this.Text, this.Font);
// evaluate the postiong of text from left;
int left = (this.Width - size.Width) / 2;
//draw a fill rectangle in order to remove the border
e.Graphics.FillRectangle(backColor, new Rectangle(left, 0, size.Width, size.Height));
//draw the text Now
e.Graphics.DrawString(this.Text, this.Font, colorBrush, new PointF(left, 0));
}
}
private void Form2_Load(object sender, EventArgs e)
{
customGrpBox1.Text = "Hello World";
}
将上述类添加到您的项目中,并使用“CustomGrpBox”而不是“GroupBox”,后者将在您的工具箱中生成后创建
你可以像这样随时设置文本
public class CustomGrpBox : GroupBox
{
private string _Text = "";
public CustomGrpBox()
{
//set the base text to empty
//base class will draw empty string
//in such way we see only text what we draw
base.Text = "";
}
//create a new property a
[Browsable(true)]
[Category("Appearance")]
[DefaultValue("GroupBoxText")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public new string Text
{
get
{
return _Text;
}
set
{
_Text = value;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
//first let the base class to draw the control
base.OnPaint(e);
//create a brush with fore color
SolidBrush colorBrush = new SolidBrush(this.ForeColor);
//create a brush with back color
var backColor = new SolidBrush(this.BackColor);
//measure the text size
var size = TextRenderer.MeasureText(this.Text, this.Font);
// evaluate the postiong of text from left;
int left = (this.Width - size.Width) / 2;
//draw a fill rectangle in order to remove the border
e.Graphics.FillRectangle(backColor, new Rectangle(left, 0, size.Width, size.Height));
//draw the text Now
e.Graphics.DrawString(this.Text, this.Font, colorBrush, new PointF(left, 0));
}
}
private void Form2_Load(object sender, EventArgs e)
{
customGrpBox1.Text = "Hello World";
}
在设计时VisualStudio中看起来是这样的,这不是一个雄辩的解决方案,但如果您有一个简单的GroupBox,它保持不变(大小相同,您只需在开头和结尾填充空格)
示例:GroupBox.Text=“这是GroupBox文本”
空格的填充量将取决于方框的长度。
当然,你会丢失GroupBox顶部的一些开始和结束行,如果这很重要,那么答案3似乎是一个很好的解决方案。@Harry,我想他指的是GroupBox的标题?是吗?是的,我错了。我以为它包含了一些链接中中心控件的材料。扩展GroupBox并将标签重新绘制到设计中红色位置是最简单的方式,因为其他回答者张贴了它。