Читати англійською Редагувати

Поділитися через


ButtonRenderer.DrawButton Method

Definition

Draws a button control.

Overloads

DrawButton(Graphics, Rectangle, PushButtonState)

Draws a button control in the specified state and bounds.

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)

Draws a button control in the specified state and bounds, with the specified image, and with an optional focus rectangle.

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

DrawButton(Graphics, Rectangle, PushButtonState)

Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs

Draws a button control in the specified state and bounds.

C#
public static void DrawButton(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.VisualStyles.PushButtonState state);

Parameters

g
Graphics

The Graphics used to draw the button.

bounds
Rectangle

The Rectangle that specifies the bounds of the button.

state
PushButtonState

One of the PushButtonState values that specifies the visual state of the button.

Remarks

If visual styles are enabled in the operating system and visual styles are applied to the current application, this method will draw the button with the current visual style. Otherwise, it will draw the button with the classic Windows style.

Applies to

.NET Framework 4.8.1 та інші версії
Продукт Версії
.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, 10

DrawButton(Graphics, Rectangle, Boolean, PushButtonState)

Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs

Draws a button control in the specified state and bounds, and with an optional focus rectangle.

C#
public static void DrawButton(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, bool focused, System.Windows.Forms.VisualStyles.PushButtonState state);

Parameters

g
Graphics

The Graphics used to draw the button.

bounds
Rectangle

The Rectangle that specifies the bounds of the button.

focused
Boolean

true to draw a focus rectangle on the button; otherwise, false.

state
PushButtonState

One of the PushButtonState values that specifies the visual state of the button.

Remarks

If visual styles are enabled in the operating system and visual styles are applied to the current application, this method will draw the button with the current visual style. Otherwise, it will draw the button with the classic Windows style.

Applies to

.NET Framework 4.8.1 та інші версії
Продукт Версії
.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, 10

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

Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs

Draws a button control in the specified state and bounds, with the specified image, and with an optional focus rectangle.

C#
public static void DrawButton(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Drawing.Image image, System.Drawing.Rectangle imageBounds, bool focused, System.Windows.Forms.VisualStyles.PushButtonState state);

Parameters

g
Graphics

The Graphics used to draw the button.

bounds
Rectangle

The Rectangle that specifies the bounds of the button.

image
Image

The Image to draw on the button.

imageBounds
Rectangle

The Rectangle that represents the dimensions of image.

focused
Boolean

true to draw a focus rectangle on the button; otherwise, false.

state
PushButtonState

One of the PushButtonState values that specifies the visual state of the button.

Remarks

If visual styles are enabled in the operating system and visual styles are applied to the current application, this method will draw the button with the current visual style. Otherwise, it will draw the button with the classic Windows style.

Applies to

.NET Framework 4.8.1 та інші версії
Продукт Версії
.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, 10

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

Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs

Draws a button control in the specified state and bounds, with the specified text, and with an optional focus rectangle.

C#
public static void DrawButton(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, string buttonText, System.Drawing.Font font, bool focused, System.Windows.Forms.VisualStyles.PushButtonState state);
C#
public static void DrawButton(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, string? buttonText, System.Drawing.Font? font, bool focused, System.Windows.Forms.VisualStyles.PushButtonState state);

Parameters

g
Graphics

The Graphics used to draw the button.

bounds
Rectangle

The Rectangle that specifies the bounds of the button.

buttonText
String

The String to draw on the button.

font
Font

The Font to apply to buttonText.

focused
Boolean

true to draw a focus rectangle on the button; otherwise, false.

state
PushButtonState

One of the PushButtonState values that specifies the visual state of the button.

Examples

The following code example uses the DrawButton(Graphics, Rectangle, String, Font, Boolean, PushButtonState) method in a custom control's OnPaint method to draw a button in the state determined by the location of the mouse pointer. This code example is part of a larger example provided for the ButtonRenderer class.

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

Remarks

If visual styles are enabled in the operating system and visual styles are applied to the current application, this method will draw the button with the current visual style. Otherwise, it will draw the button with the classic Windows style.

Applies to

.NET Framework 4.8.1 та інші версії
Продукт Версії
.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, 10

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

Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs

Draws a button control in the specified state and bounds, with the specified text and text formatting, and with an optional focus rectangle.

C#
public static void DrawButton(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, string buttonText, System.Drawing.Font font, System.Windows.Forms.TextFormatFlags flags, bool focused, System.Windows.Forms.VisualStyles.PushButtonState state);
C#
public static void DrawButton(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, string? buttonText, System.Drawing.Font? font, System.Windows.Forms.TextFormatFlags flags, bool focused, System.Windows.Forms.VisualStyles.PushButtonState state);

Parameters

g
Graphics

The Graphics used to draw the button.

bounds
Rectangle

The Rectangle that specifies the bounds of the button.

buttonText
String

The String to draw on the button.

font
Font

The Font to apply to buttonText.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values to apply to buttonText.

focused
Boolean

true to draw a focus rectangle on the button; otherwise, false.

state
PushButtonState

One of the PushButtonState values that specifies the visual state of the button.

Remarks

If visual styles are enabled in the operating system and visual styles are applied to the current application, this method will draw the button with the current visual style. Otherwise, it will draw the button with the classic Windows style.

Applies to

.NET Framework 4.8.1 та інші версії
Продукт Версії
.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, 10

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

Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs

Draws a button control in the specified state and bounds, with the specified text and image, and with an optional focus rectangle.

C#
public static void DrawButton(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, string buttonText, System.Drawing.Font font, System.Drawing.Image image, System.Drawing.Rectangle imageBounds, bool focused, System.Windows.Forms.VisualStyles.PushButtonState state);
C#
public static void DrawButton(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, string? buttonText, System.Drawing.Font? font, System.Drawing.Image image, System.Drawing.Rectangle imageBounds, bool focused, System.Windows.Forms.VisualStyles.PushButtonState state);

Parameters

g
Graphics

The Graphics used to draw the button.

bounds
Rectangle

The Rectangle that specifies the bounds of the button.

buttonText
String

The String to draw on the button.

font
Font

The Font to apply to buttonText.

image
Image

The Image to draw on the button.

imageBounds
Rectangle

The Rectangle that represents the dimensions of image.

focused
Boolean

true to draw a focus rectangle on the button; otherwise, false.

state
PushButtonState

One of the PushButtonState values that specifies the visual state of the button.

Remarks

If visual styles are enabled in the operating system and visual styles are applied to the current application, this method will draw the button with the current visual style. Otherwise, it will draw the button with the classic Windows style.

Applies to

.NET Framework 4.8.1 та інші версії
Продукт Версії
.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, 10

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

Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs
Source:
ButtonRenderer.cs

Draws a button control in the specified state and bounds; with the specified text, text formatting, and image; and with an optional focus rectangle.

C#
public static void DrawButton(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, string buttonText, System.Drawing.Font font, System.Windows.Forms.TextFormatFlags flags, System.Drawing.Image image, System.Drawing.Rectangle imageBounds, bool focused, System.Windows.Forms.VisualStyles.PushButtonState state);
C#
public static void DrawButton(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, string? buttonText, System.Drawing.Font? font, System.Windows.Forms.TextFormatFlags flags, System.Drawing.Image image, System.Drawing.Rectangle imageBounds, bool focused, System.Windows.Forms.VisualStyles.PushButtonState state);

Parameters

g
Graphics

The Graphics used to draw the button.

bounds
Rectangle

The Rectangle that specifies the bounds of the button.

buttonText
String

The String to draw on the button.

font
Font

The Font to apply to buttonText.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values to apply to buttonText.

image
Image

The Image to draw on the button.

imageBounds
Rectangle

The Rectangle that represents the dimensions of image.

focused
Boolean

true to draw a focus rectangle on the button; otherwise, false.

state
PushButtonState

One of the PushButtonState values that specifies the visual state of the button.

Remarks

If visual styles are enabled in the operating system and visual styles are applied to the current application, this method will draw the button with the current visual style. Otherwise, it will draw the button with the classic Windows style.

Applies to

.NET Framework 4.8.1 та інші версії
Продукт Версії
.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, 10