ToolStripRenderer Class
TOC
Collapse the table of content
Expand the table of content

ToolStripRenderer Class

 

Handles the painting functionality for ToolStrip objects.

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


public abstract class ToolStripRenderer

NameDescription
System_CAPS_protmethodToolStripRenderer()

Initializes a new instance of the ToolStripRenderer class.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticCreateDisabledImage(Image)

Creates a gray-scale copy of a given image.

System_CAPS_pubmethodDrawArrow(ToolStripArrowRenderEventArgs)

Draws an arrow on a ToolStripItem.

System_CAPS_pubmethodDrawButtonBackground(ToolStripItemRenderEventArgs)

Draws the background for a ToolStripButton.

System_CAPS_pubmethodDrawDropDownButtonBackground(ToolStripItemRenderEventArgs)

Draws the background for a ToolStripDropDownButton.

System_CAPS_pubmethodDrawGrip(ToolStripGripRenderEventArgs)

Draws a move handle on a ToolStrip.

System_CAPS_pubmethodDrawImageMargin(ToolStripRenderEventArgs)

Draws the space around an image on a ToolStrip.

System_CAPS_pubmethodDrawItemBackground(ToolStripItemRenderEventArgs)

Draws the background for a ToolStripItem.

System_CAPS_pubmethodDrawItemCheck(ToolStripItemImageRenderEventArgs)

Draws an image on a ToolStripItem that indicates the item is in a selected state.

System_CAPS_pubmethodDrawItemImage(ToolStripItemImageRenderEventArgs)

Draws an image on a ToolStripItem.

System_CAPS_pubmethodDrawItemText(ToolStripItemTextRenderEventArgs)

Draws text on a ToolStripItem.

System_CAPS_pubmethodDrawLabelBackground(ToolStripItemRenderEventArgs)

Draws the background for a ToolStripLabel.

System_CAPS_pubmethodDrawMenuItemBackground(ToolStripItemRenderEventArgs)

Draws the background for a ToolStripMenuItem.

System_CAPS_pubmethodDrawOverflowButtonBackground(ToolStripItemRenderEventArgs)

Draws the background for an overflow button.

System_CAPS_pubmethodDrawSeparator(ToolStripSeparatorRenderEventArgs)
System_CAPS_pubmethodDrawSplitButton(ToolStripItemRenderEventArgs)
System_CAPS_pubmethodDrawStatusStripSizingGrip(ToolStripRenderEventArgs)

Draws a sizing grip.

System_CAPS_pubmethodDrawToolStripBackground(ToolStripRenderEventArgs)

Draws the background for a ToolStrip.

System_CAPS_pubmethodDrawToolStripBorder(ToolStripRenderEventArgs)

Draws the border for a ToolStrip.

System_CAPS_pubmethodDrawToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

Draws the background of the ToolStripContentPanel.

System_CAPS_pubmethodDrawToolStripPanelBackground(ToolStripPanelRenderEventArgs)

Draws the background of the ToolStripPanel.

System_CAPS_pubmethodDrawToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

Draws the background of the ToolStripStatusLabel.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodInitialize(ToolStrip)

When overridden in a derived class, provides for custom initialization of the given ToolStrip.

System_CAPS_protmethodInitializeContentPanel(ToolStripContentPanel)

Initializes the specified ToolStripContentPanel.

System_CAPS_protmethodInitializeItem(ToolStripItem)

When overridden in a derived class, provides for custom initialization of the given ToolStripItem.

System_CAPS_protmethodInitializePanel(ToolStripPanel)

Initializes the specified ToolStripPanel.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodOnRenderArrow(ToolStripArrowRenderEventArgs)

Raises the RenderArrow event.

System_CAPS_protmethodOnRenderButtonBackground(ToolStripItemRenderEventArgs)

Raises the RenderButtonBackground event.

System_CAPS_protmethodOnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs)
System_CAPS_protmethodOnRenderGrip(ToolStripGripRenderEventArgs)

Raises the RenderGrip event.

System_CAPS_protmethodOnRenderImageMargin(ToolStripRenderEventArgs)

Draws the item background.

System_CAPS_protmethodOnRenderItemBackground(ToolStripItemRenderEventArgs)

Raises the OnRenderItemBackground event.

System_CAPS_protmethodOnRenderItemCheck(ToolStripItemImageRenderEventArgs)

Raises the RenderItemCheck event.

System_CAPS_protmethodOnRenderItemImage(ToolStripItemImageRenderEventArgs)

Raises the RenderItemImage event.

System_CAPS_protmethodOnRenderItemText(ToolStripItemTextRenderEventArgs)

Raises the RenderItemText event.

System_CAPS_protmethodOnRenderLabelBackground(ToolStripItemRenderEventArgs)

Raises the RenderLabelBackground event.

System_CAPS_protmethodOnRenderMenuItemBackground(ToolStripItemRenderEventArgs)

Raises the RenderMenuItemBackground event.

System_CAPS_protmethodOnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs)
System_CAPS_protmethodOnRenderSeparator(ToolStripSeparatorRenderEventArgs)

Raises the RenderSeparator event.

System_CAPS_protmethodOnRenderSplitButtonBackground(ToolStripItemRenderEventArgs)
System_CAPS_protmethodOnRenderStatusStripSizingGrip(ToolStripRenderEventArgs)

Raises the RenderStatusStripSizingGrip event.

System_CAPS_protmethodOnRenderToolStripBackground(ToolStripRenderEventArgs)

Raises the RenderToolStripBackground event.

System_CAPS_protmethodOnRenderToolStripBorder(ToolStripRenderEventArgs)

Raises the RenderToolStripBorder event.

System_CAPS_protmethodOnRenderToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)
System_CAPS_protmethodOnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs)
System_CAPS_protmethodOnRenderToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)
System_CAPS_protmethodSystem_CAPS_staticScaleArrowOffsetsIfNeeded()

This API supports the product infrastructure and is not intended to be used directly from your code. Applies the F:System.Windows.Forms.ToolStripRenderer.Offset2X and F:System.Windows.Forms.ToolStripRenderer.Offset2Y specified to scaling the arrow icon, if scaling is required per the DPI settings of the computer.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_protfieldSystem_CAPS_staticOffset2X

This API supports the product infrastructure and is not intended to be used directly from your code. Gets or sets the offset multiplier for twice the offset along the x-axis.

System_CAPS_protfieldSystem_CAPS_staticOffset2Y

This API supports the product infrastructure and is not intended to be used directly from your code. Gets or sets the offset multiplier for twice the offset along the y axis.

NameDescription
System_CAPS_pubeventRenderArrow

Occurs when an arrow on a ToolStripItem is rendered.

System_CAPS_pubeventRenderButtonBackground

Occurs when the background for a ToolStripButton is rendered

System_CAPS_pubeventRenderDropDownButtonBackground

Occurs when the background for a ToolStripDropDownButton is rendered.

System_CAPS_pubeventRenderGrip

Occurs when the move handle for a ToolStrip is rendered.

System_CAPS_pubeventRenderImageMargin

Draws the margin between an image and its container.

System_CAPS_pubeventRenderItemBackground

Occurs when the background for a ToolStripItem is rendered.

System_CAPS_pubeventRenderItemCheck

Occurs when the image for a selected ToolStripItem is rendered.

System_CAPS_pubeventRenderItemImage

Occurs when the image for a ToolStripItem is rendered.

System_CAPS_pubeventRenderItemText

Occurs when the text for a ToolStripItem is rendered.

System_CAPS_pubeventRenderLabelBackground

Occurs when the background for a ToolStripLabel is rendered.

System_CAPS_pubeventRenderMenuItemBackground

Occurs when the background for a ToolStripMenuItem is rendered.

System_CAPS_pubeventRenderOverflowButtonBackground

Occurs when the background for an overflow button is rendered.

System_CAPS_pubeventRenderSeparator

Occurs when a ToolStripSeparator is rendered.

System_CAPS_pubeventRenderSplitButtonBackground

Occurs when the background for a ToolStripSplitButton is rendered.

System_CAPS_pubeventRenderStatusStripSizingGrip

Occurs when the display style changes.

System_CAPS_pubeventRenderToolStripBackground

Occurs when the background for a ToolStrip is rendered.

System_CAPS_pubeventRenderToolStripBorder

Occurs when the border for a ToolStrip is rendered.

System_CAPS_pubeventRenderToolStripContentPanelBackground

Draws the background of a ToolStripContentPanel.

System_CAPS_pubeventRenderToolStripPanelBackground

Draws the background of a ToolStripPanel.

System_CAPS_pubeventRenderToolStripStatusLabelBackground

Draws the background of a ToolStripStatusLabel.

Use the ToolStripRenderer class to apply a particular style or theme to a ToolStrip. Rather than custom painting a ToolStrip and the ToolStripItem objects it contains, you set the ToolStrip.Renderer property to an object that inherits from ToolStripRenderer. The painting specified by the ToolStripRenderer is applied to the ToolStrip, as well as the items it contains.

You can do custom painting in ToolStrip controls in several ways. As with other Windows Forms controls, the ToolStrip and ToolStripItem both have overridable OnPaint methods and Paint events. As with regular painting, the coordinate system is relative to the client area of the control; that is, the upper left-hand corner of the control is 0, 0. The Paint event and OnPaint method for a ToolStripItem behave like other control paint events.

The ToolStripRenderer class has overridable methods for painting the background, item background, item image, item arrow, item text, and border of the ToolStrip. The event arguments for these methods expose several properties such as rectangles, colors, and text formats that you can adjust as desired.

To adjust just a few aspects of how an item is painted, you typically override the ToolStripRenderer.

If you are writing a new item and want to control all aspects of the painting, override the OnPaint method. From within OnPaint, you can use methods from the ToolStripRenderer.

By default, the ToolStrip is double buffered, taking advantage of the OptimizedDoubleBuffer setting.

The following code example demonstrates how to implement a custom ToolStripRenderer class. The GridStripRenderer class customizes three aspects of the GridStrip control's appearance: GridStrip border, ToolStripButton border, and ToolStripButton image. For a full code listing, see How to: Implement a Custom ToolStripRenderer.

// This class implements a custom ToolStripRenderer for the 
// GridStrip control. It customizes three aspects of the 
// GridStrip control's appearance: GridStrip border, 
// ToolStripButton border, and ToolStripButton image.
internal class GridStripRenderer : ToolStripRenderer
{   
    // The style of the empty cell's text.
    private static StringFormat style = new StringFormat();

    // The thickness (width or height) of a 
    // ToolStripButton control's border.
    static int borderThickness = 2;

    // The main bitmap that is the source for the 
    // subimagesthat are assigned to individual 
    // ToolStripButton controls.
    private Bitmap bmp = null;

    // The brush that paints the background of 
    // the GridStrip control.
    private Brush backgroundBrush = null;

    // This is the static constructor. It initializes the
    // StringFormat for drawing the text in the empty cell.
    static GridStripRenderer()
    {
        style.Alignment = StringAlignment.Center;
        style.LineAlignment = StringAlignment.Center;
    }

    // This method initializes the GridStripRenderer by
    // creating the image that is used as the source for
    // the individual button images.
    protected override void Initialize(ToolStrip ts)
    {
        base.Initialize(ts);

        this.InitializeBitmap(ts);
    }

    // This method initializes an individual ToolStripButton
    // control. It copies a subimage from the GridStripRenderer's
    // main image, according to the position and size of 
    // the ToolStripButton.
    protected override void InitializeItem(ToolStripItem item)
    {
        base.InitializeItem(item);

        GridStrip gs = item.Owner as GridStrip;

        // The empty cell does not receive a subimage.
        if ((item is ToolStripButton) &&
            (item != gs.EmptyCell))
        {
            // Copy the subimage from the appropriate 
            // part of the main image.
            Bitmap subImage = bmp.Clone(
                item.Bounds,
                PixelFormat.Undefined);

            // Assign the subimage to the ToolStripButton
            // control's Image property.
            item.Image = subImage;
        }
    }

    // This utility method creates the main image that
    // is the source for the subimages of the individual 
    // ToolStripButton controls.
    private void InitializeBitmap(ToolStrip toolStrip)
    {
        // Create the main bitmap, into which the image is drawn.
        this.bmp = new Bitmap(
            toolStrip.Size.Width,
            toolStrip.Size.Height);

        // Draw a fancy pattern. This could be any image or drawing.
        using (Graphics g = Graphics.FromImage(bmp))
        {
            // Draw smoothed lines.
            g.SmoothingMode = SmoothingMode.AntiAlias;

            // Draw the image. In this case, it is 
            // a number of concentric ellipses. 
            for (int i = 0; i < toolStrip.Size.Width; i += 8)
            {
                g.DrawEllipse(Pens.Blue, 0, 0, i, i);
            }
        }
    }

    // This method draws a border around the GridStrip control.
    protected override void OnRenderToolStripBorder(
        ToolStripRenderEventArgs e)
    {
        base.OnRenderToolStripBorder(e);

        ControlPaint.DrawFocusRectangle(
            e.Graphics,
            e.AffectedBounds,
            SystemColors.ControlDarkDark,
            SystemColors.ControlDarkDark);
    }

    // This method renders the GridStrip control's background.
    protected override void OnRenderToolStripBackground(
        ToolStripRenderEventArgs e)
    {
        base.OnRenderToolStripBackground(e);

        // This late initialization is a workaround. The gradient
        // depends on the bounds of the GridStrip control. The bounds 
        // are dependent on the layout engine, which hasn't fully
        // performed layout by the time the Initialize method runs.
        if (this.backgroundBrush == null)
        {
            this.backgroundBrush = new LinearGradientBrush(
               e.ToolStrip.ClientRectangle,
               SystemColors.ControlLightLight,
               SystemColors.ControlDark,
               90,
               true);
        }

        // Paint the GridStrip control's background.
        e.Graphics.FillRectangle(
            this.backgroundBrush, 
            e.AffectedBounds);
    }

    // This method draws a border around the button's image. If the background
    // to be rendered belongs to the empty cell, a string is drawn. Otherwise,
    // a border is drawn at the edges of the button.
    protected override void OnRenderButtonBackground(
        ToolStripItemRenderEventArgs e)
    {
        base.OnRenderButtonBackground(e);

        // Define some local variables for convenience.
        Graphics g = e.Graphics;
        GridStrip gs = e.ToolStrip as GridStrip;
        ToolStripButton gsb = e.Item as ToolStripButton;

        // Calculate the rectangle around which the border is painted.
        Rectangle imageRectangle = new Rectangle(
            borderThickness, 
            borderThickness, 
            e.Item.Width - 2 * borderThickness, 
            e.Item.Height - 2 * borderThickness);

        // If rendering the empty cell background, draw an 
        // explanatory string, centered in the ToolStripButton.
        if (gsb == gs.EmptyCell)
        {
            e.Graphics.DrawString(
                "Drag to here",
                gsb.Font, 
                SystemBrushes.ControlDarkDark,
                imageRectangle, style);
        }
        else
        {
            // If the button can be a drag source, paint its border red.
            // otherwise, paint its border a dark color.
            Brush b = gs.IsValidDragSource(gsb) ? b = 
                Brushes.Red : SystemBrushes.ControlDarkDark;

            // Draw the top segment of the border.
            Rectangle borderSegment = new Rectangle(
                0, 
                0, 
                e.Item.Width, 
                imageRectangle.Top);
            g.FillRectangle(b, borderSegment);

            // Draw the right segment.
            borderSegment = new Rectangle(
                imageRectangle.Right,
                0,
                e.Item.Bounds.Right - imageRectangle.Right,
                imageRectangle.Bottom);
            g.FillRectangle(b, borderSegment);

            // Draw the left segment.
            borderSegment = new Rectangle(
                0,
                0,
                imageRectangle.Left,
                e.Item.Height);
            g.FillRectangle(b, borderSegment);

            // Draw the bottom segment.
            borderSegment = new Rectangle(
                0,
                imageRectangle.Bottom,
                e.Item.Width,
                e.Item.Bounds.Bottom - imageRectangle.Bottom);
            g.FillRectangle(b, borderSegment);
        }
    }
}

.NET Framework
Available since 2.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft