在自定义选项卡控件上更改鼠标上未选择的选项卡颜色

时间:2017-12-24 06:45:49

标签: wpf vb.net tabcontrol mouseenter

我创建了一个自定义标签控件。代码为: 该代码是KINNDA巨大的,请耐心阅读完整的代码

Public Class mytab
Inherits System.Windows.Forms.TabControl

Dim MainColor As Color
Dim TextColor As Color
Dim LightTheme As Boolean
Public Property UseLightTheme() As Boolean
    Get
        Return LightTheme
    End Get
    Set(state As Boolean)
        LightTheme = state
        Refresh()
    End Set
End Property

Public Property TabColor As Color
    Get
        Return MainColor
    End Get
    Set(col As Color)
        MainColor = col
        Refresh()
    End Set
End Property

Public Property FontColor As Color
    Get
        Return TextColor
    End Get
    Set(col As Color)
        TextColor = col
        Refresh()
    End Set
End Property
Sub New()
    SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer, True)
    DoubleBuffered = True
    Anchor = AnchorStyles.Top And AnchorStyles.Right And AnchorStyles.Bottom And AnchorStyles.Left
    SizeMode = TabSizeMode.Fixed
    ItemSize = New Size(100, 29)
    Size = New Size(400, 250)
    MainColor = Color.FromArgb(59, 151, 241)
    TextColor = Color.White
    LightTheme = False
    Alignment = TabAlignment.Top
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)

    'tabpage color
    If LightTheme = False Then
        Try : SelectedTab.BackColor = Color.Azure : Catch : End Try

        G.Clear(Color.Azure)
    Else
        Try : SelectedTab.BackColor = Color.FromArgb(80, 80, 97) : Catch : End Try
        G.Clear(Color.FromArgb(80, 80, 97))
    End If

    'tab bar
    If LightTheme = False Then
        If Alignment = TabAlignment.Top Then
            G.FillRectangle(New SolidBrush(Color.FromArgb(80, 80, 97)), New Rectangle(0, 0, Width, ItemSize.Height + 5))
        ElseIf Alignment = TabAlignment.Bottom Then
            G.FillRectangle(New SolidBrush(Color.FromArgb(255, 255, 255, 255)), New Rectangle(0, Height - ItemSize.Height, Width, ItemSize.Height))
        ElseIf Alignment = TabAlignment.Left Then
            G.FillRectangle(New SolidBrush(Color.FromArgb(255, 255, 255, 255)), New Rectangle(0, 0, ItemSize.Height, Height))
        ElseIf Alignment = TabAlignment.Right Then
            G.FillRectangle(New SolidBrush(Color.FromArgb(255, 255, 255, 255)), New Rectangle(Width - ItemSize.Height, 0, ItemSize.Height, Height))
        End If
    Else
        If Alignment = TabAlignment.Top Then
            G.FillRectangle(New SolidBrush(Color.FromArgb(80, 80, 97)), New Rectangle(0, 0, Width, ItemSize.Height + 5))
        ElseIf Alignment = TabAlignment.Bottom Then
            G.FillRectangle(New SolidBrush(Color.FromArgb(255, 200, 200, 200)), New Rectangle(0, Height - ItemSize.Height, Width, ItemSize.Height))
        ElseIf Alignment = TabAlignment.Left Then
            G.FillRectangle(New SolidBrush(Color.FromArgb(255, 200, 200, 200)), New Rectangle(0, 0, ItemSize.Height, Height))
        ElseIf Alignment = TabAlignment.Right Then
            G.FillRectangle(New SolidBrush(Color.FromArgb(255, 200, 200, 200)), New Rectangle(Width - ItemSize.Height, 0, ItemSize.Height, Height))
        End If
    End If


    'selected tab stuff
    Dim b2 As New SolidBrush(Color.FromArgb(44, 170, 250))
    For i = 0 To TabCount - 1
        If i = SelectedIndex Then
            Dim x2 As Rectangle = New Rectangle(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2, GetTabRect(i).Width, GetTabRect(i).Height + 4)
            Dim myblend1 As New ColorBlend()
            myblend1.Colors = {Color.FromArgb(44, 170, 250), Color.FromArgb(44, 170, 250), Color.FromArgb(44, 170, 250)} 'colors
            myblend1.Positions = {0.0F, 0.5F, 1.0F}
            Dim lgBrush1 As New LinearGradientBrush(x2, Color.Black, Color.Black, 90.0F)
            lgBrush1.InterpolationColors = myblend1
            G.FillRectangle(lgBrush1, x2)
            Dim tabTextArea As Rectangle = GetTabRect(i)
            e.Graphics.FillRectangle(b2, tabTextArea)
            Using br As New SolidBrush(Color.FromArgb(80, 80, 97))
    TextRenderer.DrawText(e.Graphics, TabPages(i).Text, New Font("Segoe UI light", 10.0), tabTextArea, TabPages(i).ForeColor)

            End Using
 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, x2.Location.Y))
                        G.DrawString("      " & TabPages(i).Text, Font, Me.ToBrush(Me.TextColor), x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                    Else
                        G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Regular), Me.ToBrush(Me.TextColor), 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.Regular), Me.ToBrush(Me.TextColor), 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.Regular), Me.ToBrush(Me.TextColor), x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
            End If

            If LightTheme = False Then
                G.DrawLine(New Pen(Color.FromArgb(80, 80, 97)), New Point(x2.Location.X - 2, x2.Location.Y - 1), New Point(x2.Location.X, x2.Location.Y))
                G.DrawLine(New Pen(Color.FromArgb(80, 80, 97)), New Point(x2.Location.X - 1, x2.Bottom - 1), New Point(x2.Location.X, x2.Bottom))
            Else
                G.DrawLine(New Pen(Color.FromArgb(80, 80, 97)), New Point(x2.Location.X - 2, x2.Location.Y - 11), New Point(x2.Location.X, x2.Location.Y))
                G.DrawLine(New Pen(Color.FromArgb(80, 80, 97)), New Point(x2.Location.X - 1, x2.Bottom - 1), New Point(x2.Location.X, x2.Bottom))
            End If

        Else
            Dim x1 As Rectangle = New Rectangle(GetTabRect(i).Location.X, GetTabRect(i).Location.Y, GetTabRect(i).Width, GetTabRect(i).Height )

   ' unselected tab color
            If LightTheme = False Then
                G.FillRectangle(New SolidBrush(Color.FromArgb(80, 80, 97)), x1)

            Else
                G.FillRectangle(New SolidBrush(Color.FromArgb(80, 80, 97)), x1)

            End If

     If LightTheme = False Then
                If ImageList IsNot Nothing Then
                    Try
                        If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
                            G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x1.Location.X, x1.Location.Y))
                            G.DrawString("      " & TabPages(i).Text, Font, Brushes.Azure, x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                        Else
                            G.DrawString(TabPages(i).Text, Font, Brushes.Azure, x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                        End If
                    Catch ex As Exception
                        G.DrawString(TabPages(i).Text, Font, Brushes.Azure, x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                    End Try
                Else
                    G.DrawString(TabPages(i).Text, Font, Brushes.Azure, x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                End If
            ElseIf LightTheme = True Then
                If ImageList IsNot Nothing Then
                    Try
                        If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
                            G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x1.Location.X, x1.Location.Y))
                            G.DrawString("      " & TabPages(i).Text, Font, ToBrush(Color.FromArgb(255, 64, 64, 64)), x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                        Else
                            G.DrawString(TabPages(i).Text, Font, ToBrush(Color.FromArgb(255, 64, 64, 64)), x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                        End If
                    Catch ex As Exception
                        G.DrawString(TabPages(i).Text, Font, ToBrush(Color.FromArgb(255, 64, 64, 64)), x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                    End Try
                Else
                    G.DrawString(TabPages(i).Text, Font, ToBrush(Color.FromArgb(255, 64, 64, 64)), x1, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                End If
                '................................................................................................

            End If
        End If
    Next

    e.Graphics.DrawImage(B.Clone, 0, 0)
    G.Dispose() : B.Dispose()

End Sub

End Class

我想要的是,当我将鼠标悬停在未选中的标签上时,标题标题颜色会像内置的tabcontrol一样变化。是否可能?

我想知道更多的事情。我希望所有人都使用最近的微软产品,如office 16,outlook 16。这些软件包含一些动画的tabcontrol。我的意思是当你点击一个标签(从菜单栏),颜色平滑淡出,不像我在制作的tabcontrol,当我选择标签时,颜色变化的颜色。最近我一直在研究WPF(我的意思是XAML DESIGNING)所以我非常确定我能在WPF中实现我想要的(我的意思是Office / OutLook所拥有的kinnda动画)。但是Office / OutLook WPF应用程序是什么?有没有办法在WinForms中实现这些动画效果,因为在wpf中创建相同的TabControl需要大量的XAML编码,可能超出我的想法..

任何帮助?

0 个答案:

没有答案