ToolStripItem.OnPaint(PaintEventArgs) ToolStripItem.OnPaint(PaintEventArgs) ToolStripItem.OnPaint(PaintEventArgs) ToolStripItem.OnPaint(PaintEventArgs) Method

Определение

Вызывает событие Paint.Raises the Paint event.

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

Параметры

e
PaintEventArgs PaintEventArgs PaintEventArgs PaintEventArgs

Объект PaintEventArgs, содержащий данные события.A PaintEventArgs that contains the event data.

Примеры

В следующем примере кода показано, как переопределить OnPaint метод для пользовательской отрисовки.The following code example demonstrates how to override the OnPaint method for custom rendering. Этот пример кода является частью большого примера, ToolStripItem приведенного для класса.This code example is part of a larger example provided for the ToolStripItem class.

// This method defines the painting behavior of the control.
// It performs the following operations:
//
// Computes the layout of the item's image and text.
// Draws the item's background image.
// Draws the item's image.
// Draws the item's text.
//
// Drawing operations are implemented in the 
// RolloverItemRenderer class.
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    if (this.Owner != null)
    {
        // Find the dimensions of the image and the text 
        // areas of the item. 
        this.ComputeImageAndTextLayout();

        // Draw the background. This includes drawing a highlighted 
        // border when the mouse is in the client area.
        ToolStripItemRenderEventArgs ea = new ToolStripItemRenderEventArgs(
             e.Graphics,
             this);
        this.Owner.Renderer.DrawItemBackground(ea);

        // Draw the item's image. 
        ToolStripItemImageRenderEventArgs irea =
            new ToolStripItemImageRenderEventArgs(
            e.Graphics,
            this,
            imageRect );
        this.Owner.Renderer.DrawItemImage(irea);

        // If the item is on a drop-down, give its
        // text a different highlighted color.
        Color highlightColor = 
            this.IsOnDropDown ?
            Color.Salmon : SystemColors.ControlLightLight;

        // Draw the text, and highlight it if the 
        // the rollover state is true.
        ToolStripItemTextRenderEventArgs rea =
            new ToolStripItemTextRenderEventArgs(
            e.Graphics,
            this,
            base.Text,
            textRect,
            this.rolloverValue ? highlightColor : base.ForeColor,
            base.Font,
            base.TextAlign);
        this.Owner.Renderer.DrawItemText(rea);
    }
}
' This method defines the painting behavior of the control.
' It performs the following operations:
'
' Computes the layout of the item's image and text.
' Draws the item's background image.
' Draws the item's image.
' Draws the item's text.
'
' Drawing operations are implemented in the 
' RolloverItemRenderer class.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
   MyBase.OnPaint(e)
   
   If (Me.Owner IsNot Nothing) Then
      ' Find the dimensions of the image and the text 
      ' areas of the item. 
      Me.ComputeImageAndTextLayout()
      
      ' Draw the background. This includes drawing a highlighted 
      ' border when the mouse is in the client area.
      Dim ea As New ToolStripItemRenderEventArgs(e.Graphics, Me)
      Me.Owner.Renderer.DrawItemBackground(ea)
      
      ' Draw the item's image. 
      Dim irea As New ToolStripItemImageRenderEventArgs(e.Graphics, Me, imageRect)
      Me.Owner.Renderer.DrawItemImage(irea)
      
      ' If the item is on a drop-down, give its
      ' text a different highlighted color.
         Dim highlightColor As Color = CType(IIf(Me.IsOnDropDown, Color.Salmon, SystemColors.ControlLightLight), Color)
      
      ' Draw the text, and highlight it if the 
      ' the rollover state is true.
         Dim rea As New ToolStripItemTextRenderEventArgs( _
            e.Graphics, _
            Me, _
            MyBase.Text, _
            textRect, _
            CType(IIf(Me.rolloverValue, highlightColor, MyBase.ForeColor), Color), _
            MyBase.Font, _
            MyBase.TextAlign)
      Me.Owner.Renderer.DrawItemText(rea)
   End If
 End Sub

Комментарии

При возникновении события через делегат вызывается обработчик события.Raising an event invokes the event handler through a delegate. Дополнительные сведения см. в разделе обработка и вызов событий.For more information, see Handling and Raising Events.

Метод OnPaint также позволяет производным классам обрабатывать событие, не присоединяя делегат.The OnPaint 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.

Примечания для тех, кто наследует этот метод

При переопределении метода OnPaint(PaintEventArgs) в производном классе нужно убедиться, что вызывается метод OnPaint(PaintEventArgs) базового класса, чтобы зарегистрированные делегаты получили событие.When overriding OnPaint(PaintEventArgs) in a derived class, be sure to call the base class's OnPaint(PaintEventArgs) method so that registered delegates receive the event.

Применяется к