none
VB.net TabControl 控件 RRS feed

  • 问题

  • 请问TabControl 控件,里面的tabpage标签上的Text文字,怎么样改变它的颜色或者像按钮一样可以插入一些小图标?

    2013年8月1日 2:53

答案

  • 您好,

    根据您的需求,您可以新建一个自定义TabControl, 它继承于TabControl, 在其中重写OnPaint方法,按照您的需求修改TabPage标签的样式。

    以下是我的示例代码:

    Class DotNetBarTabcontrol
        Inherits TabControl
    
        Sub New()
            SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
            DoubleBuffered = True
            SizeMode = TabSizeMode.Fixed
            ItemSize = New Size(44, 136)
        End Sub
        Protected Overrides Sub CreateHandle()
            MyBase.CreateHandle()
            Alignment = TabAlignment.Left
        End Sub
    
        Function ToPen(ByVal color As Color) As Pen
            Return New Pen(color)
        End Function
    
        Function ToBrush(ByVal color As Color) As Brush
            Return New SolidBrush(color)
        End Function
    
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            Dim B As New Bitmap(Width, Height)
            Dim G As Graphics = Graphics.FromImage(B)
            Try : SelectedTab.BackColor = Color.White : Catch : End Try
            G.Clear(Color.White)
            G.FillRectangle(New SolidBrush(Color.FromArgb(246, 248, 252)), New Rectangle(0, 0, ItemSize.Height + 4, Height))
            G.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(ItemSize.Height + 3, 0), New Point(ItemSize.Height + 3, 999))
            For i = 0 To TabCount - 1
                If i = SelectedIndex Then
                    Dim x2 As Rectangle = New Rectangle(New Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width + 3, GetTabRect(i).Height - 1))
                    Dim myBlend As New ColorBlend()
                    'myBlend.Colors = {Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240)}
                    myBlend.Colors = {Color.FromArgb(232, 232, 123), Color.FromArgb(232, 232, 145), Color.FromArgb(232, 232, 34)}
                    myBlend.Positions = {0.0F, 0.5F, 1.0F}
                    Dim lgBrush As New LinearGradientBrush(x2, Color.Black, Color.Black, 90.0F)
                    lgBrush.InterpolationColors = myBlend
                    G.FillRectangle(lgBrush, x2)
                    G.DrawRectangle(New Pen(Color.FromArgb(170, 187, 204)), x2)
    
    
                    G.SmoothingMode = SmoothingMode.HighQuality
                    Dim p() As Point = {New Point(ItemSize.Height - 3, GetTabRect(i).Location.Y + 20), New Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 14), New Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 27)}
                    G.FillPolygon(Brushes.White, p)
                    G.DrawPolygon(New Pen(Color.FromArgb(170, 187, 204)), p)
    
                    If ImageList IsNot Nothing Then
                        Try
                            If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
    
                                G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x2.Location.X + 8, x2.Location.Y + 6))
                                G.DrawString("      " & TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                            Else
                                G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                            End If
                        Catch ex As Exception
                            G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                        End Try
                    Else
                        G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                    End If
    
                    G.DrawLine(New Pen(Color.FromArgb(200, 200, 250)), New Point(x2.Location.X - 1, x2.Location.Y - 1), New Point(x2.Location.X, x2.Location.Y))
                    G.DrawLine(New Pen(Color.FromArgb(200, 200, 250)), New Point(x2.Location.X - 1, x2.Bottom - 1), New Point(x2.Location.X, x2.Bottom))
                Else
                    Dim x2 As Rectangle = New Rectangle(New Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width + 3, GetTabRect(i).Height + 1))
                    G.FillRectangle(New SolidBrush(Color.FromArgb(246, 248, 252)), x2)
                    G.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(x2.Right, x2.Top), New Point(x2.Right, x2.Bottom))
                    If ImageList IsNot Nothing Then
                        Try
                            If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
                                G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x2.Location.X + 8, x2.Location.Y + 6))
                                G.DrawString("      " & TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                            Else
                                G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                            End If
                        Catch ex As Exception
                            G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                        End Try
                    Else
                        G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                    End If
                End If
            Next
    
            e.Graphics.DrawImage(B.Clone, 0, 0)
            G.Dispose() : B.Dispose()
        End Sub
    End Class
    

    代码中的加粗部分可以自己修改,改变颜色

    在窗体加载时新建自定义控件:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim tb As New DotNetBarTabcontrol
            tb.Width = "450"
            tb.Height = "400"
            Dim myTabPage1 As New TabPage()
            myTabPage1.Text = "TabPage" & (tb.TabPages.Count + 1)
            tb.TabPages.Add(myTabPage1)
            Dim myTabPage2 As New TabPage()
            myTabPage2.Text = "TabPage" & (tb.TabPages.Count + 1)
            tb.TabPages.Add(myTabPage2)
    
            Me.Controls.Add(tb)
        End Sub
    

    运行截图:

    以下是一些资料引用:
    #Awesome Custom Tab Control For VB.NET
    http://www.ultimateprogrammingtutorials.info/2012/12/custom-tab-control-in-vbnet.html

    #Painting Your Own Tabs - Second Edition
    http://www.codeproject.com/Articles/91387/Painting-Your-Own-Tabs-Second-Edition


    Franklin Chen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年8月2日 11:33
    版主

全部回复

  • 您好,

    根据您的需求,您可以新建一个自定义TabControl, 它继承于TabControl, 在其中重写OnPaint方法,按照您的需求修改TabPage标签的样式。

    以下是我的示例代码:

    Class DotNetBarTabcontrol
        Inherits TabControl
    
        Sub New()
            SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
            DoubleBuffered = True
            SizeMode = TabSizeMode.Fixed
            ItemSize = New Size(44, 136)
        End Sub
        Protected Overrides Sub CreateHandle()
            MyBase.CreateHandle()
            Alignment = TabAlignment.Left
        End Sub
    
        Function ToPen(ByVal color As Color) As Pen
            Return New Pen(color)
        End Function
    
        Function ToBrush(ByVal color As Color) As Brush
            Return New SolidBrush(color)
        End Function
    
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            Dim B As New Bitmap(Width, Height)
            Dim G As Graphics = Graphics.FromImage(B)
            Try : SelectedTab.BackColor = Color.White : Catch : End Try
            G.Clear(Color.White)
            G.FillRectangle(New SolidBrush(Color.FromArgb(246, 248, 252)), New Rectangle(0, 0, ItemSize.Height + 4, Height))
            G.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(ItemSize.Height + 3, 0), New Point(ItemSize.Height + 3, 999))
            For i = 0 To TabCount - 1
                If i = SelectedIndex Then
                    Dim x2 As Rectangle = New Rectangle(New Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width + 3, GetTabRect(i).Height - 1))
                    Dim myBlend As New ColorBlend()
                    'myBlend.Colors = {Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240)}
                    myBlend.Colors = {Color.FromArgb(232, 232, 123), Color.FromArgb(232, 232, 145), Color.FromArgb(232, 232, 34)}
                    myBlend.Positions = {0.0F, 0.5F, 1.0F}
                    Dim lgBrush As New LinearGradientBrush(x2, Color.Black, Color.Black, 90.0F)
                    lgBrush.InterpolationColors = myBlend
                    G.FillRectangle(lgBrush, x2)
                    G.DrawRectangle(New Pen(Color.FromArgb(170, 187, 204)), x2)
    
    
                    G.SmoothingMode = SmoothingMode.HighQuality
                    Dim p() As Point = {New Point(ItemSize.Height - 3, GetTabRect(i).Location.Y + 20), New Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 14), New Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 27)}
                    G.FillPolygon(Brushes.White, p)
                    G.DrawPolygon(New Pen(Color.FromArgb(170, 187, 204)), p)
    
                    If ImageList IsNot Nothing Then
                        Try
                            If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
    
                                G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x2.Location.X + 8, x2.Location.Y + 6))
                                G.DrawString("      " & TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                            Else
                                G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                            End If
                        Catch ex As Exception
                            G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                        End Try
                    Else
                        G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                    End If
    
                    G.DrawLine(New Pen(Color.FromArgb(200, 200, 250)), New Point(x2.Location.X - 1, x2.Location.Y - 1), New Point(x2.Location.X, x2.Location.Y))
                    G.DrawLine(New Pen(Color.FromArgb(200, 200, 250)), New Point(x2.Location.X - 1, x2.Bottom - 1), New Point(x2.Location.X, x2.Bottom))
                Else
                    Dim x2 As Rectangle = New Rectangle(New Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width + 3, GetTabRect(i).Height + 1))
                    G.FillRectangle(New SolidBrush(Color.FromArgb(246, 248, 252)), x2)
                    G.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(x2.Right, x2.Top), New Point(x2.Right, x2.Bottom))
                    If ImageList IsNot Nothing Then
                        Try
                            If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
                                G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x2.Location.X + 8, x2.Location.Y + 6))
                                G.DrawString("      " & TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                            Else
                                G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                            End If
                        Catch ex As Exception
                            G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                        End Try
                    Else
                        G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                    End If
                End If
            Next
    
            e.Graphics.DrawImage(B.Clone, 0, 0)
            G.Dispose() : B.Dispose()
        End Sub
    End Class
    

    代码中的加粗部分可以自己修改,改变颜色

    在窗体加载时新建自定义控件:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim tb As New DotNetBarTabcontrol
            tb.Width = "450"
            tb.Height = "400"
            Dim myTabPage1 As New TabPage()
            myTabPage1.Text = "TabPage" & (tb.TabPages.Count + 1)
            tb.TabPages.Add(myTabPage1)
            Dim myTabPage2 As New TabPage()
            myTabPage2.Text = "TabPage" & (tb.TabPages.Count + 1)
            tb.TabPages.Add(myTabPage2)
    
            Me.Controls.Add(tb)
        End Sub
    

    运行截图:

    以下是一些资料引用:
    #Awesome Custom Tab Control For VB.NET
    http://www.ultimateprogrammingtutorials.info/2012/12/custom-tab-control-in-vbnet.html

    #Painting Your Own Tabs - Second Edition
    http://www.codeproject.com/Articles/91387/Painting-Your-Own-Tabs-Second-Edition


    Franklin Chen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年8月2日 11:33
    版主
  • 非常感谢!

    另外插入小图标的话,我搜了下,简单的方法直接用imagelist就行了……


    2013年8月5日 8:24