Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

RadioButtonRenderer Class

Provides methods used to render an option button control (also known as a radio button) with or without visual styles. This class cannot be inherited.

System::Object
  System.Windows.Forms::RadioButtonRenderer

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

public ref class RadioButtonRenderer sealed

The RadioButtonRenderer type exposes the following members.

  NameDescription
Public propertyStatic memberRenderMatchingApplicationStateGets or sets a value indicating whether the renderer uses the application state to determine rendering style.
Top

  NameDescription
Public methodStatic memberDrawParentBackgroundDraws the background of a control's parent in the specified area.
Public methodStatic memberDrawRadioButton(Graphics, Point, RadioButtonState)Draws an option button control (also known as a radio button) in the specified state and location.
Public methodStatic memberDrawRadioButton(Graphics, Point, Rectangle, String, Font, Boolean, RadioButtonState)Draws an option button control (also known as a radio button) in the specified state and location, with the specified text, and with an optional focus rectangle.
Public methodStatic memberDrawRadioButton(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Boolean, RadioButtonState)Draws an option button control (also known as a radio button) in the specified state and location, with the specified text and text formatting, and with an optional focus rectangle.
Public methodStatic memberDrawRadioButton(Graphics, Point, Rectangle, String, Font, Image, Rectangle, Boolean, RadioButtonState)Draws an option button control (also known as a radio button) in the specified state and location, with the specified text and image, and with an optional focus rectangle.
Public methodStatic memberDrawRadioButton(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, RadioButtonState)Draws an option button control (also known as a radio button) in the specified state and location; with the specified text, text formatting, and image; and with an optional focus rectangle.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodStatic memberGetGlyphSizeReturns the size, in pixels, of the option button (also known as a radio button) glyph.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodStatic memberIsBackgroundPartiallyTransparentIndicates whether the background of the option button (also known as a radio button) has semitransparent or alpha-blended pieces.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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

If visual styles are enabled in the operating system and visual styles are applied to the current application, DrawRadioButton will draw the option button with the current visual style. Otherwise, DrawRadioButton will draw the option 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::RadioButton 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 write a custom control that uses the DrawRadioButton method to draw an option button that responds to mouse clicks.

#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::VisualStyles;

namespace RadioButtonRendererSample
{
    public ref class CustomRadioButton : public Control
    {
    private:
        Rectangle textRectangleValue;
    private:
        bool clicked;
    private:
        RadioButtonState state;

    public:
        CustomRadioButton() : Control()
        {
            textRectangleValue = Rectangle();
            state = RadioButtonState::UncheckedNormal;
            this->Location = Point(50, 50);
            this->Size = System::Drawing::Size(100, 20);
            this->Text = "Click here";
            this->Font = SystemFonts::IconTitleFont;
        }

        // Define the text bounds so that the text rectangle 
        // does not include the radio button. 
    public:
        property Rectangle TextRectangle
        {
            Rectangle get()
            {
                Graphics^ g = this->CreateGraphics();

                textRectangleValue.X = ClientRectangle.X +
                    RadioButtonRenderer::GetGlyphSize(g,
                    RadioButtonState::UncheckedNormal).Width;
                textRectangleValue.Y = ClientRectangle.Y;
                textRectangleValue.Width = ClientRectangle.Width -
                    RadioButtonRenderer::GetGlyphSize(g,
                    RadioButtonState::UncheckedNormal).Width;
                textRectangleValue.Height = ClientRectangle.Height;

                delete g;                

                return textRectangleValue;
            }
        }

        // Draw the radio button in the current state. 
    protected:
        virtual void OnPaint(PaintEventArgs^ e) override
        {
            __super::OnPaint(e);

            RadioButtonRenderer::DrawRadioButton(e->Graphics,
                ClientRectangle.Location, TextRectangle, this->Text,
                this->Font, clicked, state);
        }

        // Draw the radio button in the checked or unchecked state. 
    protected:
        virtual void OnMouseDown(MouseEventArgs^ e) override
        {
            __super::OnMouseDown(e);

            if (!clicked)
            {
                clicked = true;
                this->Text = "Clicked!";
                state = RadioButtonState::CheckedPressed;
                Invalidate();
            }
            else
            {
                clicked = false;
                this->Text = "Click here";
                state = RadioButtonState::UncheckedNormal;
                Invalidate();
            }
        }

        // Draw the radio button in the hot state. 
    protected:
        virtual void OnMouseHover(EventArgs^ e) override
        {
            __super::OnMouseHover(e);
            state = clicked ? RadioButtonState::CheckedHot :
                RadioButtonState::UncheckedHot;
            Invalidate();
        }

        // Draw the radio button in the hot state. 
    protected:
        virtual void OnMouseUp(MouseEventArgs^ e) override
        {
            __super::OnMouseUp(e);
            this->OnMouseHover(e);
        }

        // Draw the radio button in the normal (i.e. not hot) state 
    protected:
        virtual void OnMouseLeave(EventArgs^ e) override
        {
            __super::OnMouseLeave(e);
            state = clicked ? RadioButtonState::CheckedNormal :
                RadioButtonState::UncheckedNormal;
            Invalidate();
        }
    };

    public ref class Form1 : public Form
    {
    public:
        Form1() : Form()
        {
            Controls->Add(gcnew CustomRadioButton());

            if (Application::RenderWithVisualStyles)
            {
                this->Text = "Visual Styles Enabled";
            }
            else
            {
                this->Text = "Visual Styles Disabled";
            }
        }
    };
}

[STAThread]
int main()
{
    // If you do not call EnableVisualStyles below, then 
    // RadioButtonRenderer.DrawRadioButton automatically detects 
    // this and draws the radio button without visual styles.
    Application::EnableVisualStyles();
    Application::Run(gcnew RadioButtonRendererSample::Form1());
}

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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