[Control] Ein eigenes TabControl

  • VB.NET

Es gibt 16 Antworten in diesem Thema. Der letzte Beitrag () ist von iEi.

    [Control] Ein eigenes TabControl

    Hey Com,

    ich will mir für meinen Browser ein eigenes TabControl schreiben, es sollte die Grundfunktionen des normalen TabControls haben und noch was spezielles und zwar: Die Header also wo drin steht: TabPage1 o.Ä. sollen etwas abgerundet sein, wie kann ich das machen ? Ich habe noch nie so ein Control geschrieben ? Könnt Ihr mir helfen ? Jeder Vorschlag wird angenommen und um jede Hilfe wäre ich dankbar :)

    Greet iEi

    iEi schrieb:

    Hab mich noch nie so richtig mit GDI+ befasst
    Dann solltest du das mal tun, das ist nämlich Grundvorraussetzung, um Controls zu erstellen.
    Ich hab nafets (oder? warst das du? da bin ich mir gerade gar nicht sicher. @nafets3646:) mal den Code dafür geschickt, vielleicht ist er ja so nett, und gibt ihn dir (wenn du mich jetzt fragst, warum ich ihn dir nicht einfach selbst gebe: ich hab den Code in dieser Form so nicht auf meinem PC und müsste ihn erst neu erstellen).
    @Artentus
    Verwechselst du mich da nicht zufällig? Ich habe so eine Nachricht nicht (mehr) im Postfach, kann mich jedoch auch nicht erinnern, eine Nachricht von dir mit diesem Inhalt empfangen zu haben :D.
    Und was spricht dagegen es einfach mal zu probieren. Spiele dich nen bisschen mit GDI und mach mal etwas ohne alles irgendwie zusammen zu kopieren.


    Opensource Audio-Bibliothek auf github: KLICK, im Showroom oder auf NuGet.
    Auch wenn ich iEi nicht gut kenne, aber so weiß ich doch eines. Er würde lieber sterben als WPF zu verwenden. Leider geht das hier vielen so, obwohl wpf für schönes GUIs optimal ist.


    Opensource Audio-Bibliothek auf github: KLICK, im Showroom oder auf NuGet.
    @iEi: Schau dir das hier mal an.
    Code

    VB.NET-Quellcode

    1. Imports System.Drawing.Drawing2D
    2. Imports System.Drawing.Imaging
    3. Namespace Components
    4. Class SideTab
    5. Inherits TabControl
    6. ' Original author: mavamaarten (http://www.hackforums.net/member.php?action=profile&uid=244760)
    7. ' Original code: http://pastebin.com/81E1i8ez
    8. ' Edited by idb (http://s.olution.cc/)
    9. Private State As New MouseState
    10. Private Structure MouseState
    11. Dim Hover As Boolean
    12. Dim Coordinates As Point
    13. End Structure
    14. #Region "Constructor"
    15. Sub New()
    16. SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
    17. DoubleBuffered = True
    18. SizeMode = TabSizeMode.Fixed
    19. ItemSize = New Size(44, 136)
    20. End Sub
    21. #End Region
    22. #Region "Properties"
    23. Private _InactiveIconOpacity As Integer = 50
    24. Public Property InactiveIconOpacity As Integer
    25. Get
    26. Return _InactiveIconOpacity
    27. End Get
    28. Set(ByVal value As Integer)
    29. If IsNothing(value) Then value = 50
    30. If value < 0 Then value = 0
    31. If value > 100 Then value = 100
    32. _InactiveIconOpacity = value
    33. End Set
    34. End Property
    35. #End Region
    36. #Region "Methods"
    37. Private Function ToPen(ByVal color As Color) As Pen
    38. Return New Pen(color)
    39. End Function
    40. Private Function ToBrush(ByVal color As Color) As Brush
    41. Return New SolidBrush(color)
    42. End Function
    43. Private Function ImageOpacity(ByVal Image As Bitmap, ByVal Opacity As Single) As Image
    44. Dim Result As New Bitmap(Image.Width, Image.Height, Imaging.PixelFormat.Format32bppArgb)
    45. With Image
    46. Opacity = Math.Min(Opacity, 100)
    47. Using Attributes As New Imaging.ImageAttributes
    48. Dim Matrix As New Imaging.ColorMatrix
    49. Matrix.Matrix33 = Opacity / 100.0F
    50. Attributes.SetColorMatrix(Matrix)
    51. Dim Points() As PointF = {New Point(0, 0), New Point(.Width, 0), New Point(0, .Height)}
    52. Using g As Graphics = Graphics.FromImage(Result)
    53. g.Clear(Color.Transparent)
    54. g.DrawImage(Image, Points, New RectangleF(Point.Empty, .Size), GraphicsUnit.Pixel, Attributes)
    55. End Using
    56. End Using
    57. End With
    58. Return Result
    59. End Function
    60. #End Region
    61. #Region "Override Methods"
    62. Protected Overrides Sub CreateHandle()
    63. MyBase.CreateHandle()
    64. MyBase.DoubleBuffered = True
    65. SizeMode = TabSizeMode.Fixed
    66. Alignment = TabAlignment.Left
    67. SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
    68. End Sub
    69. Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
    70. State.Hover = True
    71. MyBase.OnMouseHover(e)
    72. End Sub
    73. Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
    74. State.Hover = False
    75. For Each t As TabPage In MyBase.TabPages
    76. If t.DisplayRectangle.Contains(State.Coordinates) Then
    77. MyBase.Invalidate()
    78. Exit For
    79. End If
    80. Next
    81. MyBase.OnMouseHover(e)
    82. End Sub
    83. Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
    84. State.Coordinates = e.Location
    85. For Each t As TabPage In MyBase.TabPages
    86. If t.DisplayRectangle.Contains(e.Location) Then
    87. MyBase.Invalidate()
    88. Exit For
    89. End If
    90. Next
    91. MyBase.OnMouseMove(e)
    92. End Sub
    93. Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    94. Dim B As New Bitmap(Width, Height)
    95. Dim G As Graphics = Graphics.FromImage(B)
    96. With G
    97. Try : SelectedTab.BackColor = Color.White : Catch : End Try
    98. .Clear(Color.White)
    99. .FillRectangle(New SolidBrush(Color.FromArgb(39, 39, 39)), New Rectangle(0, 0, ItemSize.Height + 4, Height)) ' Background
    100. .DrawLine(New Pen(Color.FromArgb(39, 39, 39)), New Point(ItemSize.Height + 3, 0), New Point(ItemSize.Height + 3, 999)) ' Background border
    101. For TabIndex As Integer = 0 To TabCount - 1
    102. If TabIndex = SelectedIndex Then
    103. ' Selected tab area
    104. Dim TabRect As Rectangle = New Rectangle(New Point(GetTabRect(TabIndex).Location.X - 2, GetTabRect(TabIndex).Location.Y - 2), New Size(GetTabRect(TabIndex).Width + 3, GetTabRect(TabIndex).Height - 1))
    105. ' Background
    106. .FillRectangle(New SolidBrush(Color.FromArgb(28, 28, 28)), TabRect.X, TabRect.Y, TabRect.Width + 1, TabRect.Height + 3)
    107. ' Highlight
    108. If Not TabIndex = 0 Then .DrawLine(New Pen(Color.FromArgb(60, 60, 60)), New Point(TabRect.Location.X - 1, TabRect.Top + 1), New Point(TabRect.Width, TabRect.Top + 1))
    109. ' Selected marker
    110. Dim Selected As Rectangle = New Rectangle(New Point(GetTabRect(TabIndex).Width, GetTabRect(TabIndex).Location.Y - IIf(TabIndex = 0, 1, 0)), New Size(4, GetTabRect(TabIndex).Height - IIf(TabIndex = 0, 1, 2)))
    111. .FillRectangle(New SolidBrush(Color.Red), Selected)
    112. ' Icon & Text
    113. Dim SetIcon As Boolean = False
    114. Try
    115. If Not IsNothing(ImageList) Then
    116. Dim Index As Integer = TabPages(TabIndex).ImageIndex
    117. If Not Index = -1 Then
    118. ' Icon
    119. .DrawImage(ImageList.Images(TabPages(TabIndex).ImageIndex), New Point(TabRect.Location.X + 6, TabRect.Location.Y + 8))
    120. SetIcon = True
    121. End If
    122. End If
    123. Catch ex As Exception
    124. Finally
    125. ' Text
    126. .DrawString(IIf(SetIcon, IIf(TabPages(TabIndex).Text.Length >= 11, " ", String.Empty), String.Empty) & TabPages(TabIndex).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.White, TabRect, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
    127. End Try
    128. Else
    129. ' Deselected tab area
    130. Dim TabRect As Rectangle = New Rectangle(New Point(GetTabRect(TabIndex).Location.X - 2, GetTabRect(TabIndex).Location.Y - 2), New Size(GetTabRect(TabIndex).Width + 3, GetTabRect(TabIndex).Height + 1))
    131. If State.Hover AndAlso TabRect.Contains(State.Coordinates) Then
    132. ' Background
    133. .FillRectangle(New SolidBrush(Color.FromArgb(28, 28, 28)), TabRect.X, TabRect.Y, TabRect.Width + 1, TabRect.Height)
    134. Else
    135. ' Gradient
    136. Dim GradientInfo As New ColorBlend()
    137. With GradientInfo
    138. .Colors = {Color.FromArgb(51, 51, 51), Color.FromArgb(39, 39, 39), Color.FromArgb(37, 37, 37)}
    139. .Positions = {0.0F, 0.5F, 1.0F}
    140. End With
    141. Dim GradientArea As Rectangle = New Rectangle(New Point(GetTabRect(TabIndex).Location.X - 2, GetTabRect(TabIndex).Location.Y - 1), New Size(GetTabRect(TabIndex).Width + 4, GetTabRect(TabIndex).Height))
    142. Dim Gradient As New LinearGradientBrush(GradientArea, Color.Black, Color.Black, 90.0F)
    143. Gradient.InterpolationColors = GradientInfo
    144. .FillRectangle(Gradient, GradientArea)
    145. End If
    146. ' Hightlight
    147. .DrawLine(New Pen(Color.FromArgb(60, 60, 60)), New Point(TabRect.Location.X - 1, TabRect.Top + 1), New Point(TabRect.Width, TabRect.Top + 1))
    148. ' Shadow
    149. .DrawLine(New Pen(Color.FromArgb(27, 27, 27)), New Point(TabRect.Location.X - 1, TabRect.Bottom - 1), New Point(TabRect.Location.X + TabRect.Right, TabRect.Bottom - 1))
    150. If State.Hover AndAlso TabRect.Contains(State.Coordinates) Then
    151. ' Hover marker
    152. Dim Hover As Rectangle = New Rectangle(New Point(GetTabRect(TabIndex).Width, GetTabRect(TabIndex).Location.Y), New Size(4, GetTabRect(TabIndex).Height - 2))
    153. .FillRectangle(New SolidBrush(Color.FromArgb(102, 102, 102)), Hover)
    154. End If
    155. ' Icon & Text
    156. Dim SetIcon As Boolean = False
    157. Try
    158. If Not IsNothing(ImageList) Then
    159. Dim Index As Integer = TabPages(TabIndex).ImageIndex
    160. If Not Index = -1 Then
    161. ' Icon
    162. .DrawImage(ImageOpacity(ImageList.Images(TabPages(TabIndex).ImageIndex), InactiveIconOpacity), New Point(TabRect.Location.X + 6, TabRect.Location.Y + 8))
    163. SetIcon = True
    164. End If
    165. End If
    166. Catch ex As Exception
    167. Finally
    168. ' Text
    169. .DrawString(IIf(SetIcon, IIf(TabPages(TabIndex).Text.Length >= 11, " ", String.Empty), String.Empty) & TabPages(TabIndex).Text, Font, ToBrush(Color.FromArgb(153, 153, 153)), TabRect, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
    170. End Try
    171. End If
    172. Next
    173. e.Graphics.CompositingQuality = CompositingQuality.HighQuality
    174. e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
    175. e.Graphics.SmoothingMode = SmoothingMode.HighQuality
    176. e.Graphics.DrawImage(B.Clone, 0, 0)
    177. .Dispose() : B.Dispose()
    178. End With
    179. End Sub
    180. #End Region
    181. End Class
    182. End Namespace

    Quelle: u-hacks.net/net-94/gdi-tabcontrol-11434/
    Bilder
    • tab.png

      3,78 kB, 610×369, 326 mal angesehen
    Hab hier auch noch eines @iEi:
    Ich hoffe einmal ich hab den Code richtig gekürzt.
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Drawing.Drawing2D, System.ComponentModel, System.Windows.Forms
    2. ''' <summary>
    3. ''' Flat UI Theme
    4. ''' Creator: iSynthesis (HF)
    5. ''' Version: 1.0.4
    6. ''' Date Created: 17/06/2013
    7. ''' Date Changed: 26/06/2013
    8. ''' UID: 374648
    9. ''' For any bugs / errors, PM me.
    10. ''' </summary>
    11. Module Helpers
    12. #Region " Variables"
    13. Friend G As Graphics, B As Bitmap
    14. Friend _FlatColor As Color = Color.FromArgb(35, 168, 109)
    15. Friend NearSF As New StringFormat() With {.Alignment = StringAlignment.Near, .LineAlignment = StringAlignment.Near}
    16. Friend CenterSF As New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
    17. #End Region
    18. #Region " Functions"
    19. Public Function RoundRec(ByVal Rectangle As Rectangle, ByVal Curve As Integer) As GraphicsPath
    20. Dim P As GraphicsPath = New GraphicsPath()
    21. Dim ArcRectangleWidth As Integer = Curve * 2
    22. P.AddArc(New Rectangle(Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -180, 90)
    23. P.AddArc(New Rectangle(Rectangle.Width - ArcRectangleWidth + Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -90, 90)
    24. P.AddArc(New Rectangle(Rectangle.Width - ArcRectangleWidth + Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), 0, 90)
    25. P.AddArc(New Rectangle(Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), 90, 90)
    26. P.AddLine(New Point(Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y), New Point(Rectangle.X, Curve + Rectangle.Y))
    27. Return P
    28. End Function
    29. Public Function RoundRect(x!, y!, w!, h!, Optional r! = 0.3, Optional TL As Boolean = True, Optional TR As Boolean = True, Optional BR As Boolean = True, Optional BL As Boolean = True) As GraphicsPath
    30. Dim d! = Math.Min(w, h) * r, xw = x + w, yh = y + h
    31. RoundRect = New GraphicsPath
    32. With RoundRect
    33. If TL Then .AddArc(x, y, d, d, 180, 90) Else .AddLine(x, y, x, y)
    34. If TR Then .AddArc(xw - d, y, d, d, 270, 90) Else .AddLine(xw, y, xw, y)
    35. If BR Then .AddArc(xw - d, yh - d, d, d, 0, 90) Else .AddLine(xw, yh, xw, yh)
    36. If BL Then .AddArc(x, yh - d, d, d, 90, 90) Else .AddLine(x, yh, x, yh)
    37. .CloseFigure()
    38. End With
    39. End Function
    40. '-- Credit: AeonHack
    41. Public Function DrawArrow(x As Integer, y As Integer, flip As Boolean) As GraphicsPath
    42. Dim GP As New GraphicsPath()
    43. Dim W As Integer = 12
    44. Dim H As Integer = 6
    45. If flip Then
    46. GP.AddLine(x + 1, y, x + W + 1, y)
    47. GP.AddLine(x + W, y, x + H, y + H - 1)
    48. Else
    49. GP.AddLine(x, y + H, x + W, y + H)
    50. GP.AddLine(x + W, y + H, x + H, y)
    51. End If
    52. GP.CloseFigure()
    53. Return GP
    54. End Function
    55. #End Region
    56. End Module
    57. #Region " Mouse States"
    58. Enum MouseState As Byte
    59. None = 0
    60. Over = 1
    61. Down = 2
    62. Block = 3
    63. End Enum
    64. #End Region
    65. Class FlatTabControl : Inherits TabControl
    66. #Region " Variables"
    67. Private W, H As Integer
    68. #End Region
    69. #Region " Properties"
    70. Protected Overrides Sub CreateHandle()
    71. MyBase.CreateHandle()
    72. Alignment = TabAlignment.Top
    73. End Sub
    74. #Region " Colors"
    75. <Category("Colors")> _
    76. Public Property BaseColor As Color
    77. Get
    78. Return _BaseColor
    79. End Get
    80. Set(value As Color)
    81. _BaseColor = value
    82. End Set
    83. End Property
    84. <Category("Colors")> _
    85. Public Property ActiveColor As Color
    86. Get
    87. Return _ActiveColor
    88. End Get
    89. Set(value As Color)
    90. _ActiveColor = value
    91. End Set
    92. End Property
    93. #End Region
    94. #End Region
    95. #Region " Colors"
    96. Private BGColor As Color = Color.FromArgb(60, 70, 73)
    97. Private _BaseColor As Color = Color.FromArgb(45, 47, 49)
    98. Private _ActiveColor As Color = _FlatColor
    99. #End Region
    100. Sub New()
    101. SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or _
    102. ControlStyles.ResizeRedraw Or ControlStyles.OptimizedDoubleBuffer, True)
    103. DoubleBuffered = True
    104. BackColor = Color.FromArgb(60, 70, 73)
    105. Font = New Font("Segoe UI", 10)
    106. SizeMode = TabSizeMode.Fixed
    107. ItemSize = New Size(120, 40)
    108. End Sub
    109. Protected Overrides Sub OnPaint(e As PaintEventArgs)
    110. B = New Bitmap(Width, Height) : G = Graphics.FromImage(B)
    111. W = Width - 1 : H = Height - 1
    112. With G
    113. .SmoothingMode = 2
    114. .PixelOffsetMode = 2
    115. .TextRenderingHint = 5
    116. .Clear(_BaseColor)
    117. Try : SelectedTab.BackColor = BGColor : Catch : End Try
    118. For i = 0 To TabCount - 1
    119. Dim Base As New Rectangle(New Point(GetTabRect(i).Location.X + 2, GetTabRect(i).Location.Y), New Size(GetTabRect(i).Width, GetTabRect(i).Height))
    120. Dim BaseSize As New Rectangle(Base.Location, New Size(Base.Width, Base.Height))
    121. If i = SelectedIndex Then
    122. '-- Base
    123. .FillRectangle(New SolidBrush(_BaseColor), BaseSize)
    124. '-- Gradiant
    125. '.fill
    126. .FillRectangle(New SolidBrush(_ActiveColor), BaseSize)
    127. '-- ImageList
    128. If ImageList IsNot Nothing Then
    129. Try
    130. If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
    131. '-- Image
    132. .DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(BaseSize.Location.X + 8, BaseSize.Location.Y + 6))
    133. '-- Text
    134. .DrawString(" " & TabPages(i).Text, Font, Brushes.White, BaseSize, CenterSF)
    135. Else
    136. '-- Text
    137. .DrawString(TabPages(i).Text, Font, Brushes.White, BaseSize, CenterSF)
    138. End If
    139. Catch ex As Exception
    140. Throw New Exception(ex.Message)
    141. End Try
    142. Else
    143. '-- Text
    144. .DrawString(TabPages(i).Text, Font, Brushes.White, BaseSize, CenterSF)
    145. End If
    146. Else
    147. '-- Base
    148. .FillRectangle(New SolidBrush(_BaseColor), BaseSize)
    149. '-- ImageList
    150. If ImageList IsNot Nothing Then
    151. Try
    152. If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
    153. '-- Image
    154. .DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(BaseSize.Location.X + 8, BaseSize.Location.Y + 6))
    155. '-- Text
    156. .DrawString(" " & TabPages(i).Text, Font, New SolidBrush(Color.White), BaseSize, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
    157. Else
    158. '-- Text
    159. .DrawString(TabPages(i).Text, Font, New SolidBrush(Color.White), BaseSize, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
    160. End If
    161. Catch ex As Exception
    162. Throw New Exception(ex.Message)
    163. End Try
    164. Else
    165. '-- Text
    166. .DrawString(TabPages(i).Text, Font, New SolidBrush(Color.White), BaseSize, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
    167. End If
    168. End If
    169. Next
    170. End With
    171. MyBase.OnPaint(e)
    172. G.Dispose()
    173. e.Graphics.InterpolationMode = 7
    174. e.Graphics.DrawImageUnscaled(B, 0, 0)
    175. B.Dispose()
    176. End Sub
    177. End Class


    Quelle: hackforums.net/showthread.php?tid=3559385
    Bilder
    • tc.PNG

      1,52 kB, 695×55, 294 mal angesehen