RadioButtonRenderer 类

定义

提供用于呈现带/不带视觉样式的选项按钮控件(也称为单选按钮)的方法。 此类不能被继承。

C#
public sealed class RadioButtonRenderer
C#
public static class RadioButtonRenderer
继承
RadioButtonRenderer

示例

下面的代码示例演示如何编写使用 DrawRadioButton 该方法绘制响应鼠标单击的选项按钮的自定义控件。

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();
        }
    }
}

注解

RadioButtonRenderer 类提供了一组 static 可用于呈现选项按钮控件的方法。 呈现控件是指绘制控件的用户界面。 若要绘制选项按钮,请使用其中 DrawRadioButton 一种方法。 这些方法提供各种选项,例如使用选项按钮绘制文本或图像。

如果在操作系统中启用了视觉样式,并且视觉样式应用于当前应用程序, DrawRadioButton 则会使用当前视觉样式绘制选项按钮。 否则,DrawRadioButton将使用经典Windows样式绘制选项按钮。 如果要绘制应自动匹配操作系统的当前视觉样式设置的自定义控件,这非常有用。

此类将 System.Windows.Forms.VisualStyles.VisualStyleRenderer 设置为类公开 System.Windows.Forms.VisualStyles.VisualStyleElement.Button.RadioButton 的某个元素的功能包装。 有关详细信息,请参阅 使用视觉样式呈现控件

属性

RenderMatchingApplicationState

获取或设置一个值,该值指示呈现器是否使用应用程序状态来确定呈现样式。

方法

DrawParentBackground(Graphics, Rectangle, Control)

在指定区域中绘制控件的父级的背景。

DrawRadioButton(Graphics, Point, RadioButtonState)

在指定状态下和指定位置绘制选项按钮控件(也称为单选按钮)。

DrawRadioButton(Graphics, Point, Rectangle, String, Font, Boolean, RadioButtonState)

在指定状态下和指定位置,用指定文本和可选的聚焦框绘制选项按钮控件(也称为单选按钮)。

DrawRadioButton(Graphics, Point, Rectangle, String, Font, Image, Rectangle, Boolean, RadioButtonState)

在指定状态下和指定位置,用指定文本和图像以及可选的聚焦框,绘制选项按钮控件(也称为单选按钮)。

DrawRadioButton(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Boolean, RadioButtonState)

在指定状态下和指定位置,用指定文本和文本格式以及可选的聚焦框,绘制选项按钮控件(也称为单选按钮)。

DrawRadioButton(Graphics, Point, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, RadioButtonState)

在指定状态下和指定位置,用指定文本、文本格式、图像和可选的聚焦框,绘制选项按钮控件(也称为单选按钮)。

GetGlyphSize(Graphics, RadioButtonState)

返回选项按钮(也称为单选按钮)标志符号的大小(以像素为单位)。

IsBackgroundPartiallyTransparent(RadioButtonState)

指示选项按钮(也称为单选按钮)的背景是否有半透明或 alpha 混合块。

适用于

产品 版本
.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
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅