在Visual Studio中通过设计面板翻转标签

时间:2010-09-10 14:22:36

标签: c# visual-studio user-interface

是否有可以打开或用于将标签旋转90度的peramerter或设置?我想通过设计面板使用它。

如果可能,我希望避免通过代码执行此操作。

我目前正在使用c#作为我的基础

2 个答案:

答案 0 :(得分:3)

没有将文字旋转90度的属性。你需要自己编写控件。

答案 1 :(得分:2)

在项目中添加一个新类并粘贴下面显示的代码。编译。将新控件从工具箱顶部拖放到表单上。要注意不那么出色的渲染质量和测量弦长的正常麻烦。

using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

class VerticalLabel : Label {
    private SizeF mSize;
    public VerticalLabel() {
        base.AutoSize = false;
    }
    [Browsable(false)]
    public override bool AutoSize {
        get { return false; }
        set { base.AutoSize = false; }
    }
    public override string Text {
        get { return base.Text; }
        set { base.Text = value; calculateSize(); }
    }
    public override Font Font {
        get { return base.Font; }
        set { base.Font = value; calculateSize(); }
    }
    protected override void OnPaint(PaintEventArgs e) {
        using (var br = new SolidBrush(this.ForeColor)) {
            e.Graphics.RotateTransform(-90);
            e.Graphics.DrawString(Text, Font, br, -mSize.Width, 0);
        }
    }
    private void calculateSize() {
        using (var gr = this.CreateGraphics()) {
            mSize = gr.MeasureString(this.Text, this.Font);
            this.Size = new Size((int)mSize.Height, (int)mSize.Width);
        }
    }
}