This documentation is archived and is not being maintained.

ButtonRenderer Class

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

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

'Declaration
Public NotInheritable Class ButtonRenderer
'Usage
Dim instance As ButtonRenderer

The ButtonRenderer class provides a set of static methods that can be used to render a button control. Rendering a control refers to drawing the user interface of a control. To draw a button, use one of the DrawButton methods. These methods provide a variety of options, such as drawing text or an image on the button.

If visual styles are enabled in the operating system and visual styles are applied to the current application, DrawButton will draw the button with the current visual style. Otherwise, DrawButton will draw the button with the classic Windows style. This is useful if you are drawing a custom control that should automatically match the current visual style setting of the operating system.

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.Button.PushButton 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 DrawButton method to draw a button. When the button is clicked, the control draws a smaller button inside the bounds of the original button, and the control uses the DrawParentBackground method to paint over the rest of the original button.

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

Namespace ButtonRendererSample
    Class Form1
        Inherits Form

        Public Sub New()
            Dim Button1 As New CustomButton()
            Controls.Add(Button1)

            If Application.RenderWithVisualStyles Then 
                Me.Text = "Visual Styles Enabled" 
            Else 
                Me.Text = "Visual Styles Disabled" 
            End If 
        End Sub

        <STAThread()> _
        Shared Sub Main()
            ' If you do not call EnableVisualStyles below, then   
            ' ButtonRenderer automatically detects this and draws 
            ' the button without visual styles.
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub 
    End Class 

    Public Class CustomButton
        Inherits Control

        Private clickRectangleValue As New Rectangle()
        Private state As PushButtonState = PushButtonState.Normal

        Public Sub New()
            With Me
                Size = New Size(100, 40)
                Location = New Point(50, 50)
                Font = SystemFonts.IconTitleFont
                Text = "Click here" 
            End With 
        End Sub 

        ' Define the bounds of the smaller pressed button. 
        Public ReadOnly Property ClickRectangle() As Rectangle
            Get 
                With clickRectangleValue
                    .X = Me.ClientRectangle.X + CInt(0.2 * _
                        Me.ClientRectangle.Width)
                    .Y = Me.ClientRectangle.Y + CInt(0.2 * _
                        Me.ClientRectangle.Height)
                    .Width = Me.ClientRectangle.Width - _
                        CInt(0.4 * Me.ClientRectangle.Width)
                    .Height = Me.ClientRectangle.Height - _
                        CInt(0.4 * Me.ClientRectangle.Height)
                End With 
                Return clickRectangleValue
            End Get 
        End Property 

        ' Draw the large or small button, depending on the current state. 
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            ' Draw the smaller pressed button image. 
            If state = PushButtonState.Pressed Then 
                ' Set the background color to the parent if visual styles   
                ' are disabled, because DrawParentBackground will only paint   
                ' over the control background if visual styles are enabled. 
                If Application.RenderWithVisualStyles Then 
                    Me.BackColor = Color.Azure
                Else 
                    Me.BackColor = Me.Parent.BackColor
                End If 

                ' If you comment out the call to DrawParentBackground,    
                ' the background of the control will still be visible  
                ' outside the pressed button, if visual styles are enabled.
                ButtonRenderer.DrawParentBackground(e.Graphics, _
                    Me.ClientRectangle, Me)
                ButtonRenderer.DrawButton(e.Graphics, ClickRectangle, _
                    Me.Text, Me.Font, True, state)

            ' Draw the bigger unpressed button image. 
            Else
                ButtonRenderer.DrawButton(e.Graphics, Me.ClientRectangle, _
                    Me.Text, Me.Font, False, state)
            End If 
        End Sub 

        ' Draw the smaller pressed button image. 
        Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
            MyBase.OnMouseDown(e)
            With Me
                .Text = "Clicked!"
                .state = PushButtonState.Pressed
            End With
            Invalidate()
        End Sub 

        ' Draw the button in the hot state.  
        Protected Overrides Sub OnMouseEnter(ByVal e As EventArgs)
            MyBase.OnMouseEnter(e)
            With Me
                .Text = "Click here"
                .state = PushButtonState.Hot
            End With
            Invalidate()
        End Sub 

        ' Draw the button in the unpressed state. 
        Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs)
            MyBase.OnMouseLeave(e)
            With Me
                .Text = "Click here"
                .state = PushButtonState.Normal
            End With
            Invalidate()
        End Sub 

        ' Draw the button hot if the mouse is released on the button. 
        Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
            MyBase.OnMouseUp(e)
            OnMouseEnter(e)
        End Sub 

        ' Detect when the cursor leaves the button area while it  
        ' is pressed. 
        Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
            MyBase.OnMouseMove(e)

            ' Detect when the left mouse button is down and    
            ' the cursor has left the pressed button area.  
            If (e.Button And MouseButtons.Left) = MouseButtons.Left And Not _
                Me.ClientRectangle.Contains(e.Location) And _
                state = PushButtonState.Pressed Then
                OnMouseLeave(e)
            End If 
        End Sub 

    End Class 
End Namespace

System.Object
  System.Windows.Forms.ButtonRenderer

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: