Export (0) Print
Expand All

TabRenderer Class

Provides methods used to render a tab control with visual styles. This class cannot be inherited.

System.Object
  System.Windows.Forms.TabRenderer

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public NotInheritable Class TabRenderer

The TabRenderer type exposes the following members.

  NameDescription
Public propertyStatic memberIsSupportedGets a value indicating whether the TabRenderer class can be used to draw a tab control with visual styles.
Top

  NameDescription
Public methodStatic memberDrawTabItem(Graphics, Rectangle, TabItemState)Draws a tab in the specified state and bounds.
Public methodStatic memberDrawTabItem(Graphics, Rectangle, Boolean, TabItemState)Draws a tab in the specified state and bounds, and with an optional focus rectangle.
Public methodStatic memberDrawTabItem(Graphics, Rectangle, String, Font, TabItemState)Draws a tab in the specified state and bounds, and with the specified text.
Public methodStatic memberDrawTabItem(Graphics, Rectangle, Image, Rectangle, Boolean, TabItemState)Draws a tab in the specified state and bounds, with the specified image, and with an optional focus rectangle.
Public methodStatic memberDrawTabItem(Graphics, Rectangle, String, Font, Boolean, TabItemState)Draws a tab in the specified state and bounds, with the specified text, and with an optional focus rectangle.
Public methodStatic memberDrawTabItem(Graphics, Rectangle, String, Font, TextFormatFlags, Boolean, TabItemState)Draws a tab in the specified state and bounds, with the specified text and text formatting, and with an optional focus rectangle.
Public methodStatic memberDrawTabItem(Graphics, Rectangle, String, Font, Image, Rectangle, Boolean, TabItemState)Draws a tab in the specified state and bounds, with the specified text and image, and with an optional focus rectangle.
Public methodStatic memberDrawTabItem(Graphics, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, TabItemState)Draws a tab in the specified state and bounds; with the specified text, text formatting, and image; and with an optional focus rectangle.
Public methodStatic memberDrawTabPageDraws a tab page in the specified bounds.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The TabRenderer class provides a set of static methods that can be used to render a tab control with the current visual style of the operating system. Rendering a control refers to drawing the user interface of a control. This is useful if you are drawing a custom control that should have the appearance of the current visual style. To draw a tab control, use the DrawTabPage method to draw the page, and use the DrawTabItem method to draw each tab.

If visual styles are enabled in the operating system and visual styles are applied to the client area of application windows, the methods of this class will draw the tab control with the current visual style. Otherwise, the methods and properties of this class will throw an InvalidOperationException. To determine whether the members of this class can be used, check the value of the IsSupported property.

This class wraps the functionality of a System.Windows.Forms.VisualStyles.VisualStyleRenderer that is set to one of the elements exposed by the System.Windows.Forms.VisualStyles.VisualStyleElement.Tab class. For more information, see Rendering Controls with Visual Styles.

Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: Visual styles are supported only on these platforms.

The following code example demonstrates how to create a custom control that uses the DrawTabPage and DrawTabItem methods to draw a basic tab control with two tabs.

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles

Namespace TabRendererSample

    Class Form1
        Inherits Form

        Public Sub New()
            Dim Tab1 As New CustomTabControl()
            Controls.Add(Tab1)
            Me.Size = New Size(500, 500)
        End Sub

        <STAThread()> _
        Shared Sub Main()
            ' The call to EnableVisualStyles below does not affect whether  
            ' TabRenderer.IsSupported is true; as long as visual styles  
            ' are enabled by the operating system, IsSupported is true.
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub 
    End Class 

    Public Class CustomTabControl
        Inherits Control

        Private tabPageRectangle As Rectangle
        Private tabItemRectangle1 As Rectangle
        Private tabItemRectangle2 As Rectangle
        Private tabHeight As Integer = 30
        Private tabWidth As Integer = 100
        Private tab1State As TabItemState = TabItemState.Selected
        Private tab2State As TabItemState = TabItemState.Normal
        Private tab1Text As String = "Tab 1" 
        Private tab2Text As String = "Tab 2" 
        Private tab1Focused As Boolean = True 
        Private tab2Focused As Boolean = False 

        Public Sub New()
            With Me
                .Size = New Size(300, 300)
                .Location = New Point(10, 10)
                .Font = SystemFonts.IconTitleFont
                .Text = "TabRenderer"
                .DoubleBuffered = True 
            End With

            tabPageRectangle = New Rectangle(ClientRectangle.X, _
                ClientRectangle.Y + tabHeight, _
                ClientRectangle.Width, _
                ClientRectangle.Height - tabHeight)

            ' Extend the first tab rectangle down by 2 pixels, because 
            ' it is selected by default.
            tabItemRectangle1 = New Rectangle(ClientRectangle.X, _
                ClientRectangle.Y, tabWidth, tabHeight + 2)

            tabItemRectangle2 = New Rectangle(ClientRectangle.Location.X + _
                tabWidth, ClientRectangle.Location.Y, tabWidth, tabHeight)
        End Sub 

        ' Draw the tab page and the tab items. 
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            If Not TabRenderer.IsSupported Then 
                Me.Parent.Text = "CustomTabControl Disabled" 
                Return 
            End If

            TabRenderer.DrawTabPage(e.Graphics, tabPageRectangle)
            TabRenderer.DrawTabItem(e.Graphics, tabItemRectangle1, _
                tab1Text, Me.Font, tab1Focused, tab1State)
            TabRenderer.DrawTabItem(e.Graphics, tabItemRectangle2, _
                tab2Text, Me.Font, tab2Focused, tab2State)
            Me.Parent.Text = "CustomTabControl Enabled" 
        End Sub 

        ' Draw the selected tab item. 
        Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
            MyBase.OnMouseDown(e)

            If Not TabRenderer.IsSupported Then 
                Return 
            End If 

            ' The first tab is clicked. Note that the height of the  
            ' selected tab rectangle is raised by 2, so that it draws  
            ' over the border of the tab page. 
            If tabItemRectangle1.Contains(e.Location) Then
                tab1State = TabItemState.Selected
                tab2State = TabItemState.Normal
                tabItemRectangle1.Height += 2
                tabItemRectangle2.Height -= 2
                tab1Focused = True
                tab2Focused = False 
            End If 

            ' The second tab is clicked. 
            If tabItemRectangle2.Contains(e.Location) Then
                tab2State = TabItemState.Selected
                tab1State = TabItemState.Normal
                tabItemRectangle2.Height += 2
                tabItemRectangle1.Height -= 2
                tab2Focused = True
                tab1Focused = False 
            End If

            Invalidate()
        End Sub 

    End Class 
End Namespace

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft