【问题标题】:How do I rotate a label in C#? [duplicate]如何在 C# 中旋转标签? [复制]
【发布时间】:2010-09-29 20:03:03
【问题描述】:

我想显示一个旋转 90 度的标签(这样我就可以将它们放在表格顶部作为标题)。有没有简单的方法可以做到这一点?

【问题讨论】:

    标签: c# winforms text


    【解决方案1】:

    您需要自己编写或使用自定义控件。

    您可以从The Code Project 文章开始Customized Text - Orientated Controls in C# - Part I (Label Control)。这包含额外的功能,因此您应该可以根据需要对其进行修剪。

    这里有一些有趣的代码:

    /// <summary>
    /// This is a lable, in which you can set the text in any direction/angle
    /// </summary>
    
    #region Orientation
    
    //Orientation of the text
    
    public enum Orientation
    {
        Circle,
        Arc,
        Rotate
    }
    
    public enum Direction
    {
        Clockwise,
        AntiClockwise
    }
    
    #endregion
    
    public class OrientedTextLabel : System.Windows.Forms.Label
    {
        #region Variables
    
        private double rotationAngle;
        private string text;
        private Orientation textOrientation;
        private Direction textDirection;
    
        #endregion
    
        #region Constructor
    
        public OrientedTextLabel()
        {
            //Setting the initial condition.
            rotationAngle = 0d;
            textOrientation = Orientation.Rotate;
            this.Size = new Size(105,12);
        }
    
        #endregion
    
        #region Properties
    
        [Description("Rotation Angle"),Category("Appearance")]
        public double RotationAngle
        {
            get
            {
                return rotationAngle;
            }
            set
            {
                rotationAngle = value;
                this.Invalidate();
            }
        }
    
        [Description("Kind of Text Orientation"),Category("Appearance")]
        public Orientation TextOrientation
        {
            get
            {
                return textOrientation;
            }
            set
            {
                textOrientation = value;
                this.Invalidate();
            }
        }
    
        [Description("Direction of the Text"),Category("Appearance")]
        public Direction TextDirection
        {
            get
            {
                return textDirection;
            }
            set
            {
                textDirection = value;
                this.Invalidate();
            }
        }
    
        [Description("Display Text"),Category("Appearance")]
        public override string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
                this.Invalidate();
            }
        }
    
        #endregion
    
        #region Method
    
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
    
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.Trimming = StringTrimming.None;
    
            Brush textBrush = new SolidBrush(this.ForeColor);
    
            //Getting the width and height of the text, which we are going to write
            float width = graphics.MeasureString(text,this.Font).Width;
            float height = graphics.MeasureString(text,this.Font).Height;
    
            //The radius is set to 0.9 of the width or height, b'cos not to
            //hide and part of the text at any stage
            float radius = 0f;
            if (ClientRectangle.Width<ClientRectangle.Height)
            {
                radius = ClientRectangle.Width *0.9f/2;
            }
            else
            {
                radius = ClientRectangle.Height *0.9f/2;
            }
    
            //Setting the text according to the selection
            switch (textOrientation)
            {
                case Orientation.Arc:
                {
                    //Arc angle must be get from the length of the text.
                    float arcAngle = (2*width/radius)/text.Length;
                    if(textDirection == Direction.Clockwise)
                    {
                        for (int i=0; i<text.Length; i++)
                        {
                            graphics.TranslateTransform(
                                (float)(radius*(1 - Math.Cos(arcAngle*i + rotationAngle/180 * Math.PI))),
                                (float)(radius*(1 - Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
                            graphics.RotateTransform((-90 + (float)rotationAngle + 180*arcAngle*i/(float)Math.PI));
                            graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
                            graphics.ResetTransform();
                        }
                    }
                    else
                    {
                        for (int i=0; i<text.Length; i++)
                        {
                            graphics.TranslateTransform(
                                (float)(radius*(1 - Math.Cos(arcAngle*i + rotationAngle/180*Math.PI))),
                                (float)(radius*(1 + Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
                            graphics.RotateTransform((-90 - (float)rotationAngle - 180*arcAngle*i/(float)Math.PI));
                            graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
                            graphics.ResetTransform();
                        }
                    }
                    break;
                }
                case Orientation.Circle:
                {
                    if (textDirection == Direction.Clockwise)
                    {
                        for(int i=0;i<text.Length;i++)
                        {
                            graphics.TranslateTransform(
                                (float)(radius*(1 - Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
                                (float)(radius*(1 - Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
                            graphics.RotateTransform(-90 + (float)rotationAngle + (360/text.Length)*i);
                            graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
                            graphics.ResetTransform();
                        }
                    }
                    else
                    {
                        for(int i=0;i<text.Length;i++)
                        {
                            graphics.TranslateTransform(
                                (float)(radius*(1 - Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
                                (float)(radius*(1 + Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
                            graphics.RotateTransform(-90 - (float)rotationAngle - (360/text.Length)*i);
                            graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
                            graphics.ResetTransform();
                        }
    
                    }
                    break;
                }
    
                case Orientation.Rotate:
                {
                    //For rotation, who about rotation?
                    double angle = (rotationAngle/180)*Math.PI;
                    graphics.TranslateTransform(
                        (ClientRectangle.Width+(float)(height*Math.Sin(angle))-(float)(width*Math.Cos(angle)))/2,
                        (ClientRectangle.Height-(float)(height*Math.Cos(angle))-(float)(width*Math.Sin(angle)))/2);
                    graphics.RotateTransform((float)rotationAngle);
                    graphics.DrawString(text,this.Font,textBrush,0,0);
                    graphics.ResetTransform();
    
                    break;
                }
            }
        }
        #endregion
    }
    

    【讨论】:

      【解决方案2】:

      您还可以查看 Windows ToolStrip 控件。它有一个 TextDirection 选项,可以设置为 Vertical90 或 Vertical270,这会将您的标签文本旋转到适当的方向。

      【讨论】:

      • 第二个链接好像坏了。
      • @PeterMortensen 现在应该可以使用了。
      • +1 非常简单。
      • 视频链接失效
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 2022-01-19
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多