Visual Basic中如何自定义TabControl的颜色与边框?
完全自定义WinForms TabControl样式(VB.NET)
以下是实现自定义背景色、表头颜色/文字色、移除边框的完整可运行代码,解决你之前代码中Tab丢失、样式混乱的问题:
Imports System.Drawing Imports System.Windows.Forms Public Class Form1 Sub New() InitializeComponent() ' 绑定绘制事件 AddHandler TabControl1.DrawItem, AddressOf TabControl1_DrawItem AddHandler TabControl1.MouseClick, AddressOf TabControl1_MouseClick ' 设置所有TabPage的背景色 For Each tp As TabPage In TabControl1.TabPages tp.BackColor = Color.LightBlue ' 自定义TabPage背景色 Next End Sub ' 自定义绘制Tab表头 Private Sub TabControl1_DrawItem(sender As Object, e As DrawItemEventArgs) Dim tabControl As TabControl = DirectCast(sender, TabControl) Dim tabPage As TabPage = tabControl.TabPages(e.Index) Dim g As Graphics = e.Graphics Dim tabBounds As Rectangle = tabControl.GetTabRect(e.Index) ' 1. 绘制Tab背景(区分选中/未选中状态) Dim tabBackColor As Color Dim textColor As Color If e.Index = tabControl.SelectedIndex Then tabBackColor = Color.DarkSlateBlue ' 选中的Tab背景色 textColor = Color.White ' 选中的Tab文字色 Else tabBackColor = Color.LightSlateGray ' 未选中的Tab背景色 textColor = Color.Black ' 未选中的Tab文字色 End If ' 填充Tab背景(覆盖默认边框) g.FillRectangle(New SolidBrush(tabBackColor), tabBounds) ' 2. 绘制Tab文字 Dim textFont As Font = tabPage.Font Dim textFormat As New StringFormat() textFormat.Alignment = StringAlignment.Center textFormat.LineAlignment = StringAlignment.Center g.DrawString(tabPage.Text, textFont, New SolidBrush(textColor), tabBounds, textFormat) ' 3. 移除TabPage的边框:通过绘制覆盖默认边框 If e.Index = tabControl.SelectedIndex Then Dim pageBounds As Rectangle = tabControl.ClientRectangle pageBounds.Inflate(-1, -1) ' 缩小区域覆盖边框 g.DrawRectangle(New Pen(tabControl.Parent.BackColor), pageBounds) End If End Sub ' 处理Tab点击切换(确保选中状态正确触发重绘) Private Sub TabControl1_MouseClick(sender As Object, e As MouseEventArgs) Dim tabControl As TabControl = DirectCast(sender, TabControl) Dim hitTest As TabControlHitTestInfo = tabControl.HitTest(e.X, e.Y) If hitTest.Type = TabControlHitTestType.TabItem Then tabControl.SelectedIndex = hitTest.Item.Index tabControl.Invalidate() ' 强制重绘 End If End Sub Private Sub InitializeComponent() Me.TabPage2 = New System.Windows.Forms.TabPage() Me.TabPage1 = New System.Windows.Forms.TabPage() Me.TabControl1 = New System.Windows.Forms.TabControl() Me.TabControl1.SuspendLayout() Me.SuspendLayout() ' TabPage2 Me.TabPage2.Location = New System.Drawing.Point(4, 22) Me.TabPage2.Name = "TabPage2" Me.TabPage2.Padding = New System.Windows.Forms.Padding(3) Me.TabPage2.Size = New System.Drawing.Size(557, 328) Me.TabPage2.TabIndex = 1 Me.TabPage2.Text = "TabPage2" Me.TabPage2.UseVisualStyleBackColor = True ' TabPage1 Me.TabPage1.Location = New System.Drawing.Point(4, 22) Me.TabPage1.Name = "TabPage1" Me.TabPage1.Padding = New System.Windows.Forms.Padding(3) Me.TabPage1.Size = New System.Drawing.Size(557, 328) Me.TabPage1.TabIndex = 0 Me.TabPage1.Text = "TabPage1" Me.TabPage1.UseVisualStyleBackColor = True ' TabControl1 - 关键设置:启用OwnerDraw模式 Me.TabControl1.Controls.Add(Me.TabPage1) Me.TabControl1.Controls.Add(Me.TabPage2) Me.TabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed Me.TabControl1.Location = New System.Drawing.Point(12, 21) Me.TabControl1.Name = "TabControl1" Me.TabControl1.SelectedIndex = 0 Me.TabControl1.Size = New System.Drawing.Size(565, 354) Me.TabControl1.TabIndex = 0 ' 移除默认边框样式 Me.TabControl1.Appearance = TabAppearance.FlatButtons Me.TabControl1.ItemSize = New Size(120, 25) ' 自定义Tab表头大小 ' Form1 Me.BackColor = System.Drawing.SystemColors.Control Me.ClientSize = New System.Drawing.Size(589, 387) Me.Controls.Add(Me.TabControl1) Me.Name = "Form1" Me.TabControl1.ResumeLayout(False) Me.ResumeLayout(False) End Sub Friend WithEvents TabPage2 As System.Windows.Forms.TabPage Friend WithEvents TabPage1 As System.Windows.Forms.TabPage Friend WithEvents TabControl1 As System.Windows.Forms.TabControl End Class
关键实现说明
- 启用自定义绘制:必须将TabControl的
DrawMode设为OwnerDrawFixed,这是自定义样式的前提。同时设置Appearance = FlatButtons简化边框处理。 - 区分Tab状态:在
DrawItem事件中判断当前Tab是否选中,分别设置不同的背景色和文字色,解决表头颜色丢失的问题。 - 移除边框:
- 表头边框:通过用背景色填充整个Tab区域,覆盖默认边框。
- TabPage边框:在选中Tab时,用父容器的背景色绘制一个覆盖默认边框的矩形。
- 修复Tab不可见问题:之前的代码硬编码了
TabPage1,现在通过e.Index动态获取当前要绘制的Tab,确保所有Tab都能正确显示。 - 文字绘制优化:使用
StringFormat实现文字居中,避免文字偏移。
内容的提问来源于stack exchange,提问作者Wabi-Sabi
相关产品推荐
相关产品推荐

