Как повернуть метку в C#?

Я хочу показать этикетку, повернутую на 90 градусов (чтобы я мог разместить их в верхней части таблицы в качестве заголовков). Есть простой способ сделать это?

Стоит ли изучать PHP в 2026-2027 годах?
Стоит ли изучать PHP в 2026-2027 годах?
Привет всем, сегодня я хочу высказать свои соображения по поводу вопроса, который я уже много раз получал в своем сообществе: "Стоит ли изучать PHP в...
Поведение ключевого слова "this" в стрелочной функции в сравнении с нормальной функцией
Поведение ключевого слова "this" в стрелочной функции в сравнении с нормальной функцией
В JavaScript одним из самых запутанных понятий является поведение ключевого слова "this" в стрелочной и обычной функциях.
Приемы CSS-макетирования - floats и Flexbox
Приемы CSS-макетирования - floats и Flexbox
Здравствуйте, друзья-студенты! Готовы совершенствовать свои навыки веб-дизайна? Сегодня в нашем путешествии мы рассмотрим приемы CSS-верстки - в...
Тестирование функциональных ngrx-эффектов в Angular 16 с помощью Jest
В системе управления состояниями ngrx, совместимой с Angular 16, появились функциональные эффекты. Это здорово и делает код определенно легче для...
Концепция локализации и ее применение в приложениях React ⚡️
Концепция локализации и ее применение в приложениях React ⚡️
Локализация - это процесс адаптации приложения к различным языкам и культурным требованиям. Это позволяет пользователям получить опыт, соответствующий...
Пользовательский скаляр GraphQL
Пользовательский скаляр GraphQL
Листовые узлы системы типов GraphQL называются скалярами. Достигнув скалярного типа, невозможно спуститься дальше по иерархии типов. Скалярный тип...
12
0
55 679
2
Перейти к ответу Данный вопрос помечен как решенный

Ответы 2

Ответ принят как подходящий

Вам нужно будет написать свой собственный или использовать настраиваемый элемент управления.

Статья Кодовый проект, с которой вы можете начать, называется Настраиваемый текст - ориентированные элементы управления в C# - часть I (элемент управления меткой). Он содержит дополнительные функции, поэтому вы можете урезать его, если хотите.

И вот из него интересный код:

/// <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
}

Вы также можете взглянуть на элемент управления Windows ToolStrip. У него есть опция для TextDirection, которая может быть установлена ​​на Vertical90 или Vertical270, и это будет вращать текст вашей метки в соответствующем направлении.

Вторая ссылка вроде не работает.

Peter Mortensen 16.01.2014 12:41

@PeterMortensen теперь можно использовать.

Joshua Drake 16.01.2014 20:03

+1 за то, что было очень легко.

adam 05.02.2014 18:09

Ссылка на ваше видео мертва

OmarL 30.04.2019 11:21

Другие вопросы по теме