TabPage.vb
上传用户:szledliu
上传日期:2021-01-29
资源大小:13805k
文件大小:33k
源码类别:

C#编程

开发平台:

C#

  1. Imports System.ComponentModel
  2. Imports System.Drawing.Drawing2D
  3. <DesignTimeVisible(False), Description("Represents a single tab page in a MdiTabControl.TabControl.")> _
  4. Public Class TabPage
  5.     Private m_BackHighColor As Color
  6.     Private m_BackHighColorDisabled As Color
  7.     Private m_BackLowColor As Color
  8.     Private m_BackLowColorDisabled As Color
  9.     Private m_BorderColor As Color
  10.     Private m_BorderColorDisabled As Color
  11.     Private m_ForeColorDisabled As Color
  12.     Private m_Selected As Boolean = False
  13.     Private m_Hot As Boolean = False
  14.     Private m_MaximumWidth As Integer
  15.     Private m_MinimumWidth As Integer
  16.     Private m_PadLeft As Integer
  17.     Private m_PadRight As Integer
  18.     Private m_CloseButtonVisible As Boolean
  19.     Private m_CloseButton As Image
  20.     Private m_CloseButtonImageHot As Image
  21.     Private m_CloseButtonImageDisabled As Image
  22.     Private m_CloseButtonBackHighColor As Color
  23.     Private m_CloseButtonBackLowColor As Color
  24.     Private m_CloseButtonBorderColor As Color
  25.     Private m_CloseButtonForeColor As Color
  26.     Private m_CloseButtonBackHighColorDisabled As Color
  27.     Private m_CloseButtonBackLowColorDisabled As Color
  28.     Private m_CloseButtonBorderColorDisabled As Color
  29.     Private m_CloseButtonForeColorDisabled As Color
  30.     Private m_CloseButtonBackHighColorHot As Color
  31.     Private m_CloseButtonBackLowColorHot As Color
  32.     Private m_CloseButtonBorderColorHot As Color
  33.     Private m_CloseButtonForeColorHot As Color
  34.     Private m_HotTrack As Boolean
  35.     Private m_CloseButtonSize As Size
  36.     Private m_FontBoldOnSelect As Boolean
  37.     Private m_IconSize As Size
  38.     Private m_SmoothingMode As SmoothingMode
  39.     Private m_Alignment As TabControl.TabAlignment
  40.     Private m_GlassGradient As Boolean
  41.     Private m_BorderEnhanced As Boolean
  42.     Private m_RenderMode As ToolStripRenderMode
  43.     Private m_BorderEnhanceWeight As TabControl.Weight
  44.     Friend WithEvents m_Form As Form
  45.     Friend TabVisible As Boolean
  46.     Friend TabLeft As Integer
  47.     Friend MenuItem As New ToolStripMenuItem
  48.     Private MouseOverCloseButton As Boolean = False
  49.     <Description("Occurs when the user clicks the Tab Control.")> _
  50.     Public Shadows Event Click(ByVal sender As Object, ByVal e As EventArgs)
  51.     Friend Event Close(ByVal sender As Object, ByVal e As EventArgs)
  52.     Friend Event GetTabRegion(ByVal sender As Object, ByVal e As TabControl.GetTabRegionEventArgs)
  53.     Friend Event TabPaintBackground(ByVal sender As Object, ByVal e As TabControl.TabPaintEventArgs)
  54.     Friend Event TabPaintBorder(ByVal sender As Object, ByVal e As TabControl.TabPaintEventArgs)
  55.     Friend Event Draging(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
  56.     Friend Event EnterForm As EventHandler
  57.     Friend Event LeaveForm As EventHandler
  58.     Sub New(ByVal Form As System.Windows.Forms.Form)
  59.         ' This call is required by the Windows Form Designer.
  60.         InitializeComponent()
  61.         ' Add any initialization after the InitializeComponent() call.
  62.         Me.SuspendLayout()
  63.         Me.SetStyle(ControlStyles.DoubleBuffer, True)
  64.         Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
  65.         Me.SetStyle(ControlStyles.UserPaint, True)
  66.         Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
  67.         MyBase.BackColor = Color.Transparent
  68.         Me.Visible = False
  69.         Me.Size = New System.Drawing.Size(1, 1)
  70.         Form.TopLevel = False
  71.         Form.MdiParent = Nothing
  72.         Form.FormBorderStyle = Windows.Forms.FormBorderStyle.None
  73.         Form.Dock = DockStyle.Fill
  74.         Me.m_Form = Form
  75.         MenuItem.Text = Form.Text
  76.         MenuItem.Image = Form.Icon.ToBitmap
  77.         MenuItem.Tag = Me
  78.         Me.ResumeLayout(False)
  79.     End Sub
  80.     <Description("Gets the form associated with the tab page")> _
  81.     Public ReadOnly Property Form() As Object
  82.         Get
  83.             Return m_Form
  84.         End Get
  85.     End Property
  86.     <Description("Gets or sets the System.Drawing.Color structure that represents the starting color of the Background linear gradient for the tab.")> _
  87.     Public Property BackHighColor() As Color
  88.         Get
  89.             Return m_BackHighColor
  90.         End Get
  91.         Set(ByVal value As Color)
  92.             m_BackHighColor = value
  93.             Invalidate()
  94.         End Set
  95.     End Property
  96.     <Description("Gets or sets the System.Drawing.Color structure that represents the ending color of the Background linear gradient for the tab.")> _
  97.     Public Property BackLowColor() As Color
  98.         Get
  99.             Return m_BackLowColor
  100.         End Get
  101.         Set(ByVal value As Color)
  102.             m_BackLowColor = value
  103.             Invalidate()
  104.         End Set
  105.     End Property
  106.     <Description("Gets or sets the System.Drawing.Color structure that represents the border color.")> _
  107.     Friend Property BorderColor() As Color
  108.         Get
  109.             Return m_BorderColor
  110.         End Get
  111.         Set(ByVal value As Color)
  112.             m_BorderColor = value
  113.             Invalidate()
  114.         End Set
  115.     End Property
  116.     <Description("Gets or sets the System.Drawing.Color structure that represents the starting color of the Background linear gradient for a non selected tab.")> _
  117.     Public Property BackHighColorDisabled() As Color
  118.         Get
  119.             Return m_BackHighColorDisabled
  120.         End Get
  121.         Set(ByVal value As Color)
  122.             m_BackHighColorDisabled = value
  123.             Invalidate()
  124.         End Set
  125.     End Property
  126.     <Description("Gets or sets the System.Drawing.Color structure that represents the ending color of the Background linear gradient for a non selected tab.")> _
  127.     Public Property BackLowColorDisabled() As Color
  128.         Get
  129.             Return m_BackLowColorDisabled
  130.         End Get
  131.         Set(ByVal value As Color)
  132.             m_BackLowColorDisabled = value
  133.             Invalidate()
  134.         End Set
  135.     End Property
  136.     <Description("Gets or sets the System.Drawing.Color structure that represents the border color of the tab when not selected.")> _
  137.     Public Property BorderColorDisabled() As Color
  138.         Get
  139.             Return m_BorderColorDisabled
  140.         End Get
  141.         Set(ByVal value As Color)
  142.             m_BorderColorDisabled = value
  143.             Invalidate()
  144.         End Set
  145.     End Property
  146.     <Description("Gets or sets the System.Drawing.Color structure that represents the fore color of the tab when not selected.")> _
  147.     Public Property ForeColorDisabled() As Color
  148.         Get
  149.             Return m_ForeColorDisabled
  150.         End Get
  151.         Set(ByVal value As Color)
  152.             m_ForeColorDisabled = value
  153.             Invalidate()
  154.         End Set
  155.     End Property
  156.     Friend Property IsSelected() As Boolean
  157.         Get
  158.             Return m_Selected
  159.         End Get
  160.         Set(ByVal Value As Boolean)
  161.             If m_Selected <> Value Then
  162.                 m_Selected = Value
  163.                 If m_Selected Then
  164.                     m_Hot = False
  165.                 End If
  166.                 Invalidate()
  167.             End If
  168.         End Set
  169.     End Property
  170.     <Description("Returns whether the tab is selected or not.")> _
  171.     Public ReadOnly Property Selected() As Boolean
  172.         Get
  173.             Return IsSelected
  174.         End Get
  175.     End Property
  176.     Friend Property MaximumWidth() As Integer
  177.         Get
  178.             Return m_MaximumWidth
  179.         End Get
  180.         Set(ByVal value As Integer)
  181.             m_MaximumWidth = value
  182.             CalculateWidth()
  183.             Invalidate()
  184.         End Set
  185.     End Property
  186.     Friend Property MinimumWidth() As Integer
  187.         Get
  188.             Return m_MinimumWidth
  189.         End Get
  190.         Set(ByVal value As Integer)
  191.             m_MinimumWidth = value
  192.             CalculateWidth()
  193.             Invalidate()
  194.         End Set
  195.     End Property
  196.     Friend Property PadLeft() As Integer
  197.         Get
  198.             Return m_PadLeft
  199.         End Get
  200.         Set(ByVal value As Integer)
  201.             m_PadLeft = value
  202.             CalculateWidth()
  203.             Invalidate()
  204.         End Set
  205.     End Property
  206.     Friend Property PadRight() As Integer
  207.         Get
  208.             Return m_PadRight
  209.         End Get
  210.         Set(ByVal value As Integer)
  211.             m_PadRight = value
  212.             CalculateWidth()
  213.             Invalidate()
  214.         End Set
  215.     End Property
  216.     <Description("Gets or sets whether the tab close button is visble or not.")> _
  217.     Public Property CloseButtonVisible() As Boolean
  218.         Get
  219.             Return m_CloseButtonVisible
  220.         End Get
  221.         Set(ByVal value As Boolean)
  222.             If m_CloseButtonVisible <> value Then
  223.                 m_CloseButtonVisible = value
  224.                 CalculateWidth()
  225.                 Invalidate()
  226.             End If
  227.         End Set
  228.     End Property
  229.     Public Property CloseButtonImage() As Image
  230.         Get
  231.             Return m_CloseButton
  232.         End Get
  233.         Set(ByVal value As Image)
  234.             m_CloseButton = value
  235.             Invalidate()
  236.         End Set
  237.     End Property
  238.     Public Property CloseButtonImageHot() As Image
  239.         Get
  240.             Return m_CloseButtonImageHot
  241.         End Get
  242.         Set(ByVal value As Image)
  243.             m_CloseButtonImageHot = value
  244.             Invalidate()
  245.         End Set
  246.     End Property
  247.     Public Property CloseButtonImageDisabled() As Image
  248.         Get
  249.             Return m_CloseButtonImageDisabled
  250.         End Get
  251.         Set(ByVal value As Image)
  252.             m_CloseButtonImageDisabled = value
  253.             Invalidate()
  254.         End Set
  255.     End Property
  256.     Public Property CloseButtonBackHighColor() As System.Drawing.Color
  257.         Get
  258.             Return m_CloseButtonBackHighColor
  259.         End Get
  260.         Set(ByVal Value As Color)
  261.             m_CloseButtonBackHighColor = Value
  262.         End Set
  263.     End Property
  264.     Public Property CloseButtonBackLowColor() As System.Drawing.Color
  265.         Get
  266.             Return m_CloseButtonBackLowColor
  267.         End Get
  268.         Set(ByVal Value As Color)
  269.             m_CloseButtonBackLowColor = Value
  270.         End Set
  271.     End Property
  272.     Public Property CloseButtonBorderColor() As System.Drawing.Color
  273.         Get
  274.             Return m_CloseButtonBorderColor
  275.         End Get
  276.         Set(ByVal Value As Color)
  277.             m_CloseButtonBorderColor = Value
  278.         End Set
  279.     End Property
  280.     Public Property CloseButtonForeColor() As System.Drawing.Color
  281.         Get
  282.             Return m_CloseButtonForeColor
  283.         End Get
  284.         Set(ByVal Value As Color)
  285.             m_CloseButtonForeColor = Value
  286.         End Set
  287.     End Property
  288.     Public Property CloseButtonBackHighColorDisabled() As System.Drawing.Color
  289.         Get
  290.             Return m_CloseButtonBackHighColorDisabled
  291.         End Get
  292.         Set(ByVal Value As Color)
  293.             m_CloseButtonBackHighColorDisabled = Value
  294.         End Set
  295.     End Property
  296.     Public Property CloseButtonBackLowColorDisabled() As System.Drawing.Color
  297.         Get
  298.             Return m_CloseButtonBackLowColorDisabled
  299.         End Get
  300.         Set(ByVal Value As Color)
  301.             m_CloseButtonBackLowColorDisabled = Value
  302.         End Set
  303.     End Property
  304.     Public Property CloseButtonBorderColorDisabled() As System.Drawing.Color
  305.         Get
  306.             Return m_CloseButtonBorderColorDisabled
  307.         End Get
  308.         Set(ByVal Value As Color)
  309.             m_CloseButtonBorderColorDisabled = Value
  310.         End Set
  311.     End Property
  312.     Public Property CloseButtonForeColorDisabled() As System.Drawing.Color
  313.         Get
  314.             Return m_CloseButtonForeColorDisabled
  315.         End Get
  316.         Set(ByVal Value As Color)
  317.             m_CloseButtonForeColorDisabled = Value
  318.         End Set
  319.     End Property
  320.     Public Property CloseButtonBackHighColorHot() As System.Drawing.Color
  321.         Get
  322.             Return m_CloseButtonBackHighColorHot
  323.         End Get
  324.         Set(ByVal Value As Color)
  325.             m_CloseButtonBackHighColorHot = Value
  326.         End Set
  327.     End Property
  328.     Public Property CloseButtonBackLowColorHot() As System.Drawing.Color
  329.         Get
  330.             Return m_CloseButtonBackLowColorHot
  331.         End Get
  332.         Set(ByVal Value As Color)
  333.             m_CloseButtonBackLowColorHot = Value
  334.         End Set
  335.     End Property
  336.     Public Property CloseButtonBorderColorHot() As System.Drawing.Color
  337.         Get
  338.             Return m_CloseButtonBorderColorHot
  339.         End Get
  340.         Set(ByVal Value As Color)
  341.             m_CloseButtonBorderColorHot = Value
  342.         End Set
  343.     End Property
  344.     Public Property CloseButtonForeColorHot() As System.Drawing.Color
  345.         Get
  346.             Return m_CloseButtonForeColorHot
  347.         End Get
  348.         Set(ByVal Value As Color)
  349.             m_CloseButtonForeColorHot = Value
  350.         End Set
  351.     End Property
  352.     Friend Property HotTrack() As Boolean
  353.         Get
  354.             Return m_HotTrack
  355.         End Get
  356.         Set(ByVal value As Boolean)
  357.             m_HotTrack = value
  358.             Invalidate()
  359.         End Set
  360.     End Property
  361.     Friend Property CloseButtonSize() As Size
  362.         Get
  363.             Return m_CloseButtonSize
  364.         End Get
  365.         Set(ByVal value As Size)
  366.             m_CloseButtonSize = value
  367.             CalculateWidth()
  368.             Invalidate()
  369.         End Set
  370.     End Property
  371.     Friend Property FontBoldOnSelect() As Boolean
  372.         Get
  373.             Return m_FontBoldOnSelect
  374.         End Get
  375.         Set(ByVal value As Boolean)
  376.             m_FontBoldOnSelect = value
  377.             CalculateWidth()
  378.             Invalidate()
  379.         End Set
  380.     End Property
  381.     Friend Property IconSize() As Size
  382.         Get
  383.             Return m_IconSize
  384.         End Get
  385.         Set(ByVal value As Size)
  386.             m_IconSize = value
  387.             CalculateWidth()
  388.             Invalidate()
  389.         End Set
  390.     End Property
  391.     Friend Property SmoothingMode() As SmoothingMode
  392.         Get
  393.             Return m_SmoothingMode
  394.         End Get
  395.         Set(ByVal value As SmoothingMode)
  396.             m_SmoothingMode = value
  397.             Invalidate()
  398.         End Set
  399.     End Property
  400.     Friend Property Alignment() As TabControl.TabAlignment
  401.         Get
  402.             Return m_Alignment
  403.         End Get
  404.         Set(ByVal value As TabControl.TabAlignment)
  405.             m_Alignment = value
  406.             Invalidate()
  407.         End Set
  408.     End Property
  409.     Friend Property GlassGradient() As Boolean
  410.         Get
  411.             Return m_GlassGradient
  412.         End Get
  413.         Set(ByVal value As Boolean)
  414.             m_GlassGradient = value
  415.         End Set
  416.     End Property
  417.     Friend Property BorderEnhanced() As Boolean
  418.         Get
  419.             Return m_BorderEnhanced
  420.         End Get
  421.         Set(ByVal value As Boolean)
  422.             m_BorderEnhanced = value
  423.         End Set
  424.     End Property
  425.     Friend Property RenderMode() As ToolStripRenderMode
  426.         Get
  427.             Return m_RenderMode
  428.         End Get
  429.         Set(ByVal value As ToolStripRenderMode)
  430.             m_RenderMode = value
  431.             Invalidate()
  432.         End Set
  433.     End Property
  434.     Friend Property BorderEnhanceWeight() As TabControl.Weight
  435.         Get
  436.             Return m_BorderEnhanceWeight
  437.         End Get
  438.         Set(ByVal value As TabControl.Weight)
  439.             m_BorderEnhanceWeight = value
  440.         End Set
  441.     End Property
  442.     Public Property Icon() As Icon
  443.         Get
  444.             Return m_Form.Icon
  445.         End Get
  446.         Set(ByVal value As Icon)
  447.             m_Form.Icon = value
  448.             Dim r As New Region(New Rectangle(PadLeft, (Me.Height / 2 - m_IconSize.Height / 2), m_IconSize.Width, m_IconSize.Height))
  449.             Me.Invalidate(r)
  450.             r.Dispose()
  451.             r = Nothing
  452.             MenuItem.Image = value.ToBitmap
  453.         End Set
  454.     End Property
  455.     <Description("Selects the TabPage.")> _
  456.     Public Shadows Sub [Select]()
  457.         If Not IsSelected Then
  458.             RaiseEvent Click(Me, New EventArgs)
  459.         End If
  460.     End Sub
  461.     Private Function CreateGradientBrush(ByVal Rectangle As Rectangle, ByVal Color1 As Color, ByVal Color2 As Color) As Drawing2D.LinearGradientBrush
  462.         If m_GlassGradient Then
  463.             Return Helper.CreateGlassGradientBrush(Rectangle, Color1, Color2)
  464.         Else
  465.             Return New Drawing2D.LinearGradientBrush(Rectangle, Color1, Color2, Drawing2D.LinearGradientMode.Vertical)
  466.         End If
  467.     End Function
  468.     Private Sub TabContents_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Form.Enter
  469.         RaiseEvent EnterForm(Me, e)
  470.     End Sub
  471.     Private Sub TabContent_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles m_Form.FormClosed
  472.         ' if the form is closed closes the tabpage
  473.         RaiseEvent Close(Me, New EventArgs)
  474.     End Sub
  475.     Private Sub m_Form_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Form.Leave
  476.         RaiseEvent LeaveForm(Me, e)
  477.     End Sub
  478.     Private Sub TabContent_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Form.TextChanged
  479.         CalculateWidth()
  480.         Invalidate()
  481.         MenuItem.Text = m_Form.Text
  482.     End Sub
  483.     Private Sub Tab_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
  484.         If m_Selected And Not (MouseOverCloseButton And m_CloseButtonVisible) Then Exit Sub
  485.         If e.Button = Windows.Forms.MouseButtons.Left Then
  486.             ' Close button was clicked
  487.             If MouseOverCloseButton And m_CloseButtonVisible Then
  488.                 ' try to close the form
  489.                 m_Form.Close()
  490.             Else ' tab was clicked
  491.                 ' select the tab
  492.                 [Select]()
  493.             End If
  494.         End If
  495.     End Sub
  496.     Private Sub Tab_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
  497.         If m_Selected Then Exit Sub
  498.         If m_HotTrack Then m_Hot = True
  499.         Invalidate()
  500.     End Sub
  501.     Private Sub Tab_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
  502.         MouseOverCloseButton = False
  503.         m_Hot = False
  504.         Invalidate()
  505.     End Sub
  506.     Private Sub Tab_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
  507.         Static State As Boolean = False
  508.         If m_CloseButtonVisible Then
  509.             ' verify if the mouse is over the close button
  510.             Dim x As Integer = Me.Width - PadRight - m_CloseButtonSize.Width - 2
  511.             Dim y As Integer = Me.Height / 2 - m_CloseButtonSize.Height / 2
  512.             MouseOverCloseButton = e.X >= x And e.X <= x + m_CloseButtonSize.Width - 1 And e.Y >= y And e.Y <= y + m_CloseButtonSize.Height - 1
  513.             If State <> MouseOverCloseButton And m_CloseButtonVisible Then
  514.                 State = MouseOverCloseButton
  515.                 Dim r As New Region(New Rectangle(x, y, m_CloseButtonSize.Width, m_CloseButtonSize.Height))
  516.                 Me.Invalidate(r)
  517.                 r.Dispose()
  518.                 r = Nothing
  519.             End If
  520.         End If
  521.         If Me.RectangleToScreen(Me.ClientRectangle).Contains(Me.PointToScreen(New Point(e.X, e.Y))) Then
  522.             Cursor = Cursors.Default
  523.         Else ' the mouse is outside the tab (it happens only when the mouse was pressed on the tab and moved away while pressed)
  524.             RaiseEvent Draging(Me, e)
  525.             Cursor = Cursors.No
  526.         End If
  527.     End Sub
  528.     ' Draws the tab text (the form text)
  529.     Private Sub DrawText(ByVal g As Graphics)
  530.         Dim f As Font = New Font(Font, IIf(m_Selected And m_FontBoldOnSelect, FontStyle.Bold, FontStyle.Regular))
  531.         Dim b As Brush = New SolidBrush(IIf(m_Selected Or m_Hot, ForeColor, m_ForeColorDisabled))
  532.         Dim bounds As RectangleF = New RectangleF(PadLeft + IIf(m_Form.Icon Is Nothing, 0, m_IconSize.Width) + 2, 1, Width - PadLeft - IIf(m_Form.Icon Is Nothing, 0, m_IconSize.Height) - 5 - IIf(m_CloseButtonVisible, m_CloseButtonSize.Width, 0) - PadRight, Me.DisplayRectangle.Height)
  533.         Dim MyFormat As StringFormat = New StringFormat
  534.         MyFormat.FormatFlags = StringFormatFlags.NoWrap
  535.         MyFormat.LineAlignment = StringAlignment.Center
  536.         MyFormat.Trimming = StringTrimming.EllipsisCharacter
  537.         g.DrawString(m_Form.Text, f, b, bounds, MyFormat)
  538.         MyFormat.Dispose()
  539.         b.Dispose()
  540.         f.Dispose()
  541.         MyFormat = Nothing
  542.         b = Nothing
  543.         f = Nothing
  544.     End Sub
  545.     ' Draws the tab icon if exists (the form icon)
  546.     Private Sub DrawIcon(ByVal g As Graphics)
  547.         Try
  548.             If m_Form.Icon Is Nothing Then Exit Sub
  549.             Dim r As Rectangle = New Rectangle(PadLeft, (Me.Height / 2 - m_IconSize.Height / 2), m_IconSize.Width, m_IconSize.Height)
  550.             Dim i As Icon = New Icon(m_Form.Icon, m_IconSize)
  551.             g.DrawIcon(i, r)
  552.             DestroyIcon(i.Handle)
  553.             i.Dispose()
  554.             i = Nothing
  555.         Catch ex As Exception
  556.         End Try
  557.     End Sub
  558.     <System.Runtime.InteropServices.DllImportAttribute("user32.dll")> _
  559.     Private Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
  560.     End Function
  561.     ' Draws the Close Button
  562.     Private Sub DrawCloseButton(ByVal g As Graphics)
  563.         Try
  564.             Dim I As Bitmap
  565.             Dim x As Integer = Me.Width - (m_CloseButtonSize.Width + PadRight + 2)
  566.             Dim y As Integer = Me.Height / 2 - m_CloseButtonSize.Height / 2
  567.             If MouseOverCloseButton Then
  568.                 I = m_CloseButtonImageHot
  569.             ElseIf m_Selected Then
  570.                 I = m_CloseButton
  571.             Else
  572.                 I = m_CloseButtonImageDisabled
  573.             End If
  574.             Dim IsDisposable As Boolean = False
  575.             If I Is Nothing Then
  576.                 I = GetButton()
  577.                 IsDisposable = True
  578.             End If
  579.             Dim icon As Icon = Drawing.Icon.FromHandle(I.GetHicon)
  580.             Dim r As Rectangle = New Rectangle(x, y, m_CloseButtonSize.Width, m_CloseButtonSize.Height)
  581.             g.DrawIcon(icon, r)
  582.             If IsDisposable Then
  583.                 I.Dispose()
  584.                 I = Nothing
  585.             End If
  586.             DestroyIcon(icon.Handle)
  587.             icon.Dispose()
  588.             icon = Nothing
  589.         Catch ex As Exception
  590.         End Try
  591.     End Sub
  592.     ' Generates the close button image
  593.     Private Function GetButton() As Bitmap
  594.         Dim Points() As System.Drawing.Point = {New Point(1, 0), New Point(3, 0), New Point(5, 2), New Point(7, 0), New Point(9, 0), New Point(6, 3), New Point(6, 4), New Point(9, 7), New Point(7, 7), New Point(5, 5), New Point(3, 7), New Point(1, 7), New Point(4, 4), New Point(4, 3)}
  595.         Dim gp As New Drawing2D.GraphicsPath
  596.         Dim bch As Color
  597.         Dim bcl As Color
  598.         Dim bc As Color
  599.         Dim fc As Color
  600.         Dim B As Bitmap
  601.         Dim m As New Drawing2D.Matrix
  602.         Dim path() As System.Drawing.Point = {New Point(0, 1), New Point(1, 0), New Point(15, 0), New Point(16, 1), New Point(16, 14), New Point(15, 15), New Point(1, 15), New Point(0, 14)}
  603.         Dim g As Graphics
  604.         If MouseOverCloseButton Then
  605.             bch = RenderColors.TabCloseButtonBackHighColorHot(m_RenderMode, CloseButtonBackHighColorHot)
  606.             bcl = RenderColors.TabCloseButtonBackLowColorHot(m_RenderMode, CloseButtonBackLowColorHot)
  607.             bc = RenderColors.TabCloseButtonBorderColorHot(m_RenderMode, CloseButtonBorderColorHot)
  608.             fc = RenderColors.TabCloseButtonForeColorHot(m_RenderMode, CloseButtonForeColorHot)
  609.         ElseIf m_Selected Then
  610.             bch = RenderColors.TabCloseButtonBackHighColor(m_RenderMode, CloseButtonBackHighColor)
  611.             bcl = RenderColors.TabCloseButtonBackLowColor(m_RenderMode, CloseButtonBackLowColor)
  612.             bc = RenderColors.TabCloseButtonBorderColor(m_RenderMode, CloseButtonBorderColor)
  613.             fc = RenderColors.TabCloseButtonForeColor(m_RenderMode, CloseButtonForeColor)
  614.         Else
  615.             bch = RenderColors.TabCloseButtonBackHighColorDisabled(m_RenderMode, CloseButtonBackHighColorDisabled)
  616.             bcl = RenderColors.TabCloseButtonBackLowColorDisabled(m_RenderMode, CloseButtonBackLowColorDisabled)
  617.             bc = RenderColors.TabCloseButtonBorderColorDisabled(m_RenderMode, CloseButtonBorderColorDisabled)
  618.             fc = RenderColors.TabCloseButtonForeColorDisabled(m_RenderMode, CloseButtonForeColorDisabled)
  619.         End If
  620.         B = New Bitmap(17, 17)
  621.         B.MakeTransparent()
  622.         g = Graphics.FromImage(B)
  623.         ' draw the border and background
  624.         g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
  625.         Dim l As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, 15), bch, bcl)
  626.         g.FillPolygon(l, path)
  627.         Dim p As New Pen(bc)
  628.         g.DrawPolygon(p, path)
  629.         g.SmoothingMode = Drawing2D.SmoothingMode.Default
  630.         ' draw the foreground
  631.         gp.AddPolygon(Points)
  632.         m.Translate(3, 4)
  633.         gp.Transform(m)
  634.         p.Dispose()
  635.         p = New Pen(fc)
  636.         g.DrawPolygon(p, gp.PathPoints)
  637.         Dim sb As New SolidBrush(fc)
  638.         g.FillPolygon(sb, gp.PathPoints)
  639.         sb.Dispose()
  640.         p.Dispose()
  641.         gp.Dispose()
  642.         g.Dispose()
  643.         m.Dispose()
  644.         Return B
  645.     End Function
  646.     ' Calculates the tab width
  647.     Private Sub CalculateWidth()
  648.         Dim g As Graphics = Me.CreateGraphics()
  649.         Dim iw As Integer = 0
  650.         Dim cbw As Integer = 0
  651.         Dim w As Integer = Width
  652.         If m_Form.Icon IsNot Nothing Then iw = m_IconSize.Width
  653.         If m_CloseButtonVisible Then cbw = m_CloseButtonSize.Width
  654.         Dim f As New Font(Font, IIf(m_FontBoldOnSelect, FontStyle.Bold, FontStyle.Regular))
  655.         w = PadLeft + iw + 3 + g.MeasureString(m_Form.Text, f).Width + 3 + cbw + m_PadRight + 2
  656.         f.Dispose()
  657.         If w < m_MinimumWidth + 1 Then
  658.             w = m_MinimumWidth + 1
  659.         ElseIf w > m_MaximumWidth + 1 Then
  660.             w = m_MaximumWidth + 1
  661.         End If
  662.         If w <> Width Then
  663.             Width = w
  664.         End If
  665.         g.Dispose()
  666.     End Sub
  667.     ' Get the tab region shape
  668.     Private Function GetRegion(ByVal W As Integer, ByVal H As Integer, ByVal H1 As Integer) As Point()
  669.         Dim R() As Point = {New Point(0, H), New Point(0, 2), New Point(2, 0), New Point(W - 3, 0), New Point(W - 1, 2), New Point(W - 1, H)}
  670.         Dim e As New TabControl.GetTabRegionEventArgs(R, W, H, Me.IsSelected)
  671.         RaiseEvent GetTabRegion(Me, e)
  672.         Array.Resize(e.Points, e.Points.Length + 2)
  673.         Array.Copy(e.Points, 0, e.Points, 1, e.Points.Length - 1)
  674.         e.Points(0) = New Point(e.Points(1).X, H1)
  675.         e.Points(e.Points.Length - 1) = New Point(e.Points(e.Points.Length - 2).X, H1)
  676.         Return e.Points
  677.     End Function
  678.     Private Sub MirrorPath(ByVal GraphicPath As Drawing2D.GraphicsPath)
  679.         Dim m As New Matrix
  680.         m.Translate(0, Height - 1)
  681.         m.Scale(1, -1)
  682.         GraphicPath.Transform(m)
  683.         m.Dispose()
  684.     End Sub
  685.     ' Paint the tab
  686.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  687.         Dim Painting As Boolean = False
  688.         If Painting Then Exit Sub
  689.         Painting = True
  690.         Me.SuspendLayout()
  691.         Dim RenderBorderColor As Color
  692.         Dim RenderBottomColor As Color
  693.         Dim RenderHighColor As Color
  694.         Dim RenderLowColor As Color
  695.         Dim GraphicPath As New Drawing2D.GraphicsPath
  696.         Dim w As Integer = Me.Width
  697.         CalculateWidth()
  698.         If w <> Me.Width Then
  699.             GraphicPath.Dispose()
  700.             Exit Sub
  701.         End If
  702.         If m_Selected Then
  703.             RenderBorderColor = RenderColors.BorderColor(m_RenderMode, BorderColor)
  704.             RenderHighColor = RenderColors.TabBackHighColor(m_RenderMode, BackHighColor)
  705.             RenderLowColor = RenderColors.TabBackLowColor(m_RenderMode, BackLowColor)
  706.             RenderBottomColor = RenderColors.TabBackLowColor(m_RenderMode, BackLowColor)
  707.         ElseIf m_Hot Then
  708.             RenderBorderColor = RenderColors.BorderColor(m_RenderMode, BorderColor)
  709.             RenderHighColor = RenderColors.TabBackHighColor(m_RenderMode, BackHighColor)
  710.             RenderLowColor = RenderColors.TabBackLowColor(m_RenderMode, BackLowColor)
  711.             RenderBottomColor = RenderColors.BorderColor(m_RenderMode, BorderColor)
  712.         Else
  713.             RenderBorderColor = RenderColors.BorderColorDisabled(m_RenderMode, BorderColorDisabled)
  714.             RenderHighColor = RenderColors.TabBackHighColorDisabled(m_RenderMode, BackHighColorDisabled)
  715.             RenderLowColor = RenderColors.TabBackLowColorDisabled(m_RenderMode, BackLowColorDisabled)
  716.             RenderBottomColor = RenderColors.BorderColor(m_RenderMode, BorderColor)
  717.         End If
  718.         e.Graphics.SmoothingMode = m_SmoothingMode
  719.         GraphicPath.AddPolygon(GetRegion(Width - 1, Height - 1, IIf(Me.IsSelected, Height, Height - 1)))
  720.         ' if is bottom mirror the button vertically
  721.         If m_Alignment = TabControl.TabAlignment.Bottom Then
  722.             MirrorPath(GraphicPath)
  723.             Dim x As Color = RenderHighColor
  724.             RenderHighColor = RenderLowColor
  725.             RenderLowColor = x
  726.         End If
  727.         ' Get the correct region including all the borders
  728.         Dim R As Region = New Region(GraphicPath)
  729.         Dim R1 As Region = New Region(GraphicPath)
  730.         Dim R2 As Region = New Region(GraphicPath)
  731.         Dim R3 As Region = New Region(GraphicPath)
  732.         Dim M1 As Matrix = New Matrix
  733.         Dim M2 As Matrix = New Matrix
  734.         Dim M3 As Matrix = New Matrix
  735.         M1.Translate(0, -0.5)
  736.         M2.Translate(0, 0.5)
  737.         M3.Translate(1, 0)
  738.         R1.Transform(M1)
  739.         R2.Transform(M2)
  740.         R3.Transform(M3)
  741.         R.Union(R1)
  742.         R.Union(R2)
  743.         R.Union(R3)
  744.         Me.Region = R
  745.         Dim RF As RectangleF = R.GetBounds(e.Graphics)
  746.         Dim rec As New Rectangle(0, 0, RF.Width, RF.Height)
  747.         Dim te As TabControl.TabPaintEventArgs
  748.         te = New TabControl.TabPaintEventArgs(e.Graphics, rec, m_Selected, m_Hot, GraphicPath, Width, Height)
  749.         RaiseEvent TabPaintBackground(Me, te) ' try to owner draw
  750.         Dim gb As LinearGradientBrush = CreateGradientBrush(New Rectangle(0, 0, Me.Width, Me.Height), RenderHighColor, RenderLowColor)
  751.         If Not te.Handled Then e.Graphics.FillPath(gb, GraphicPath)
  752.         gb.Dispose()
  753.         te.Dispose()
  754.         te = New TabControl.TabPaintEventArgs(e.Graphics, rec, m_Selected, m_Hot, GraphicPath, Width, Height)
  755.         RaiseEvent TabPaintBorder(Me, te) ' try to owner draw
  756.         If Not te.Handled Then
  757.             If m_BorderEnhanced Then
  758.                 Dim c As Color = IIf(m_Alignment = TabControl.TabAlignment.Bottom, RenderLowColor, RenderHighColor)
  759.                 Dim p As New Pen(c, m_BorderEnhanceWeight)
  760.                 e.Graphics.DrawLines(p, GraphicPath.PathPoints)
  761.                 p.Dispose()
  762.             End If
  763.             Dim p1 As New Pen(RenderBorderColor)
  764.             e.Graphics.DrawLines(p1, GraphicPath.PathPoints)
  765.             p1.Dispose()
  766.         End If
  767.         te.Dispose()
  768.         e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.None
  769.         e.Graphics.DrawLine(New Pen(RenderBottomColor), GraphicPath.PathPoints(0), GraphicPath.PathPoints(GraphicPath.PointCount - 1))
  770.         e.Graphics.SmoothingMode = m_SmoothingMode
  771.         DrawIcon(e.Graphics)
  772.         DrawText(e.Graphics)
  773.         If m_CloseButtonVisible Then DrawCloseButton(e.Graphics)
  774.         Me.ResumeLayout()
  775.         ' do the memory cleanup
  776.         GraphicPath.Dispose()
  777.         M1.Dispose()
  778.         M2.Dispose()
  779.         M3.Dispose()
  780.         R1.Dispose()
  781.         R2.Dispose()
  782.         R3.Dispose()
  783.         R.Dispose()
  784.         te.Dispose()
  785.         Painting = False
  786.     End Sub
  787. #Region " Obsolete properties "
  788.     <EditorBrowsable(EditorBrowsableState.Never)> _
  789.     Public Overrides Property MinimumSize() As Size
  790.         Get
  791.         End Get
  792.         Set(ByVal value As Size)
  793.         End Set
  794.     End Property
  795.     <EditorBrowsable(EditorBrowsableState.Never)> _
  796.     Public Overrides Property MaximumSize() As Size
  797.         Get
  798.         End Get
  799.         Set(ByVal value As Size)
  800.         End Set
  801.     End Property
  802.     <EditorBrowsable(EditorBrowsableState.Never)> _
  803.     Public Shadows Property Padding() As Padding
  804.         Get
  805.         End Get
  806.         Set(ByVal value As Padding)
  807.         End Set
  808.     End Property
  809.     <EditorBrowsable(EditorBrowsableState.Never)> _
  810.     Public Overrides Property BackColor() As Color
  811.         Get
  812.         End Get
  813.         Set(ByVal value As Color)
  814.         End Set
  815.     End Property
  816.     <EditorBrowsable(EditorBrowsableState.Never)> _
  817.     Public Overrides Property Dock() As System.Windows.Forms.DockStyle
  818.         Get
  819.         End Get
  820.         Set(ByVal value As System.Windows.Forms.DockStyle)
  821.         End Set
  822.     End Property
  823.     <EditorBrowsable(EditorBrowsableState.Never)> _
  824.     Public Overrides Property Anchor() As System.Windows.Forms.AnchorStyles
  825.         Get
  826.         End Get
  827.         Set(ByVal value As System.Windows.Forms.AnchorStyles)
  828.         End Set
  829.     End Property
  830.     <EditorBrowsable(EditorBrowsableState.Never)> _
  831.     Public Overrides Property Text() As String
  832.         Get
  833.             Return Nothing
  834.         End Get
  835.         Set(ByVal value As String)
  836.         End Set
  837.     End Property
  838. #End Region
  839. End Class