Export (0) Print
Expand All

ButtonRenderer Class

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

System.Object
  System.Windows.Forms.ButtonRenderer

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

public sealed class ButtonRenderer

The ButtonRenderer 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 memberDrawButton(Graphics, Rectangle, PushButtonState)Draws a button control in the specified state and bounds.
Public methodStatic memberDrawButton(Graphics, Rectangle, Boolean, PushButtonState)Draws a button control in the specified state and bounds, and with an optional focus rectangle.
Public methodStatic memberDrawButton(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.
Public methodStatic memberDrawButton(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.
Public methodStatic memberDrawButton(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.
Public methodStatic memberDrawButton(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.
Public methodStatic memberDrawButton(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.
Public methodStatic memberDrawParentBackgroundDraws the background of a control's parent in the specified area.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
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 button has semitransparent or alpha-blended pieces.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The 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. 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.

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

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.

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 create a custom control that uses the DrawButton method to draw a button. 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;
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);
            }
        }
    }
}

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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