C# vertical label in a Windows Forms

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

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

9条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 12:17

    It absolutely works. I found it on net and little changed

    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();
            }
        }
    }
    

提交回复
热议问题