对齐按钮内的中间文本字符串
Align middle text string inside the button
我正在为一个小项目制作一些自定义控件。我创建了一个像这样的 TP1CustomFlatButton:
如果我在我的 TP1CustomFlatButton 底部添加一个带有文本的标签,这对我来说很容易。我不想处理该标签的事件,所以我使用事件 onPaint 来绘制文本。我遵循了 Microsoft 的教程,得到了像我附上的图片一样的自定义平面按钮。我想要得到的是使文本在我的 TP1CustomFlatButton 底部居中对齐。
这是我的 TP1CustomFlatButton 代码:
// constructor
public TP1CustomFlatButton()
{
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.BackColor = Color.MediumSeaGreen;
this.ForeColor = Color.White;
this.Text = "TP1CustomButton";
}
protected override void OnPaint(PaintEventArgs pevent)
{
pevent.Graphics.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, 0, this.Width, this.Height));
TextFormatFlags flags = TextFormatFlags.Bottom;
//render text
TextRenderer.DrawText(pevent.Graphics, this.Text, this.Font, new Point((int)(this.Width - Text.Length)/2,this.Height), this.ForeColor, flags);
//draw image
Image img = this.BackgroundImage;
//create rectangle to display image
Rectangle imgRec = new Rectangle(this.Width - 32 /3, this.Height - 32/ 3, 32, 32);
if(img!=null)
pevent.Graphics.DrawImage(img, imgRec);
}
我真的对坐标 X 和 Y 感到困惑。正如您所看到的代码,我试图使 "SETTINGS" 文本字符串在我的 TP1CustomFlatButton 底部居中对齐。我花了 5 个小时阅读有关 Windows Form 中控件的坐标和位置的更多信息。但是现在真的好累。
希望有人能为我的自定义控件提供任何建议或任何解决方案。
尝试如下更改 TextFormatFlags:
TextFormatFlags 标志 = TextFormatFlags.Bottom | TextFormatFlags.VerticalCenter;
此外,请查看此 link
您需要使用MeasureString()
方法来计算中间值。
另请参阅我为找到中间部分所做的更改(在 drawPoint
字段中计算)。
根据您的代码查看我的示例:
public TP1CustomFlatButton()
{
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.BackColor = Color.MediumSeaGreen;
this.ForeColor = Color.White;
this.Text = "middle";
}
protected override void OnPaint(PaintEventArgs pevent)
{
pevent.Graphics.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, 0, this.Width, this.Height));
TextFormatFlags flags = TextFormatFlags.Bottom;
//render text
String drawString = this.Text;
SizeF size = pevent.Graphics.MeasureString(drawString, this.Font);
Point drawPoint = new Point((int)this.Size.Width / 2 - (int)size.Width / 2,this.Height);
TextRenderer.DrawText(pevent.Graphics, this.Text, this.Font, drawPoint, this.ForeColor, flags);
//draw image
Image img = this.BackgroundImage;
//create rectangle to display image
Rectangle imgRec = new Rectangle(this.Width - 32 / 3, this.Height - 32 / 3, 32, 32);
if (img != null)
pevent.Graphics.DrawImage(img, imgRec);
}
输出:
我正在为一个小项目制作一些自定义控件。我创建了一个像这样的 TP1CustomFlatButton:
如果我在我的 TP1CustomFlatButton 底部添加一个带有文本的标签,这对我来说很容易。我不想处理该标签的事件,所以我使用事件 onPaint 来绘制文本。我遵循了 Microsoft 的教程,得到了像我附上的图片一样的自定义平面按钮。我想要得到的是使文本在我的 TP1CustomFlatButton 底部居中对齐。
这是我的 TP1CustomFlatButton 代码:
// constructor
public TP1CustomFlatButton()
{
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.BackColor = Color.MediumSeaGreen;
this.ForeColor = Color.White;
this.Text = "TP1CustomButton";
}
protected override void OnPaint(PaintEventArgs pevent)
{
pevent.Graphics.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, 0, this.Width, this.Height));
TextFormatFlags flags = TextFormatFlags.Bottom;
//render text
TextRenderer.DrawText(pevent.Graphics, this.Text, this.Font, new Point((int)(this.Width - Text.Length)/2,this.Height), this.ForeColor, flags);
//draw image
Image img = this.BackgroundImage;
//create rectangle to display image
Rectangle imgRec = new Rectangle(this.Width - 32 /3, this.Height - 32/ 3, 32, 32);
if(img!=null)
pevent.Graphics.DrawImage(img, imgRec);
}
我真的对坐标 X 和 Y 感到困惑。正如您所看到的代码,我试图使 "SETTINGS" 文本字符串在我的 TP1CustomFlatButton 底部居中对齐。我花了 5 个小时阅读有关 Windows Form 中控件的坐标和位置的更多信息。但是现在真的好累。
希望有人能为我的自定义控件提供任何建议或任何解决方案。
尝试如下更改 TextFormatFlags:
TextFormatFlags 标志 = TextFormatFlags.Bottom | TextFormatFlags.VerticalCenter;
此外,请查看此 link
您需要使用MeasureString()
方法来计算中间值。
另请参阅我为找到中间部分所做的更改(在 drawPoint
字段中计算)。
根据您的代码查看我的示例:
public TP1CustomFlatButton()
{
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.BackColor = Color.MediumSeaGreen;
this.ForeColor = Color.White;
this.Text = "middle";
}
protected override void OnPaint(PaintEventArgs pevent)
{
pevent.Graphics.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, 0, this.Width, this.Height));
TextFormatFlags flags = TextFormatFlags.Bottom;
//render text
String drawString = this.Text;
SizeF size = pevent.Graphics.MeasureString(drawString, this.Font);
Point drawPoint = new Point((int)this.Size.Width / 2 - (int)size.Width / 2,this.Height);
TextRenderer.DrawText(pevent.Graphics, this.Text, this.Font, drawPoint, this.ForeColor, flags);
//draw image
Image img = this.BackgroundImage;
//create rectangle to display image
Rectangle imgRec = new Rectangle(this.Width - 32 / 3, this.Height - 32 / 3, 32, 32);
if (img != null)
pevent.Graphics.DrawImage(img, imgRec);
}
输出: