C# vertical label in a Windows Forms

前端 未结 9 1231
独厮守ぢ
独厮守ぢ 2020-11-28 11:40

Is it possible to display a label vertically in a Windows Forms?

相关标签:
9条回答
  • 2020-11-28 12:33

    Create a class myLabel which can rotate it's Text on any angle specified by you.

    You can use it by code or simply dragging from 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);
        }
    }
    

    Now this custom control is used into your form.

    You have to set below properties

     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
    
    0 讨论(0)
  • 2020-11-28 12:35

    I just turned off the AutoSize property and resized the label vertically. I made the label wide enough for only one character. Then I changed TextAlign to center to make the alignment look better. This worked great for me.

    0 讨论(0)
  • 2020-11-28 12:41

    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.

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题