CheckBoxRenderer Class
TOC
Collapse the table of content
Expand the table of content

CheckBoxRenderer Class

 

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

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

System.Object
  System.Windows.Forms.CheckBoxRenderer

Public NotInheritable Class CheckBoxRenderer

NameDescription
System_CAPS_pubpropertySystem_CAPS_staticRenderMatchingApplicationState

Gets or sets a value indicating whether the renderer uses the application state to determine rendering style.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticDrawCheckBox(Graphics, Point, CheckBoxState)

Draws a check box control in the specified state and location.

System_CAPS_pubmethodSystem_CAPS_staticDrawCheckBox(Graphics, Point, Rectangle, String, Font, Boolean, CheckBoxState)

Draws a check box control in the specified state and location, with the specified text, and with an optional focus rectangle.

System_CAPS_pubmethodSystem_CAPS_staticDrawCheckBox(Graphics, Point, Rectangle, String, Font, Image, Rectangle, Boolean, CheckBoxState)

Draws a check box control in the specified state and location, with the specified text and image, and with an optional focus rectangle.

System_CAPS_pubmethodSystem_CAPS_staticDrawCheckBox(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Boolean, CheckBoxState)

Draws a check box control in the specified state and location, with the specified text and text formatting, and with an optional focus rectangle.

System_CAPS_pubmethodSystem_CAPS_staticDrawCheckBox(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, CheckBoxState)

Draws a check box control in the specified state and location; with the specified text, text formatting, and image; and with an optional focus rectangle.

System_CAPS_pubmethodSystem_CAPS_staticDrawParentBackground(Graphics, Rectangle, Control)

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

System_CAPS_pubmethodEquals(Object)

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

System_CAPS_pubmethodSystem_CAPS_staticGetGlyphSize(Graphics, CheckBoxState)

Returns the size of the check box glyph.

System_CAPS_pubmethodGetHashCode()

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

System_CAPS_pubmethodGetType()

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

System_CAPS_pubmethodSystem_CAPS_staticIsBackgroundPartiallyTransparent(CheckBoxState)

Indicates whether the background of the check box has semitransparent or alpha-blended pieces.

System_CAPS_pubmethodToString()

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

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

If visual styles are enabled in the operating system and visual styles are applied to the current application, DrawCheckBox will draw the check box with the current visual style. Otherwise, DrawCheckBox will draw the check box 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.CheckBox class. For more information, see Rendering Controls with Visual Styles.

WinXpFamily

Visual styles are supported only on these platforms.

The following code example demonstrates how to write a custom control that uses the DrawCheckBox method to draw a check box that responds to mouse clicks.

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

Namespace CheckBoxRendererSample

    Class Form1
        Inherits Form

        Public Sub New()
            Dim CheckBox1 As New CustomCheckBox()
            Controls.Add(CheckBox1)

            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 
            ' CheckBoxRenderer.DrawCheckBox automatically detects   
            ' this and draws the check box without visual styles.
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub
    End Class

    Public Class CustomCheckBox
        Inherits Control

        Private textRectangleValue As New Rectangle()
        Private clickedLocationValue As New Point()
        Private clicked As Boolean = False
        Private state As CheckBoxState = CheckBoxState.UncheckedNormal

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

        ' Calculate the text bounds, exluding the check box.
        Public ReadOnly Property TextRectangle() As Rectangle
            Get
                Using g As Graphics = Me.CreateGraphics()
                    With textRectangleValue
                        .X = Me.ClientRectangle.X + _
                            CheckBoxRenderer.GetGlyphSize(g, _
                            CheckBoxState.UncheckedNormal).Width
                        .Y = Me.ClientRectangle.Y
                        .Width = Me.ClientRectangle.Width - _
                            CheckBoxRenderer.GetGlyphSize(g, _
                            CheckBoxState.UncheckedNormal).Width
                        .Height = Me.ClientRectangle.Height
                    End With
                End Using
                Return textRectangleValue
            End Get
        End Property

        ' Draw the check box in the current state.
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)
            CheckBoxRenderer.DrawCheckBox(e.Graphics, _
                Me.ClientRectangle.Location, TextRectangle, Me.Text, _
                Me.Font, TextFormatFlags.HorizontalCenter, _
                clicked, state)
        End Sub

        ' Draw the check box in the checked or unchecked state, alternately.
        Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
            MyBase.OnMouseDown(e)
            If Not clicked Then
                With Me
                    .clicked = True
                    .Text = "Clicked!"
                    .state = CheckBoxState.CheckedPressed
                End With
                Invalidate()
            Else
                With Me
                    .clicked = False
                    .Text = "Click here"
                    .state = CheckBoxState.UncheckedNormal
                End With
                Invalidate()
            End If
        End Sub

        ' Draw the check box in the hot state. 
        Protected Overrides Sub OnMouseHover(ByVal e As EventArgs)
            MyBase.OnMouseHover(e)
            If clicked Then
                state = CheckBoxState.CheckedHot
            Else
                state = CheckBoxState.UncheckedHot
            End If
            Invalidate()
        End Sub

        ' Draw the check box in the hot state. 
        Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
            MyBase.OnMouseUp(e)
            Me.OnMouseHover(e)
        End Sub

        ' Draw the check box in the unpressed state.
        Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs)
            MyBase.OnMouseLeave(e)
            If clicked Then
                state = CheckBoxState.CheckedNormal
            Else
                state = CheckBoxState.UncheckedNormal
            End If
            Invalidate()
        End Sub

    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:
© 2016 Microsoft