ToolStripRenderer 类

定义

处理 ToolStrip 对象的绘制功能。

public ref class ToolStripRenderer abstract
public abstract class ToolStripRenderer
type ToolStripRenderer = class
Public MustInherit Class ToolStripRenderer
继承
ToolStripRenderer
派生

示例

以下代码示例演示了如何实现一个自定义 ToolStripRenderer 类。 类 GridStripRenderer 自定义控件外观的 GridStrip 三个方面: GridStrip 边框、 ToolStripButton 边框和 ToolStripButton 图像。 有关完整代码列表,请参阅 如何:实现自定义 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);
        }
    }
}
' 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.
Friend Class GridStripRenderer
     Inherits ToolStripRenderer

   ' The style of the empty cell's text.
   Private Shared style As New StringFormat()
   
   ' The thickness (width or height) of a 
   ' ToolStripButton control's border.
   Private Shared borderThickness As Integer = 2
   
   ' The main bitmap that is the source for the 
   ' subimagesthat are assigned to individual 
   ' ToolStripButton controls.
   Private bmp As Bitmap = Nothing
   
   ' The brush that paints the background of 
   ' the GridStrip control.
   Private backgroundBrush As Brush = Nothing
   
   
   ' This is the static constructor. It initializes the
   ' StringFormat for drawing the text in the empty cell.
   Shared Sub New()
      style.Alignment = StringAlignment.Center
      style.LineAlignment = StringAlignment.Center
   End Sub 
   
   ' This method initializes the GridStripRenderer by
   ' creating the image that is used as the source for
   ' the individual button images.
   Protected Overrides Sub Initialize(ts As ToolStrip)
      MyBase.Initialize(ts)
      
      Me.InitializeBitmap(ts)
     End Sub

   ' 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 Overrides Sub InitializeItem(item As ToolStripItem)
      MyBase.InitializeItem(item)
      
         Dim gs As GridStrip = item.Owner
      
      ' The empty cell does not receive a subimage.
         If ((TypeOf (item) Is ToolStripButton) And _
              (item IsNot gs.EmptyCell)) Then
             ' Copy the subimage from the appropriate 
             ' part of the main image.
             Dim subImage As Bitmap = bmp.Clone(item.Bounds, PixelFormat.Undefined)

             ' Assign the subimage to the ToolStripButton
             ' control's Image property.
             item.Image = subImage
         End If
   End Sub 

   ' This utility method creates the main image that
   ' is the source for the subimages of the individual 
   ' ToolStripButton controls.
   Private Sub InitializeBitmap(toolStrip As ToolStrip)
      ' Create the main bitmap, into which the image is drawn.
      Me.bmp = New Bitmap(toolStrip.Size.Width, toolStrip.Size.Height)
      
      ' Draw a fancy pattern. This could be any image or drawing.
      Dim g As Graphics = Graphics.FromImage(bmp)
      Try
         ' Draw smoothed lines.
         g.SmoothingMode = SmoothingMode.AntiAlias
         
         ' Draw the image. In this case, it is 
         ' a number of concentric ellipses. 
         Dim i As Integer
         For i = 0 To toolStrip.Size.Width - 8 Step 8
            g.DrawEllipse(Pens.Blue, 0, 0, i, i)
         Next i
      Finally
         g.Dispose()
      End Try
   End Sub 
   
   ' This method draws a border around the GridStrip control.
   Protected Overrides Sub OnRenderToolStripBorder(e As ToolStripRenderEventArgs)
      MyBase.OnRenderToolStripBorder(e)
      
      ControlPaint.DrawFocusRectangle(e.Graphics, e.AffectedBounds, SystemColors.ControlDarkDark, SystemColors.ControlDarkDark)
   End Sub 

   ' This method renders the GridStrip control's background.
   Protected Overrides Sub OnRenderToolStripBackground(e As ToolStripRenderEventArgs)
      MyBase.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 Me.backgroundBrush Is Nothing Then
         Me.backgroundBrush = New LinearGradientBrush(e.ToolStrip.ClientRectangle, SystemColors.ControlLightLight, SystemColors.ControlDark, 90, True)
      End If
      
      ' Paint the GridStrip control's background.
      e.Graphics.FillRectangle(Me.backgroundBrush, e.AffectedBounds)
     End Sub

   ' 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 Overrides Sub OnRenderButtonBackground(e As ToolStripItemRenderEventArgs)
      MyBase.OnRenderButtonBackground(e)
      
      ' Define some local variables for convenience.
      Dim g As Graphics = e.Graphics
      Dim gs As GridStrip = e.ToolStrip 
      Dim gsb As ToolStripButton = e.Item 
      
      ' Calculate the rectangle around which the border is painted.
      Dim imageRectangle As 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 Is gs.EmptyCell Then
             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.
             Dim b As Brush = IIf(gs.IsValidDragSource(gsb), Brushes.Red, SystemBrushes.ControlDarkDark)

             ' Draw the top segment of the border.
             Dim borderSegment As 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)
         End If
     End Sub
 End Class

注解

ToolStripRenderer使用 类将特定样式或主题应用于 ToolStrip。 将 属性设置为ToolStrip.Renderer继承自 ToolStripRenderer的对象,而不是自定义绘制 ToolStripToolStripItem 及其包含的对象。 指定的 ToolStripRenderer 绘图将应用于 ToolStrip,以及它包含的项。

可以通过多种方式在 ToolStrip 控件中执行自定义绘制。 与其他 Windows 窗体控件一样,ToolStripToolStripItem 两者都具有可重写的 OnPaint 方法和 Paint 事件。 与常规绘制一样,坐标系相对于控件的工作区:也就是说,控件的左上角为 0, 0。 ToolStripItemPaint 事件和 OnPaint 方法的行为方式与其他控件绘制事件一样。

ToolStripRenderer 具有可替代的方法,用于绘制 的背景、项背景、项图像、项箭头、项文本和边框 ToolStrip。 这些方法的事件参数会公开多个属性,如矩形、颜色和文本格式,你可以根据需要进行调整。

若要仅调整项绘制方式的几个方面,通常会重写 ToolStripRenderer

如果要编写一个新项并且要控制绘制的所有方面,请重写 OnPaint 方法。 在 OnPaint 内部,可以使用来自 ToolStripRenderer 的方法。

默认情况下,ToolStrip 会进行双缓冲(利用 OptimizedDoubleBuffer 设置)。

构造函数

ToolStripRenderer()

初始化 ToolStripRenderer 类的新实例。

字段

Offset2X

获取或设置两次沿 x 轴偏移的偏移乘数。

Offset2Y

获取或设置两次沿 y 轴偏移的偏移乘数。

方法

CreateDisabledImage(Image)

创建给定图像的灰度副本。

DrawArrow(ToolStripArrowRenderEventArgs)

ToolStripItem 上绘制一个箭头。

DrawButtonBackground(ToolStripItemRenderEventArgs)

绘制 ToolStripButton 的背景。

DrawDropDownButtonBackground(ToolStripItemRenderEventArgs)

绘制 ToolStripDropDownButton 的背景。

DrawGrip(ToolStripGripRenderEventArgs)

ToolStrip 上绘制一个移动手柄。

DrawImageMargin(ToolStripRenderEventArgs)

ToolStrip 上的图像周围绘制空白。

DrawItemBackground(ToolStripItemRenderEventArgs)

绘制 ToolStripItem 的背景。

DrawItemCheck(ToolStripItemImageRenderEventArgs)

ToolStripItem 上绘制一个图像,指示该项是否处于选中状态。

DrawItemImage(ToolStripItemImageRenderEventArgs)

ToolStripItem 上绘制一个图像。

DrawItemText(ToolStripItemTextRenderEventArgs)

ToolStripItem 上绘制文本。

DrawLabelBackground(ToolStripItemRenderEventArgs)

绘制 ToolStripLabel 的背景。

DrawMenuItemBackground(ToolStripItemRenderEventArgs)

绘制 ToolStripMenuItem 的背景。

DrawOverflowButtonBackground(ToolStripItemRenderEventArgs)

绘制溢出按钮的背景。

DrawSeparator(ToolStripSeparatorRenderEventArgs)

绘制 ToolStripSeparator

DrawSplitButton(ToolStripItemRenderEventArgs)

绘制 ToolStripSplitButton

DrawStatusStripSizingGrip(ToolStripRenderEventArgs)

绘制大小调整手柄。

DrawToolStripBackground(ToolStripRenderEventArgs)

绘制 ToolStrip 的背景。

DrawToolStripBorder(ToolStripRenderEventArgs)

绘制 ToolStrip 的边框。

DrawToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

绘制 ToolStripContentPanel 的背景。

DrawToolStripPanelBackground(ToolStripPanelRenderEventArgs)

绘制 ToolStripPanel 的背景。

DrawToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

绘制 ToolStripStatusLabel 的背景。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
Initialize(ToolStrip)

在派生类中重写时,提供给定 ToolStrip 的自定义初始化。

InitializeContentPanel(ToolStripContentPanel)

初始化指定的 ToolStripContentPanel

InitializeItem(ToolStripItem)

在派生类中重写时,提供给定 ToolStripItem 的自定义初始化。

InitializePanel(ToolStripPanel)

初始化指定的 ToolStripPanel

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OnRenderArrow(ToolStripArrowRenderEventArgs)

引发 RenderArrow 事件。

OnRenderButtonBackground(ToolStripItemRenderEventArgs)

引发 RenderButtonBackground 事件。

OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs)

引发 RenderDropDownButtonBackground 事件。

OnRenderGrip(ToolStripGripRenderEventArgs)

引发 RenderGrip 事件。

OnRenderImageMargin(ToolStripRenderEventArgs)

绘制项的背景。

OnRenderItemBackground(ToolStripItemRenderEventArgs)

引发 OnRenderItemBackground(ToolStripItemRenderEventArgs) 事件。

OnRenderItemCheck(ToolStripItemImageRenderEventArgs)

引发 RenderItemCheck 事件。

OnRenderItemImage(ToolStripItemImageRenderEventArgs)

引发 RenderItemImage 事件。

OnRenderItemText(ToolStripItemTextRenderEventArgs)

引发 RenderItemText 事件。

OnRenderLabelBackground(ToolStripItemRenderEventArgs)

引发 RenderLabelBackground 事件。

OnRenderMenuItemBackground(ToolStripItemRenderEventArgs)

引发 RenderMenuItemBackground 事件。

OnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs)

引发 RenderOverflowButtonBackground 事件。

OnRenderSeparator(ToolStripSeparatorRenderEventArgs)

引发 RenderSeparator 事件。

OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs)

引发 OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs) 事件。

OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs)

引发 RenderStatusStripSizingGrip 事件。

OnRenderToolStripBackground(ToolStripRenderEventArgs)

引发 RenderToolStripBackground 事件。

OnRenderToolStripBorder(ToolStripRenderEventArgs)

引发 RenderToolStripBorder 事件。

OnRenderToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

引发 RenderToolStripContentPanelBackground 事件。

OnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs)

引发 RenderToolStripPanelBackground 事件。

OnRenderToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

引发 RenderToolStripStatusLabelBackground 事件。

ScaleArrowOffsetsIfNeeded()

如果需要基于计算机的 DPI 设置来进行缩放,则将 Offset2XOffset2Y 值应用于缩放箭头图标。

ScaleArrowOffsetsIfNeeded(Int32)

基于指定的 DPI 值将 Offset2XOffset2Y 值应用于缩放箭头图标。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

事件

RenderArrow

在呈现 ToolStripItem 上的箭头时发生。

RenderButtonBackground

当呈现 ToolStripButton 的背景时发生。

RenderDropDownButtonBackground

当呈现 ToolStripDropDownButton 的背景时发生。

RenderGrip

当呈现 ToolStrip 的移动手柄时发生。

RenderImageMargin

绘制图像与图像容器之间的边距。

RenderItemBackground

当呈现 ToolStripItem 的背景时发生。

RenderItemCheck

当呈现选定 ToolStripItem 的图像时发生。

RenderItemImage

当呈现 ToolStripItem 的图像时发生。

RenderItemText

当呈现 ToolStripItem 的文本时发生。

RenderLabelBackground

当呈现 ToolStripLabel 的背景时发生。

RenderMenuItemBackground

当呈现 ToolStripMenuItem 的背景时发生。

RenderOverflowButtonBackground

在呈现溢出按钮的背景时发生。

RenderSeparator

当呈现 ToolStripSeparator 时发生。

RenderSplitButtonBackground

当呈现 ToolStripSplitButton 的背景时发生。

RenderStatusStripSizingGrip

当显示样式更改时发生。

RenderToolStripBackground

当呈现 ToolStrip 的背景时发生。

RenderToolStripBorder

当呈现 ToolStrip 的边框时发生。

RenderToolStripContentPanelBackground

绘制 ToolStripContentPanel 的背景。

RenderToolStripPanelBackground

绘制 ToolStripPanel 的背景。

RenderToolStripStatusLabelBackground

绘制 ToolStripStatusLabel 的背景。

适用于

另请参阅