vb.net 设计时带有水平文本的垂直选项卡控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24125714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vertical tab control with horizontal text at design time
提问by Neolisk
A clear omission seems to be that after applying this approach:
一个明显的遗漏似乎是在应用这种方法后:
Which is also recommended by Microsoft:
这也是微软推荐的:
There is no text on tabs at design time, so further development and support becomes a nightmare.
设计时选项卡上没有文本,因此进一步的开发和支持变成了一场噩梦。


Is there a way to make tab text also display at design time?
有没有办法让标签文本在设计时也显示?
回答by Hans Passant
Just create your own control so the custom drawing also works at design time. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. I tweaked it a bit to make it no so garish.
只需创建您自己的控件,以便自定义绘图在设计时也能正常工作。向您的项目添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖放到表单上。我稍微调整了一下,让它不那么花哨。
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));
}
}
}
回答by Bj?rn-Roger Kringsj?
You need to subclass the TabControland override OnDrawItem. Here's an example:
您需要子类化TabControl并覆盖OnDrawItem. 下面是一个例子:
Public Class UITabControl
Inherits TabControl
Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
Using brush As New SolidBrush(Me.ForeColor)
Using format As New StringFormat() With {.LineAlignment = StringAlignment.Center}
Select Case Me.Alignment
Case TabAlignment.Left
format.Alignment = StringAlignment.Near
Case TabAlignment.Top
format.Alignment = StringAlignment.Far
End Select
format.FormatFlags = (format.FormatFlags Or StringFormatFlags.NoWrap)
Dim rect As Rectangle = e.Bounds
rect.X += 3
rect.Width -= 6
e.Graphics.DrawString(Me.TabPages(e.Index).Text, Me.Font, brush, rect, format)
End Using
End Using
MyBase.OnDrawItem(e)
End Sub
End Class
回答by Alex Essilfie
Since you linked to my question, I thought it expedient to inform you of updates to my question thread.
由于您链接到我的问题,我认为通知您我的问题线程的更新是权宜之计。
I have, in an answer to my question, uploaded my code for the control in the interest of the programming community.
在回答我的问题时,为了编程社区的利益,我上传了我的控件代码。
This is a screenshot of the control at runtime.
这是运行时控件的屏幕截图。
It features full design time support, automatic resizing of tabs (up to 128px wide) and tab icons as well.
它具有完整的设计时支持、选项卡的自动调整大小(最大 128 像素宽)和选项卡图标。
The code can be downloaded from here.
代码可以从这里下载。

