ToolStripRenderer.OnRenderToolStripBackground(ToolStripRenderEventArgs) ToolStripRenderer.OnRenderToolStripBackground(ToolStripRenderEventArgs) ToolStripRenderer.OnRenderToolStripBackground(ToolStripRenderEventArgs) ToolStripRenderer.OnRenderToolStripBackground(ToolStripRenderEventArgs) Method

定义

引发 RenderToolStripBackground 事件。Raises the RenderToolStripBackground event.

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

参数

示例

下面的代码示例演示如何重写OnRenderToolStripBackground方法以绘制背景的渐变ToolStrip控件。The following code example demonstrates how to override the OnRenderToolStripBackground method to paint a gradient in the background of a ToolStrip control. 此代码示例是为提供一个更大示例的一部分ToolStripRenderer类。This code example is part of a larger example provided for the ToolStripRenderer class.

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

注解

引发事件时,将通过委托调用事件处理程序。Raising an event invokes the event handler through a delegate. 有关详细信息,请参阅处理和引发事件For more information, see Handling and Raising Events.

OnRenderToolStripBackground 方法还允许派生类对事件进行处理而不必附加委托。The OnRenderToolStripBackground method also allows derived classes to handle the event without attaching a delegate. 这是在派生类中处理事件的首选技术。This is the preferred technique for handling the event in a derived class.

继承者说明

在派生类中重写 OnRenderToolStripBackground(ToolStripRenderEventArgs) 时,一定要调用基类的 OnRenderToolStripBackground(ToolStripRenderEventArgs) 方法,以便已注册的委托对事件进行接收。When overriding OnRenderToolStripBackground(ToolStripRenderEventArgs) in a derived class, be sure to call the base class's OnRenderToolStripBackground(ToolStripRenderEventArgs) method so that registered delegates receive the event.

适用于

另请参阅