在Windows窗体C#标签的垂直 [英] C# vertical label in a Windows Forms
问题描述
是否有可能在 Windows窗体垂直显示的标签?
Is it possible to display a label vertically in a Windows Forms?
推荐答案
标签很容易,你需要做的就是重写Paint事件和垂直方向绘制文本。请注意,GDI为水平绘制文本进行了优化。如果您旋转文本(即使你通过90度的倍数旋转),它会看起来特别糟糕。
Labels are easy, all you have to do is override the Paint event and draw the text vertically. Do note that GDI is optimised for Drawing text horizontally. If you rotate text (even if you rotate through multiples of 90 degrees) it will looks notably worse.
也许是做的最好的事情就是提醒你的文字(或获得一个标签,本身画)到一个位图,然后显示位图旋转。
Perhaps the best thing to do is draw your text (or get a label to draw itself) onto a bitmap, then display the bitmap rotated.
一些C#code绘制自定义控件与垂直文本。需要注意的是文本的ClearType的作品从不如果文本不是水平:
Some C# code for drawing a Custom Control with vertical text. Note that ClearType text NEVER works if the text is not horizontal:
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;
}
}
这篇关于在Windows窗体C#标签的垂直的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!