![](/img/trans.png)
[英]Override/Change .NET Winforms Font Vertical Alignment Behavior for Label Control
[英]Alignment in custom winforms label control
我用了这个答案: ForeColor中的Alpha创建了一个自定义标签元素,该标签元素允许通过ARGB进行淡化,这与默认标签不同。
using System;
using System.Drawing;
using System.Windows.Forms;
public class MyLabel : Label {
protected override void OnPaint(PaintEventArgs e) {
Rectangle rc = this.ClientRectangle;
StringFormat fmt = new StringFormat(StringFormat.GenericTypographic);
using (var br = new SolidBrush(this.ForeColor)) {
e.Graphics.DrawString(this.Text, this.Font, br, rc, fmt);
}
}
}
我很好奇如何将TextAlign实现到此类中,以允许文本内容正确对齐。
感谢@Aybe的评论,我确定需要将Alignment添加到StringFormat var fmt中,如下所示:
fmt.Alignment = StringAlignment.Center;
使整个类看起来像这样:
using System;
using System.Drawing;
using System.Windows.Forms;
public class MyLabel : Label {
protected override void OnPaint(PaintEventArgs e) {
Rectangle rc = this.ClientRectangle;
StringFormat fmt = new StringFormat(StringFormat.GenericTypographic);
fmt.Alignment = StringAlignment.Center;
using (var br = new SolidBrush(this.ForeColor))
{
e.Graphics.DrawString(this.Text, this.Font, br, rc, fmt);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.