VisualStyleRenderer Class

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Provides methods for drawing and getting information about a VisualStyleElement. This class cannot be inherited.

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

System.Object
  System.Windows.Forms.VisualStyles.VisualStyleRenderer

Public NotInheritable Class VisualStyleRenderer

NameDescription
System_CAPS_pubmethodVisualStyleRenderer(String, Int32, Int32)

Initializes a new instance of the VisualStyleRenderer class using the given class, part, and state values.

System_CAPS_pubmethodVisualStyleRenderer(VisualStyleElement)

Initializes a new instance of the VisualStyleRenderer class using the given VisualStyleElement.

NameDescription
System_CAPS_pubpropertyClass

Gets the class name of the current visual style element.

System_CAPS_pubpropertyHandle

Gets a unique identifier for the current class of visual style elements.

System_CAPS_pubpropertySystem_CAPS_staticIsSupported

Gets a value specifying whether the operating system is using visual styles to draw controls.

System_CAPS_pubpropertyLastHResult

Gets the last error code returned by the native visual styles (UxTheme) API methods encapsulated by the VisualStyleRenderer class.

System_CAPS_pubpropertyPart

Gets the part of the current visual style element.

System_CAPS_pubpropertyState

Gets the state of the current visual style element.

NameDescription
System_CAPS_pubmethodDrawBackground(IDeviceContext, Rectangle)

Draws the background image of the current visual style element within the specified bounding rectangle.

System_CAPS_pubmethodDrawBackground(IDeviceContext, Rectangle, Rectangle)

Draws the background image of the current visual style element within the specified bounding rectangle and clipped to the specified clipping rectangle.

System_CAPS_pubmethodDrawEdge(IDeviceContext, Rectangle, Edges, EdgeStyle, EdgeEffects)

Draws one or more edges of the specified bounding rectangle.

System_CAPS_pubmethodDrawImage(Graphics, Rectangle, Image)

Draws the specified image within the specified bounds.

System_CAPS_pubmethodDrawImage(Graphics, Rectangle, ImageList, Int32)

Draws the image from the specified ImageList within the specified bounds.

System_CAPS_pubmethodDrawParentBackground(IDeviceContext, Rectangle, Control)

Draws the background of a control's parent in the specified area.

System_CAPS_pubmethodDrawText(IDeviceContext, Rectangle, String)

Draws text in the specified bounds using default formatting.

System_CAPS_pubmethodDrawText(IDeviceContext, Rectangle, String, Boolean)

Draws text in the specified bounds with the option of displaying disabled text.

System_CAPS_pubmethodDrawText(IDeviceContext, Rectangle, String, Boolean, TextFormatFlags)

Draws text in the specified bounding rectangle with the option of displaying disabled text and applying other text formatting.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetBackgroundContentRectangle(IDeviceContext, Rectangle)

Returns the content area for the background of the current visual style element.

System_CAPS_pubmethodGetBackgroundExtent(IDeviceContext, Rectangle)

Returns the entire background area for the current visual style element.

System_CAPS_pubmethodGetBackgroundRegion(IDeviceContext, Rectangle)

Returns the region for the background of the current visual style element.

System_CAPS_pubmethodGetBoolean(BooleanProperty)

Returns the value of the specified Boolean property for the current visual style element.

System_CAPS_pubmethodGetColor(ColorProperty)

Returns the value of the specified color property for the current visual style element.

System_CAPS_pubmethodGetEnumValue(EnumProperty)

Returns the value of the specified enumerated type property for the current visual style element.

System_CAPS_pubmethodGetFilename(FilenameProperty)

Returns the value of the specified file name property for the current visual style element.

System_CAPS_pubmethodGetFont(IDeviceContext, FontProperty)

Returns the value of the specified font property for the current visual style element.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetInteger(IntegerProperty)

Returns the value of the specified integer property for the current visual style element.

System_CAPS_pubmethodGetMargins(IDeviceContext, MarginProperty)

Returns the value of the specified margins property for the current visual style element.

System_CAPS_pubmethodGetPartSize(IDeviceContext, Rectangle, ThemeSizeType)

Returns the value of the specified size property of the current visual style part using the specified drawing bounds.

System_CAPS_pubmethodGetPartSize(IDeviceContext, ThemeSizeType)

Returns the value of the specified size property of the current visual style part.

System_CAPS_pubmethodGetPoint(PointProperty)

Returns the value of the specified point property for the current visual style element.

System_CAPS_pubmethodGetString(StringProperty)

Returns the value of the specified string property for the current visual style element.

System_CAPS_pubmethodGetTextExtent(IDeviceContext, Rectangle, String, TextFormatFlags)

Returns the size and location of the specified string when drawn with the font of the current visual style element within the specified initial bounding rectangle.

System_CAPS_pubmethodGetTextExtent(IDeviceContext, String, TextFormatFlags)

Returns the size and location of the specified string when drawn with the font of the current visual style element.

System_CAPS_pubmethodGetTextMetrics(IDeviceContext)

Retrieves information about the font specified by the current visual style element.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodHitTestBackground(Graphics, Rectangle, Region, Point, HitTestOptions)

Returns a hit test code indicating whether the point is contained in the background of the current visual style element and within the specified bounds.

System_CAPS_pubmethodHitTestBackground(IDeviceContext, Rectangle, IntPtr, Point, HitTestOptions)

Returns a hit test code indicating whether the point is contained in the background of the current visual style element and within the specified region.

System_CAPS_pubmethodHitTestBackground(IDeviceContext, Rectangle, Point, HitTestOptions)

Returns a hit test code indicating whether a point is contained in the background of the current visual style element.

System_CAPS_pubmethodIsBackgroundPartiallyTransparent()

Indicates whether the background of the current visual style element has any semitransparent or alpha-blended pieces.

System_CAPS_pubmethodSystem_CAPS_staticIsElementDefined(VisualStyleElement)

Determines whether the specified visual style element is defined by the current visual style.

System_CAPS_pubmethodSetParameters(String, Int32, Int32)

Sets this VisualStyleRenderer to the visual style element represented by the specified class, part, and state values.

System_CAPS_pubmethodSetParameters(VisualStyleElement)

Sets this VisualStyleRenderer to the visual style element represented by the specified VisualStyleElement.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

The System.Windows.Forms.VisualStyles namespace exposes VisualStyleElement objects that represent all of the controls and user interface (UI) elements that are supported by visual styles. To draw or get information about a particular element, you must set a VisualStyleRenderer to the element you are interested in. A VisualStyleRenderer is automatically set to a specified VisualStyleElement in the VisualStyleRenderer constructor, but you can also set an existing VisualStyleRenderer to a different element by calling the SetParameters method.

To draw an element, use the DrawBackground method. The VisualStyleRenderer class also includes methods, such as GetColor and GetEnumValue, that provide information about how an element is defined by the current visual style.

The VisualStyleRenderer constructor and many of the VisualStyleRenderer methods throw exceptions unless visual styles are enabled in the operating system and visual styles are applied to the client area of application windows. To check for these conditions, use the staticIsSupported property.

The VisualStyleRenderer class wraps the functionality of the visual styles (UxTheme) API from the Windows Shell portion of the Windows Platform SDK. For more information about visual styles, see "Using Windows XP Visual Styles" in the Platform SDK portion of the MSDN Library at http://msdn.microsoft.com/library.

WinXpFamily

Visual styles are supported only on these platforms.

The following code example uses the VisualStyleRenderer class to implement a custom control that simulates some of the basic UI of a window, including dragging with the title bar, resizing with the sizing handle, and closing. This example uses several VisualStyleElement objects that represent standard parts of a window, including the elements exposed by the VisualStyleElement.Window.Caption, VisualStyleElement.Window.CloseButton, and VisualStyleElement.Status.Gripper classes.

Imports System
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles

Namespace VisualStyleRendererSample

    Class Form1
        Inherits Form

        Public Sub New()
            With Me
                .Size = New Size(800, 600)
                .Location = New Point(20, 20)
                .BackColor = Color.DarkGray
            End With
            Dim Window1 As New ImitationWindow()
            Controls.Add(Window1)
        End Sub

        <STAThread()> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub
    End Class

    Public Class ImitationWindow
        Inherits Control

        Private windowElements As _
            New Dictionary(Of String, VisualStyleElement)
        Private elementRectangles As _
            New Dictionary(Of String, Rectangle)
        Private renderer As VisualStyleRenderer = Nothing
        Private closeButtonOffset As Point
        Private gripperSize As Size
        Private closeButtonSize As Size
        Private isResizing As Boolean = False
        Private isMoving As Boolean = False
        Private isClosing As Boolean = False
        Private captionHeight As Integer
        Private frameThickness As Integer
        Private statusHeight As Integer = 22
        Private originalClick As New Point()
        Private resizeOffset As New Point()

        Public Sub New()
            With Me
                .Location = New Point(50, 50)
                .Size = New Size(350, 300)
                .BackColor = Color.Azure
                .DoubleBuffered = True
                .MinimumSize = New Size(300, 200)
                .Font = SystemFonts.CaptionFont
                .Text = "Imitation Window"
            End With

            ' Create a collection of VisualStyleElement objects.
            With windowElements
                .Add("windowCaption", _
                    VisualStyleElement.Window.Caption.Active)
                .Add("windowBottom", _
                    VisualStyleElement.Window.FrameBottom.Active)
                .Add("windowLeft", _
                    VisualStyleElement.Window.FrameLeft.Active)
                .Add("windowRight", _
                    VisualStyleElement.Window.FrameRight.Active)
                .Add("windowClose", _
                    VisualStyleElement.Window.CloseButton.Normal)
                .Add("statusBar", _
                    VisualStyleElement.Status.Bar.Normal)
                .Add("statusGripper", _
                    VisualStyleElement.Status.Gripper.Normal)
            End With

            ' Get the sizes and location offsets for the window parts  
            ' as specified by the visual style, and then use this   
            ' information to calcualate the rectangles for each part.
            GetPartDetails()
            CalculateRectangles()
        End Sub

        ' Get the sizes and offsets for the window parts as specified 
        ' by the visual style.
        Private Sub GetPartDetails()
            ' Do nothing further if visual styles are not enabled.
            If Not Application.RenderWithVisualStyles Then
                Return
            End If

            Using g As Graphics = Me.CreateGraphics()
                ' Get the size and offset of the close button.
                If SetRenderer(windowElements("windowClose")) Then
                    closeButtonSize = _
                        renderer.GetPartSize(g, ThemeSizeType.True)
                    closeButtonOffset = _
                        renderer.GetPoint(PointProperty.Offset)
                End If

                ' Get the height of the window caption.
                If SetRenderer(windowElements("windowCaption")) Then
                    captionHeight = renderer.GetPartSize(g, _
                        ThemeSizeType.True).Height
                End If

                ' Get the thickness of the left, bottom, and right 
                ' window frame.
                If SetRenderer(windowElements("windowLeft")) Then
                    frameThickness = renderer.GetPartSize(g, _
                        ThemeSizeType.True).Width
                End If

                ' Get the size of the resizing gripper.
                If SetRenderer(windowElements("statusGripper")) Then
                    gripperSize = renderer.GetPartSize(g, _
                        ThemeSizeType.True)
                End If
            End Using
        End Sub

        ' Use the part metrics to determine the current size of the 
        ' rectangles for all of the window parts.
        Private Sub CalculateRectangles()

            Dim heightMinusFrame As Integer = _
                ClientRectangle.Height - frameThickness

            ' Calculate the window frame rectangles and add them
            ' to the Dictionary of rectangles.
            elementRectangles("windowCaption") = _
                New Rectangle(0, 0, ClientRectangle.Width, _
                captionHeight)
            elementRectangles("windowBottom") = _
                New Rectangle(0, heightMinusFrame, _
                ClientRectangle.Width, frameThickness)
            elementRectangles("windowLeft") = _
                New Rectangle(0, captionHeight, frameThickness, _
                heightMinusFrame - captionHeight)
            elementRectangles("windowRight") = _
                New Rectangle(ClientRectangle.Width - frameThickness, _
                captionHeight, frameThickness, _
                heightMinusFrame - captionHeight)

            ' Calculate the window button rectangle and add it
            ' to the Dictionary of rectangles.
            elementRectangles("windowClose") = _
                New Rectangle(ClientRectangle.Right + _
                closeButtonOffset.X, closeButtonOffset.Y, _
                closeButtonSize.Width, closeButtonSize.Height)

            ' Calculate the status bar rectangles and add them
            ' to the Dictionary of rectangles.
            elementRectangles("statusBar") = _
                New Rectangle(frameThickness, _
                heightMinusFrame - statusHeight, _
                ClientRectangle.Width - 2 * frameThickness, _
                statusHeight)
            elementRectangles("statusGripper") = _
                New Rectangle(ClientRectangle.Right - _
                gripperSize.Width - frameThickness, _
                heightMinusFrame - gripperSize.Height, _
                gripperSize.Width, gripperSize.Height)
        End Sub

        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            ' Ensure that visual styles are supported.
            If Not Application.RenderWithVisualStyles Then
                Me.Text = "Visual styles are not enabled."
                TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, _
                    Me.Location, Me.ForeColor)
                Return
            End If

            ' Set the clip region to define the curved corners of 
            ' the caption.
            SetClipRegion()

            ' Draw each part of the window.
            Dim entry As KeyValuePair(Of String, VisualStyleElement)
            For Each entry In windowElements
                If SetRenderer(entry.Value) Then
                    renderer.DrawBackground(e.Graphics, _
                        elementRectangles(entry.Key))
                End If
            Next entry

            ' Draw the caption text.
            TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, _
                elementRectangles("windowCaption"), Color.White, _
                TextFormatFlags.VerticalCenter Or _
                TextFormatFlags.HorizontalCenter)
        End Sub

        ' Initiate dragging, resizing, or closing the imitation window.
        Private Sub ImitationWindow_MouseDown(ByVal sender As Object, _
            ByVal e As MouseEventArgs) Handles Me.MouseDown

            ' The user clicked the close button.
            If elementRectangles("windowClose"). _
                Contains(e.Location) Then

                windowElements("windowClose") = _
                    VisualStyleElement.Window.CloseButton.Pressed
                isClosing = True

            ' The user clicked the status grip.
            ElseIf elementRectangles("statusGripper"). _
                Contains(e.Location) Then

                isResizing = True
                Me.Cursor = Cursors.SizeNWSE
                resizeOffset.X = Me.Right - Me.Left - e.X
                resizeOffset.Y = Me.Bottom - Me.Top - e.Y

            ' The user clicked the window caption.
            ElseIf elementRectangles("windowCaption"). _
                Contains(e.Location) Then

                isMoving = True
                originalClick.X = e.X
                originalClick.Y = e.Y
            End If
            Invalidate()
        End Sub

        ' Stop any current resizing or moving actions.
        Private Sub ImitationWindow_MouseUp(ByVal sender As Object, _
            ByVal e As MouseEventArgs) Handles Me.MouseUp

            ' Stop moving the location of the window rectangles.
            If isMoving Then
                isMoving = False

            ' Change the cursor back to the default if the 
            ' user stops resizing.
            ElseIf isResizing Then
                isResizing = False

            ' Close the application if the user clicks the 
            ' close button.
            ElseIf elementRectangles("windowClose"). _
                Contains(e.Location) And isClosing Then
                Application.Exit()
            End If
        End Sub

        ' Handle resizing or moving actions.
        Private Sub ImitationWindow_MouseMove(ByVal sender As Object, _
            ByVal e As MouseEventArgs) Handles Me.MouseMove

            ' The left mouse button is down.
            If (MouseButtons.Left And e.Button) = _
                MouseButtons.Left Then

                ' Calculate the new control size if the user is  
                ' dragging the resizing grip.
                If isResizing Then
                    Me.Width = e.X + resizeOffset.X
                    Me.Height = e.Y + resizeOffset.Y
                    CalculateRectangles()

                ' Calculate the new location of the control if   
                ' the user is dragging the window caption.
                ElseIf isMoving Then
                    Dim XChange As Integer = Me.Location.X + _
                        (e.X - originalClick.X)
                    Dim YChange As Integer = Me.Location.Y + _
                        (e.Y - originalClick.Y)
                    Me.Location = New Point(XChange, YChange)

                ' Cancel the closing action if the user clicked and  
                ' held down on the close button, and has dragged the   
                ' pointer outside the button.
                ElseIf Not elementRectangles("windowClose"). _
                    Contains(e.Location) And isClosing Then

                    isClosing = False
                    windowElements("windowClose") = _
                        VisualStyleElement.Window.CloseButton.Normal
                End If

            ' The left mouse button is not down.
            Else
                ' Paint the close button hot if the cursor is on it.
                If elementRectangles("windowClose"). _
                    Contains(e.Location) Then
                    windowElements("windowClose") = _
                        VisualStyleElement.Window.CloseButton.Hot
                Else
                    windowElements("windowClose") = _
                        VisualStyleElement.Window.CloseButton.Normal
                End If

                ' Use a resizing cursor if the cursor is on the 
                ' status grip.
                If elementRectangles("statusGripper"). _
                    Contains(e.Location) Then
                    Me.Cursor = Cursors.SizeNWSE
                Else
                    Me.Cursor = Cursors.Default
                End If
            End If
            Invalidate()
        End Sub

        ' Calculate and set the clipping region for the control  
        ' so that the corners of the title bar are rounded.
        Private Sub SetClipRegion()
            If Not Application.RenderWithVisualStyles Then
                Return
            End If

            Using g As Graphics = Me.CreateGraphics()
                ' Get the current region for the window caption.
                If SetRenderer(windowElements("windowCaption")) Then
                    Dim clipRegion As Region = _
                        renderer.GetBackgroundRegion(g, _
                        elementRectangles("windowCaption"))

                    ' Get the client rectangle, but exclude the   
                    ' region of the window caption.
                    Dim height As Integer = _
                        CInt(clipRegion.GetBounds(g).Height)
                    Dim nonCaptionRect As _
                        New Rectangle(ClientRectangle.X, _
                        ClientRectangle.Y + height, _
                        ClientRectangle.Width, _
                        ClientRectangle.Height - height)

                    ' Add the rectangle to the caption region, and  
                    ' make this region the form's clipping region.
                    clipRegion.Union(nonCaptionRect)
                    Me.Region = clipRegion
                End If
            End Using
        End Sub

        ' Set the VisualStyleRenderer to a new element.
        Private Function SetRenderer(ByVal element As _
            VisualStyleElement) As Boolean

            If Not VisualStyleRenderer.IsElementDefined(element) Then
                Return False
            End If

            If renderer Is Nothing Then
                renderer = New VisualStyleRenderer(element)
            Else
                renderer.SetParameters(element)
            End If

            Return True
        End Function

    End Class
End Namespace

.NET Framework
Available since 2.0

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

Return to top
Show: