ToolStripRenderer.OnRenderButtonBackground 方法

定义

引发 RenderButtonBackground 事件。

protected:
 virtual void OnRenderButtonBackground(System::Windows::Forms::ToolStripItemRenderEventArgs ^ e);
protected virtual void OnRenderButtonBackground (System.Windows.Forms.ToolStripItemRenderEventArgs e);
abstract member OnRenderButtonBackground : System.Windows.Forms.ToolStripItemRenderEventArgs -> unit
override this.OnRenderButtonBackground : System.Windows.Forms.ToolStripItemRenderEventArgs -> unit
Protected Overridable Sub OnRenderButtonBackground (e As ToolStripItemRenderEventArgs)

参数

示例

下面的代码示例演示如何重写 方法,OnRenderButtonBackground以在控件的 Image周围ToolStripButton绘制边框。 此代码示例是为 ToolStripRenderer 类提供的一个更大示例的一部分。

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

注解

引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件

OnRenderButtonBackground 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。

继承者说明

在派生类中重写 OnRenderButtonBackground(ToolStripItemRenderEventArgs) 时,一定要调用基类的 OnRenderButtonBackground(ToolStripItemRenderEventArgs) 方法,以便已注册的委托对事件进行接收。

适用于

另请参阅