当前位置:  开发笔记 > 编程语言 > 正文

如何在C#中旋转标签?

如何解决《如何在C#中旋转标签?》经验,为你挑选了2个好方法。

我想显示一个旋转90度的标签(所以我可以将一堆标签放在桌子顶部作为标题).是否有捷径可寻?



1> Bob..:

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

一个代码项目的文章,你可以开始进行自定义文本-定向控制在C#中-第一部分(标签控制).这包含额外的功能,因此如果您愿意,您应该可以将其修剪下来.

这里有一些感兴趣的代码:

/// 
/// This is a lable, in which you can set the text in any direction/angle
/// 

#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



2> Joshua Drake..:

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

概述:ToolStrip类(MSDN)

关于其使用的简短视频:http://msdn.microsoft.com/en-us/vstudio/bb798042对于轮换,请参见3:05进行轮换.

推荐阅读
周扒pi
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有