Edit

Share via


RadioButtonRenderer Class

Definition

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.

C#
public sealed class RadioButtonRenderer
C#
public static class RadioButtonRenderer
Inheritance
RadioButtonRenderer

Examples

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.

C#
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace RadioButtonRendererSample
{
    class Form1 : Form
    {
        Button button1 = new Button();
        public Form1()
            : base()
        {
            CustomRadioButton RadioButton1 = new CustomRadioButton();
          
            button1.Location = new System.Drawing.Point(175, 231);
            button1.Size = new System.Drawing.Size(105, 23);
            button1.Text = "Toggle Style";
            button1.Click += new System.EventHandler(this.button1_Click);
            Controls.Add(RadioButton1);
            Controls.Add(button1);

            if (Application.RenderWithVisualStyles)
                this.Text = "Visual Styles Enabled";
            else
                this.Text = "Visual Styles Disabled";
        }

        [STAThread]
        static void 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(new Form1());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.VisualStyleState =
                Application.VisualStyleState ^
                VisualStyleState.ClientAndNonClientAreasEnabled;

            GroupBoxRenderer.RenderMatchingApplicationState = true;
            if (Application.RenderWithVisualStyles)
                this.Text = "Visual Styles Enabled";
            else
                this.Text = "Visual Styles Disabled";
        }
    }

    public class CustomRadioButton : Control
    {
        private Rectangle textRectangleValue = new Rectangle();
        private bool clicked = false;
        private RadioButtonState state = RadioButtonState.UncheckedNormal;

        public CustomRadioButton()
            : base()
        {
            this.Location = new Point(50, 50);
            this.Size = new 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 Rectangle TextRectangle
        {
            get
            {
                using (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;
                }

                return textRectangleValue;
            }
        }

        // Draw the radio button in the current state.
        protected override void OnPaint(PaintEventArgs e)
        {
            base.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 override void OnMouseDown(MouseEventArgs e)
        {
            base.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 override void OnMouseHover(EventArgs e)
        {
            base.OnMouseHover(e);
            state = clicked ? RadioButtonState.CheckedHot :
                RadioButtonState.UncheckedHot;
            Invalidate();
        }

        // Draw the radio button in the hot state.
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            this.OnMouseHover(e);
        }

        // Draw the radio button in the unpressed state.
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            state = clicked ? RadioButtonState.CheckedNormal :
                RadioButtonState.UncheckedNormal;
            Invalidate();
        }
    }
}

Remarks

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.

Properties

RenderMatchingApplicationState

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

Methods

DrawParentBackground(Graphics, Rectangle, Control)

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

DrawRadioButton(Graphics, Point, RadioButtonState)

Draws an option button control (also known as a radio button) in the specified state and location.

DrawRadioButton(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.

DrawRadioButton(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.

DrawRadioButton(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.

DrawRadioButton(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.

GetGlyphSize(Graphics, RadioButtonState)

Returns the size, in pixels, of the option button (also known as a radio button) glyph.

IsBackgroundPartiallyTransparent(RadioButtonState)

Indicates whether the background of the option button (also known as a radio button) has semitransparent or alpha-blended pieces.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also