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

  • Vertical Tab Control with horizontal text in Winforms

  • 微软也推荐:
  • How to: Display Side-Aligned Tabs with TabControl

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

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

    最佳答案

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

    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/

    10-17 01:58