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

标签 c# vb.net winforms tabcontrol

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

这也是微软推荐的:

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

enter image description here

有没有办法让标签文本在设计时也显示出来?

最佳答案

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

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));
        }
    }
}

关于c# - 设计时带有水平文本的垂直选项卡控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24125714/

相关文章:

c# - 请帮助我了解匿名代表?

c# - 如何将任何应用程序的选定文本放入 Windows 窗体应用程序

vb.net - 将数据导出到多个 Excel 工作表

c# - Winforms、数据绑定(bind)、列表框和文本框

c# - 恢复最小化窗口前要求输入密码

C# WPF - 根据背景图像动态更改文本颜色

c# - 部分观点的论证是错误的类型——这怎么可能?

.net - 获取 Windows 窗体应用程序的执行目录的路径

vb.net - 将 jpg 转换为 avi vb.net

mysql - 如何在 VB.net 中为 SQL 编码字符串