ButtonRenderer ButtonRenderer ButtonRenderer ButtonRenderer Class

定义

提供一些方法,这些方法使用或不使用视觉样式呈现按钮控件。Provides methods used to render a button control with or without visual styles. 此类不能被继承。This class cannot be inherited.

public ref class ButtonRenderer sealed
public sealed class ButtonRenderer
type ButtonRenderer = class
Public NotInheritable Class ButtonRenderer
继承
ButtonRendererButtonRendererButtonRendererButtonRenderer

示例

下面的代码示例演示如何创建使用DrawButton方法绘制按钮的自定义控件。The following code example demonstrates how to create a custom control that uses the DrawButton method to draw a button. 单击该按钮时, 控件将在原始按钮的边界内绘制一个较小的按钮, 该控件使用DrawParentBackground方法来绘制原始按钮的其余部分。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.

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

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

namespace ButtonRendererSample
{
    public ref class CustomButton : public Control
    {
    private:
        Rectangle clickRectangleValue;

    private:
        PushButtonState state;

    public:
        CustomButton()
        {
            __super::Control();
            this->Size = System::Drawing::Size(100, 40);
            this->Location = Point(50, 50);
            this->Font = SystemFonts::IconTitleFont;
            this->Text = "Click here";
            clickRectangleValue = Rectangle();
            state = PushButtonState::Normal;
        }

        // Define the bounds of the smaller pressed button.
    public:
        property Rectangle ClickRectangle
        {
            Rectangle get()
            {
                clickRectangleValue.X = ClientRectangle.X +
                    (int)(.2 * ClientRectangle.Width);
                clickRectangleValue.Y = ClientRectangle.Y +
                    (int)(.2 * ClientRectangle.Height);
                clickRectangleValue.Width = ClientRectangle.Width -
                    (int)(.4 * ClientRectangle.Width);
                clickRectangleValue.Height = ClientRectangle.Height -
                    (int)(.4 * ClientRectangle.Height);

                return clickRectangleValue;
            }
        }

        // Draw the large or small button, depending on the current state.
    protected:
        virtual void OnPaint(PaintEventArgs^ e) override
        {
            __super::OnPaint(e);

            // Draw the smaller pressed button image
            if (state == PushButtonState::Pressed)
            {
                // 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)
                {
                    this->BackColor = Color::Azure;
                }
                else
                {
                    this->BackColor = this->Parent->BackColor;
                }


                // 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,
                    ClientRectangle, this);
                ButtonRenderer::DrawButton(e->Graphics, ClickRectangle,
                    this->Text, this->Font, true, state);
            }

            // Draw the bigger unpressed button image.
            else
            {
                ButtonRenderer::DrawButton(e->Graphics, ClientRectangle,
                    this->Text, this->Font, false, state);
            }
        }

        // Draw the smaller pressed button image.
    protected:
        virtual void OnMouseDown(MouseEventArgs^ e) override
        {
            __super::OnMouseDown(e);
            this->Text = "Clicked!";
            state = PushButtonState::Pressed;
            Invalidate();
        }

        // Draw the button in the hot state.
    protected:
        virtual void OnMouseEnter(EventArgs^ e) override
        {
            __super::OnMouseEnter(e);
            this->Text = "Click here";
            state = PushButtonState::Hot;
            Invalidate();
        }

        // Draw the button in the unpressed state.
    protected:
        virtual void OnMouseLeave(EventArgs^ e) override
        {
            __super::OnMouseLeave(e);
            this->Text = "Click here";
            state = PushButtonState::Normal;
            Invalidate();
        }

        // Draw the button hot if the mouse is released on the button.
    protected:
        virtual void OnMouseUp(MouseEventArgs^ e) override
        {
            __super::OnMouseUp(e);
            OnMouseEnter(e);
        }

        // Detect when the cursor leaves the button area while
        // it is pressed.
    protected:
        virtual void OnMouseMove(MouseEventArgs^ e) override
        {
            __super::OnMouseMove(e);

            // Detect when the left mouse button is down and
            // the cursor has left the pressed button area.
            if ((e->Button & ::MouseButtons::Left) == ::MouseButtons::Left &&
                !ClientRectangle.Contains(e->Location) &&
                state == PushButtonState::Pressed)
            {
                OnMouseLeave(e);
            }
        }
    };

    ref class Form1 : public Form
    {
    public:
        Form1()
        {
            __super::Form();
            CustomButton^ Button1 = gcnew CustomButton();
            Controls->Add(Button1);

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

using namespace ButtonRendererSample;

[STAThread]
int main()
{ 
    // If you do not call EnableVisualStyles below, then
    // ButtonRenderer automatically detects this and draws
    // the button without visual styles.
    Application::EnableVisualStyles();
    Application::Run(gcnew Form1());
}
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace ButtonRendererSample
{
    class Form1 : Form
    {
        public Form1()
            : base()
        {
            CustomButton Button1 = new CustomButton();
            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   
            // ButtonRenderer automatically detects this and draws
            // the button without visual styles.
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }

    public class CustomButton : Control
    {
        private Rectangle clickRectangleValue = new Rectangle();
        private PushButtonState state = PushButtonState.Normal;

        public CustomButton()
            : base()
        {
            this.Size = new Size(100, 40);
            this.Location = new Point(50, 50);
            this.Font = SystemFonts.IconTitleFont;
            this.Text = "Click here";
        }

        // Define the bounds of the smaller pressed button.
        public Rectangle ClickRectangle
        {
            get
            {
                clickRectangleValue.X = ClientRectangle.X +
                    (int)(.2 * ClientRectangle.Width);
                clickRectangleValue.Y = ClientRectangle.Y +
                    (int)(.2 * ClientRectangle.Height);
                clickRectangleValue.Width = ClientRectangle.Width -
                    (int)(.4 * ClientRectangle.Width);
                clickRectangleValue.Height = ClientRectangle.Height -
                    (int)(.4 * ClientRectangle.Height);

                return clickRectangleValue;
            }
        }

        // Draw the large or small button, depending on the current state.
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            // Draw the smaller pressed button image
            if (state == PushButtonState.Pressed)
            {
                // 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.
                this.BackColor = Application.RenderWithVisualStyles ?
                    Color.Azure : this.Parent.BackColor;

                // 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,
                    ClientRectangle, this);
                ButtonRenderer.DrawButton(e.Graphics, ClickRectangle,
                    this.Text, this.Font, true, state);
            }

            // Draw the bigger unpressed button image.
            else
            {
                ButtonRenderer.DrawButton(e.Graphics, ClientRectangle,
                    this.Text, this.Font, false, state);
            }
        }

        // Draw the smaller pressed button image.
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.Text = "Clicked!";
            state = PushButtonState.Pressed;
            Invalidate();
        }

        // Draw the button in the hot state. 
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.Text = "Click here";
            state = PushButtonState.Hot;
            Invalidate();
        }

        // Draw the button in the unpressed state.
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.Text = "Click here";
            state = PushButtonState.Normal;
            Invalidate();
        }

        // Draw the button hot if the mouse is released on the button. 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            OnMouseEnter(e);
        }

        // Detect when the cursor leaves the button area while 
        // it is pressed.
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            // Detect when the left mouse button is down and   
            // the cursor has left the pressed button area. 
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left &&
                !ClientRectangle.Contains(e.Location) &&
                state == PushButtonState.Pressed)
            {
                OnMouseLeave(e);
            }
        }
    }
}
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

注解

类提供一static组可用于呈现按钮控件的方法。 ButtonRendererThe 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. 若要绘制按钮, 请使用其中一DrawButton种方法。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.

如果在操作系统中启用了视觉样式, 并将视觉样式应用于当前应用程序, DrawButton将绘制具有当前视觉样式的按钮。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. 否则, DrawButton将绘制具有经典 Windows 样式的按钮。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.

此类包装设置为System.Windows.Forms.VisualStyles.VisualStyleRenderer System.Windows.Forms.VisualStyles.VisualStyleElement.Button.PushButton类公开的元素之一的的功能。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.

属性

RenderMatchingApplicationState RenderMatchingApplicationState RenderMatchingApplicationState RenderMatchingApplicationState

获取或设置一个值,该值指示呈现器是否使用应用程序状态来确定呈现样式。Gets or sets a value indicating whether the renderer uses the application state to determine rendering style.

方法

DrawButton(Graphics, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, Boolean, PushButtonState)

在指定边界内使用一个可选焦点矩形绘制指定状态的按钮控件。Draws a button control in the specified state and bounds, and with an optional focus rectangle.

DrawButton(Graphics, Rectangle, Image, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, Image, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, Image, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, Image, Rectangle, Boolean, PushButtonState)

在指定边界内使用指定图像和一个可选焦点矩形绘制指定状态的按钮控件。Draws a button control in the specified state and bounds, with the specified image, and with an optional focus rectangle.

DrawButton(Graphics, Rectangle, PushButtonState) DrawButton(Graphics, Rectangle, PushButtonState) DrawButton(Graphics, Rectangle, PushButtonState) DrawButton(Graphics, Rectangle, PushButtonState)

在指定边界内绘制指定状态的按钮控件。Draws a button control in the specified state and bounds.

DrawButton(Graphics, Rectangle, String, Font, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, Boolean, PushButtonState)

在指定边界内使用指定文本和一个可选焦点矩形绘制指定状态的按钮控件。Draws a button control in the specified state and bounds, with the specified text, and with an optional focus rectangle.

DrawButton(Graphics, Rectangle, String, Font, Image, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, Image, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, Image, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, Image, Rectangle, Boolean, PushButtonState)

在指定边界内使用指定文本、图像和一个可选焦点矩形绘制指定状态的按钮控件。Draws a button control in the specified state and bounds, with the specified text and image, and with an optional focus rectangle.

DrawButton(Graphics, Rectangle, String, Font, TextFormatFlags, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, TextFormatFlags, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, TextFormatFlags, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, TextFormatFlags, Boolean, PushButtonState)

在指定边界内使用指定文本、文本格式和一个可选焦点矩形绘制指定状态的按钮控件。Draws a button control in the specified state and bounds, with the specified text and text formatting, and with an optional focus rectangle.

DrawButton(Graphics, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, PushButtonState) DrawButton(Graphics, Rectangle, String, Font, TextFormatFlags, Image, Rectangle, Boolean, PushButtonState)

在指定边界内使用指定文本、文本格式、图像和一个可选焦点矩形绘制指定状态的按钮控件。Draws a button control in the specified state and bounds; with the specified text, text formatting, and image; and with an optional focus rectangle.

DrawParentBackground(Graphics, Rectangle, Control) DrawParentBackground(Graphics, Rectangle, Control) DrawParentBackground(Graphics, Rectangle, Control) DrawParentBackground(Graphics, Rectangle, Control)

在指定区域中绘制控件的父级的背景。Draws the background of a control's parent in the specified area.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsBackgroundPartiallyTransparent(PushButtonState) IsBackgroundPartiallyTransparent(PushButtonState) IsBackgroundPartiallyTransparent(PushButtonState) IsBackgroundPartiallyTransparent(PushButtonState)

指示按钮的背景是否有半透明部分或 Alpha 混合部分。Indicates whether the background of the button has semitransparent or alpha-blended pieces.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅