标签很简单,您只需覆盖Paint事件并垂直绘制文本即可。请注意,GDI已针对水平绘制文本进行了优化。如果你旋转文字(即使你旋转90度的倍数),它看起来会更糟。
原文链接:https://www.f2er.com/windows/372258.html也许最好的办法是将文本绘制(或获取自己绘制的标签)到位图上,然后显示旋转的位图。
一些用于使用垂直文本绘制自定义控件的C#代码。请注意,如果文本不是水平的,则ClearType文本永远不会起作用:
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; public partial class VerticalLabel : UserControl { public VerticalLabel() { InitializeComponent(); } private void VerticalLabel_SizeChanged(object sender,EventArgs e) { GenerateTexture(); } private void GenerateTexture() { StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; format.Trimming = StringTrimming.EllipsisCharacter; Bitmap img = new Bitmap(this.Height,this.Width); Graphics G = Graphics.FromImage(img); G.Clear(this.BackColor); SolidBrush brush_text = new SolidBrush(this.ForeColor); G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; G.DrawString(this.Name,this.Font,brush_text,new Rectangle(0,img.Width,img.Height),format); brush_text.Dispose(); img.RotateFlip(RotateFlipType.Rotate270FlipNone); this.BackgroundImage = img; } }