Windows窗体中的C#垂直标签

是否可以在Windows窗体中垂直显示标签?

标签很简单,您只需覆盖Paint事件并垂直绘制文本即可。 请注意,GDI已针对水平绘制文本进行了优化。 如果你旋转文字(即使你旋转90度的倍数),它看起来会更糟。

也许最好的办法是将文本绘制(或获取自己绘制的标签)到位图上,然后显示旋转的位图。

一些用于使用垂直文本绘制自定义控件的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, 0, img.Width, img.Height), format); brush_text.Dispose(); img.RotateFlip(RotateFlipType.Rotate270FlipNone); this.BackgroundImage = img; } } 

创建一个myLabel类,它可以在您指定的任何角度上旋转它的Text。

您可以通过代码使用它或只是从ToolBox拖动

 using System.Drawing; class myLabel:System.Windows.Forms.Label { public int RotateAngle { get; set; } // to rotate your text public string NewText { get; set; } // to draw text protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { Brush b =new SolidBrush(this.ForeColor); e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2); e.Graphics.RotateTransform(this.RotateAngle); e.Graphics.DrawString(this.NewText, this.Font,b , 0f, 0f); base.OnPaint(e); } } 

现在,此自定义控件将用于您的表单。

您必须在下面设置属性

  1. mylbl.Text = ""; //which can be changed by NewText property 2. mylbl.AutoSize = false; // adjust according to your text 3. mylbl.NewText = "Hello"; // whatever you want to display 4. mylbl.ForeColor = Color.Red; // color to display 5. mylbl.RotateAngle = -90; //angle to rotate 

我扩展了Javed Akram的答案,自动调整小部件的大小(我需要这个function)。 它适用于正角度和负角度,Javed声明的方式:

  1. mylbl.Text = ""; // which can be changed by NewText property 2. mylbl.AutoSize = false; // adjust according to your text 3. mylbl.NewText = "Hello"; // whatever you want to display 4. mylbl.ForeColor = Color.Red; // color to display 5. mylbl.RotateAngle = -90; // angle to rotate 

这是代码:

 public class RotatingLabel : System.Windows.Forms.Label { private int m_RotateAngle = 0; private string m_NewText = string.Empty; public int RotateAngle { get { return m_RotateAngle; } set { m_RotateAngle = value; Invalidate(); } } public string NewText { get { return m_NewText; } set { m_NewText = value; Invalidate(); } } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { Func DegToRad = (angle) => Math.PI * angle / 180.0; Brush b = new SolidBrush(this.ForeColor); SizeF size = e.Graphics.MeasureString(this.NewText, this.Font, this.Parent.Width); int normalAngle = ((RotateAngle % 360) + 360) % 360; double normaleRads = DegToRad(normalAngle); int hSinTheta = (int)Math.Ceiling((size.Height * Math.Sin(normaleRads))); int wCosTheta = (int)Math.Ceiling((size.Width * Math.Cos(normaleRads))); int wSinTheta = (int)Math.Ceiling((size.Width * Math.Sin(normaleRads))); int hCosTheta = (int)Math.Ceiling((size.Height * Math.Cos(normaleRads))); int rotatedWidth = Math.Abs(hSinTheta) + Math.Abs(wCosTheta); int rotatedHeight = Math.Abs(wSinTheta) + Math.Abs(hCosTheta); this.Width = rotatedWidth; this.Height = rotatedHeight; int numQuadrants = (normalAngle >= 0 && normalAngle < 90) ? 1 : (normalAngle >= 90 && normalAngle < 180) ? 2 : (normalAngle >= 180 && normalAngle < 270) ? 3 : (normalAngle >= 270 && normalAngle < 360) ? 4 : 0; int horizShift = 0; int vertShift = 0; if (numQuadrants == 1) { horizShift = Math.Abs(hSinTheta); } else if (numQuadrants == 2) { horizShift = rotatedWidth; vertShift = Math.Abs(hCosTheta); } else if (numQuadrants == 3) { horizShift = Math.Abs(wCosTheta); vertShift = rotatedHeight; } else if (numQuadrants == 4) { vertShift = Math.Abs(wSinTheta); } e.Graphics.TranslateTransform(horizShift, vertShift); e.Graphics.RotateTransform(this.RotateAngle); e.Graphics.DrawString(this.NewText, this.Font, b, 0f, 0f); base.OnPaint(e); } } 

我找到了一种简单的方法,无需在项目中添加代码或类!

创建标签时,只需添加:

 this.label1.text = "V\nE\nR\nT\nI\nC\nA\nL\n"; 

这对我有用!

您可以在OnPaint事件或Paint方法中旋转文本而不是标签控件:

 private void uc1_Paint(object sender, PaintEventArgs e) { string Name; var g = e.Graphics; g.DrawString(Name, new Font("Tahoma", 8), Brushes.Black, 0, 0, new StringFormat(StringFormatFlags.DirectionVertical)); } 

2015年更新旧post。 由于大多数其他答案似乎在可用性方面严重影响VS2013的设计师,我建议这个解决方案:

http://www.codeproject.com/Articles/19774/Extended-Vertical-Label-Control-in-C-NET

来自其他人的二手件

杰里米

 public partial class VerticalLabel_UserControl : UserControl { private IComponentChangeService _changeService; private string strPropertyText = "Vertical Text"; public VerticalLabel_UserControl() { InitializeComponent(); } [EditorBrowsable(EditorBrowsableState.Always)] [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [Bindable(true)] public override string Text { get { return base.Text; } set { base.Text = value; this.Invalidate(); } } private void VerticalLabel_UserControl_SizeChanged(object sender, EventArgs e) { GenerateTexture(); } protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); } 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.strPropertyText, this.Font, brush_text, new Rectangle(0, 0, img.Width, img.Height), format); img.RotateFlip(RotateFlipType.Rotate270FlipNone); this.BackgroundImage = img; brush_text.Dispose(); } public override System.ComponentModel.ISite Site { get { return base.Site; } set { _changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService)); if (_changeService != null) _changeService.ComponentChanged -= new ComponentChangedEventHandler(OnComponentChanged); base.Site = value; if (!DesignMode) return; _changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService)); if (_changeService != null) _changeService.ComponentChanged += new ComponentChangedEventHandler(OnComponentChanged); } } private void OnComponentChanged(object sender, ComponentChangedEventArgs ce) { VerticalLabel_UserControl label = ce.Component as VerticalLabel_UserControl; if (label == null || !label.DesignMode) return; if (((IComponent)ce.Component).Site == null || ce.Member == null || ce.Member.Name != "Text") return; //Causes the default text to be updated string strName = this.Name.ToLower(); string strText = this.Text.ToLower(); if (strText.Contains(strName)) { this.Text = "Vertical Text"; } else { strPropertyText = this.Text; } //Prints the text vertically GenerateTexture(); } } 

它绝对有效。 我在网上找到它并且几乎没有变化

 using System; using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.ComponentModel; public class VerticalLabel : System.Windows.Forms.Label { private bool bFlip = true; public VerticalLabel() { } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.Trimming = StringTrimming.None; stringFormat.FormatFlags = StringFormatFlags.DirectionVertical; Brush textBrush = new SolidBrush(this.ForeColor); Matrix storedState = g.Transform; if (bFlip) { g.RotateTransform(180f); g.TranslateTransform(-ClientRectangle.Width,-ClientRectangle.Height); } g.DrawString( this.Text, this.Font, textBrush, ClientRectangle, stringFormat); g.Transform = storedState; } [Description("When this parameter is true the VLabel flips at 180 degrees."),Category("Appearance")] public bool Flip180 { get { return bFlip; } set { bFlip = value; this.Invalidate(); } } } 

我只是关闭了AutoSize属性并垂直调整了标签大小。 我让标签足够宽,只有一个角色。 然后我将TextAlign更改为居中以使对齐看起来更好。 这对我很有用。