Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ToolStripItemRenderEventArgs Class

Provides data for the events that render the background of objects derived from ToolStripItem in the ToolStripRenderer class.

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

Public Class ToolStripItemRenderEventArgs _
	Inherits EventArgs

The ToolStripItemRenderEventArgs type exposes the following members.

Public methodToolStripItemRenderEventArgsInitializes a new instance of the ToolStripItemRenderEventArgs class for the specified ToolStripItem and using the specified Graphics.

Public propertyGraphicsGets the graphics used to paint the ToolStripItem.
Public propertyItemGets the ToolStripItem to paint.
Public propertyToolStripGets the value of the ToolStripItem.Owner property for the ToolStripItem to paint.

Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

The following code example demonstrates how to override the OnRenderButtonBackground method to draw a border around a ToolStripButton control's Image. This code example is part of a larger example provided for the ToolStripRenderer class.

  ' 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)

     ' 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)
            ' 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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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