设计时带有水平文本的垂直选项卡控件

Neo*_*isk 4 c# vb.net tabcontrol winforms

应用这种方法后,一个明显的遗漏似乎是:

微软也推荐:

设计时选项卡上没有文本,因此进一步的开发和支持变成了一场噩梦。

在此输入图像描述

有没有办法让选项卡文本在设计时也显示?

Han*_*ant 5

只需创建您自己的控件,以便自定义绘图也可以在设计时工作。将新类添加到您的项目中并粘贴下面所示的代码。编译。将新控件从工具箱顶部拖放到窗体上。我稍微调整了一下,让它不那么花哨。

using System;
using System.Drawing;
using System.Windows.Forms;

class VerticalTabControl : TabControl {
    public VerticalTabControl() {
        this.Alignment = TabAlignment.Right;
        this.DrawMode = TabDrawMode.OwnerDrawFixed;
        this.SizeMode = TabSizeMode.Fixed;
        this.ItemSize = new Size(this.Font.Height * 3 / 2, 75);
    }
    public override Font Font {
        get { return base.Font;  }
        set {
            base.Font = value;
            this.ItemSize = new Size(value.Height * 3 / 2, base.ItemSize.Height);
        }
    }
    protected override void OnDrawItem(DrawItemEventArgs e) {
        using (var _textBrush = new SolidBrush(this.ForeColor)) {
            TabPage _tabPage = this.TabPages[e.Index];
            Rectangle _tabBounds = this.GetTabRect(e.Index);

            if (e.State != DrawItemState.Selected) e.DrawBackground();
            else {
                using (var brush = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.White, Color.LightGray, 90f)) {
                    e.Graphics.FillRectangle(brush, e.Bounds);
                }
            }

            StringFormat _stringFlags = new StringFormat();
            _stringFlags.Alignment = StringAlignment.Center;
            _stringFlags.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString(_tabPage.Text, this.Font, _textBrush, _tabBounds, new StringFormat(_stringFlags));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)