The VB6 SSTab control allows you to add a control over the tab area (i.e. where the tab page’s caption appears); the TabControl .NET control (as well as the VB6SSTab control, which inherits from TabControl) doesn’t offer this feature.
However, you can simulate a label in the tab area of the .NET control by creating a class that inherits from VB6SSTab and then overrides its OnDrawItem method. The following example shows a possible implementation of such a control, that displays centered rather than left-aligned captions
Public Class CustomVb6SSTab
Inherits VB6SSTab
Private m_HighlightedColor As Color = SystemColors.WindowText
Public Property HighlightedColor() As Color
Get
Return m_HighlightedColor
End Get
Set(ByVal value As Color)
m_HighlightedColor = value
End Set
End Property
Protected Overrides Sub OnDrawItem(e As System.Windows.Forms.DrawItemEventArgs)
MyBase.OnDrawItem(e)
If e.Index <> MyBase.SelectedIndex AndAlso _
Me.HighlightedColor <> SystemColors.WindowText Then Exit Sub
Dim page As TabPage = MyBase.NetObject.TabPages(e.Index)
Dim rect As New RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, _
e.Bounds.Height - 2)
Dim gr As Graphics = e.Graphics
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Near
sf.Trimming = StringTrimming.Character
sf.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show
sf.Alignment = MyBase.TabCaptionAlignment
sf.LineAlignment = StringAlignment.Center
Using br As Brush = New SolidBrush(page.BackColor)
gr.FillRectangle(br, rect)
End Using
Using br As Brush = New SolidBrush(Me.HighlightedColor)
gr.DrawString(page.Text, Me.Font, br, rect, sf)
End Using
End Sub
End Class
public Class CustomVb6SSTab : VB6SSTab
{
private Color m_HighlightedColor = SystemColors.WindowText;
public Color HighlightedColor
{
get
{
return m_HighlightedColor;
}
set
{
m_HighlightedColor = value;
}
}
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (e.Index != MyBase.SelectedIndex &&
this.HighlightedColor != SystemColors.WindowText)
{
return;
}
TabPage page = base.NetObject.TabPages[e.Index];
RectangleF rect = new RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width,
e.Bounds.Height - 2);
Graphics gr = e.Graphics;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.Trimming = StringTrimming.Character;
sf.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show;
sf.Alignment = base.TabCaptionAlignment;
sf.LineAlignment = StringAlignment.Center;
using (Brush br = new SolidBrush(page.BackColor))
gr.FillRectangle(br, rect);
using (Brush br = new SolidBrush(Me.HighlightedColor))
gr.DrawString(page.Text, Methis.Font, br, rect, sf);
}
}