![](/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.