Export (0) Print
Expand All

ToolStripRenderer Class

Handles the painting functionality for ToolStrip objects.

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

'Declaration
Public MustInherit Class ToolStripRenderer

The ToolStripRenderer type exposes the following members.

  NameDescription
Protected methodToolStripRendererInitializes a new instance of the ToolStripRenderer class.
Top

  NameDescription
Public methodStatic memberCreateDisabledImageCreates a gray-scale copy of a given image.
Public methodDrawArrowDraws an arrow on a ToolStripItem.
Public methodDrawButtonBackgroundDraws the background for a ToolStripButton.
Public methodDrawDropDownButtonBackgroundDraws the background for a ToolStripDropDownButton.
Public methodDrawGripDraws a move handle on a ToolStrip.
Public methodDrawImageMarginDraws the space around an image on a ToolStrip.
Public methodDrawItemBackgroundDraws the background for a ToolStripItem.
Public methodDrawItemCheckDraws an image on a ToolStripItem that indicates the item is in a selected state.
Public methodDrawItemImageDraws an image on a ToolStripItem.
Public methodDrawItemTextDraws text on a ToolStripItem.
Public methodDrawLabelBackgroundDraws the background for a ToolStripLabel.
Public methodDrawMenuItemBackgroundDraws the background for a ToolStripMenuItem.
Public methodDrawOverflowButtonBackgroundDraws the background for an overflow button.
Public methodDrawSeparatorDraws a ToolStripSeparator.
Public methodDrawSplitButtonDraws a ToolStripSplitButton.
Public methodDrawStatusStripSizingGripDraws a sizing grip.
Public methodDrawToolStripBackgroundDraws the background for a ToolStrip.
Public methodDrawToolStripBorderDraws the border for a ToolStrip.
Public methodDrawToolStripContentPanelBackgroundDraws the background of the ToolStripContentPanel.
Public methodDrawToolStripPanelBackgroundDraws the background of the ToolStripPanel.
Public methodDrawToolStripStatusLabelBackgroundDraws the background of the ToolStripStatusLabel.
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 a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodInitializeWhen overridden in a derived class, provides for custom initialization of the given ToolStrip.
Protected methodInitializeContentPanelInitializes the specified ToolStripContentPanel.
Protected methodInitializeItemWhen overridden in a derived class, provides for custom initialization of the given ToolStripItem.
Protected methodInitializePanelInitializes the specified ToolStripPanel.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnRenderArrowRaises the RenderArrow event.
Protected methodOnRenderButtonBackgroundRaises the RenderButtonBackground event.
Protected methodOnRenderDropDownButtonBackgroundRaises the RenderDropDownButtonBackground event.
Protected methodOnRenderGripRaises the RenderGrip event.
Protected methodOnRenderImageMarginDraws the item background.
Protected methodOnRenderItemBackgroundRaises the OnRenderItemBackground event.
Protected methodOnRenderItemCheckRaises the RenderItemCheck event.
Protected methodOnRenderItemImageRaises the RenderItemImage event.
Protected methodOnRenderItemTextRaises the RenderItemText event.
Protected methodOnRenderLabelBackgroundRaises the RenderLabelBackground event.
Protected methodOnRenderMenuItemBackgroundRaises the RenderMenuItemBackground event.
Protected methodOnRenderOverflowButtonBackgroundRaises the RenderOverflowButtonBackground event.
Protected methodOnRenderSeparatorRaises the RenderSeparator event.
Protected methodOnRenderSplitButtonBackgroundRaises the OnRenderSplitButtonBackground event.
Protected methodOnRenderStatusStripSizingGripRaises the RenderStatusStripSizingGrip event.
Protected methodOnRenderToolStripBackgroundRaises the RenderToolStripBackground event.
Protected methodOnRenderToolStripBorderRaises the RenderToolStripBorder event.
Protected methodOnRenderToolStripContentPanelBackgroundRaises the RenderToolStripContentPanelBackground event.
Protected methodOnRenderToolStripPanelBackgroundRaises the RenderToolStripPanelBackground event.
Protected methodOnRenderToolStripStatusLabelBackgroundRaises the RenderToolStripStatusLabelBackground event.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventRenderArrowOccurs when an arrow on a ToolStripItem is rendered.
Public eventRenderButtonBackgroundOccurs when the background for a ToolStripButton is rendered
Public eventRenderDropDownButtonBackgroundOccurs when the background for a ToolStripDropDownButton is rendered.
Public eventRenderGripOccurs when the move handle for a ToolStrip is rendered.
Public eventRenderImageMarginDraws the margin between an image and its container.
Public eventRenderItemBackgroundOccurs when the background for a ToolStripItem is rendered.
Public eventRenderItemCheckOccurs when the image for a selected ToolStripItem is rendered.
Public eventRenderItemImageOccurs when the image for a ToolStripItem is rendered.
Public eventRenderItemTextOccurs when the text for a ToolStripItem is rendered.
Public eventRenderLabelBackgroundOccurs when the background for a ToolStripLabel is rendered.
Public eventRenderMenuItemBackgroundOccurs when the background for a ToolStripMenuItem is rendered.
Public eventRenderOverflowButtonBackgroundOccurs when the background for an overflow button is rendered.
Public eventRenderSeparatorOccurs when a ToolStripSeparator is rendered.
Public eventRenderSplitButtonBackgroundOccurs when the background for a ToolStripSplitButton is rendered.
Public eventRenderStatusStripSizingGripOccurs when the display style changes.
Public eventRenderToolStripBackgroundOccurs when the background for a ToolStrip is rendered.
Public eventRenderToolStripBorderOccurs when the border for a ToolStrip is rendered.
Public eventRenderToolStripContentPanelBackgroundDraws the background of a ToolStripContentPanel.
Public eventRenderToolStripPanelBackgroundDraws the background of a ToolStripPanel.
Public eventRenderToolStripStatusLabelBackgroundDraws the background of a ToolStripStatusLabel.
Top

Use the ToolStripRenderer class to apply a particular style or theme to a ToolStrip. Rather than custom painting a ToolStrip and the ToolStripItem objects it contains, you set the ToolStrip.Renderer property to an object that inherits from ToolStripRenderer. The painting specified by the ToolStripRenderer is applied to the ToolStrip, as well as the items it contains.

You can do custom painting in ToolStrip controls in several ways. As with other Windows Forms controls, the ToolStrip and ToolStripItem both have overridable OnPaint methods and Paint events. As with regular painting, the coordinate system is relative to the client area of the control; that is, the upper left-hand corner of the control is 0, 0. The Paint event and OnPaint method for a ToolStripItem behave like other control paint events.

The ToolStripRenderer class has overridable methods for painting the background, item background, item image, item arrow, item text, and border of the ToolStrip. The event arguments for these methods expose several properties such as rectangles, colors, and text formats that you can adjust as desired.

To adjust just a few aspects of how an item is painted, you typically override the ToolStripRenderer.

If you are writing a new item and want to control all aspects of the painting, override the OnPaint method. From within OnPaint, you can use methods from the ToolStripRenderer.

By default, the ToolStrip is double buffered, taking advantage of the OptimizedDoubleBuffer setting.

The following code example demonstrates how to implement a custom ToolStripRenderer class. The GridStripRenderer class customizes three aspects of the GridStrip control's appearance: GridStrip border, ToolStripButton border, and ToolStripButton image. For a full code listing, see How to: Implement a Custom 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.
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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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

Community Additions

ADD
Show:
© 2014 Microsoft