Partager via


ToolStripRenderer Classe

Définition

Gère la fonctionnalité de peinture pour ToolStrip les objets.

public ref class ToolStripRenderer abstract
public abstract class ToolStripRenderer
type ToolStripRenderer = class
Public MustInherit Class ToolStripRenderer
Héritage
ToolStripRenderer
Dérivé

Exemples

L’exemple de code suivant montre comment implémenter une classe personnalisée ToolStripRenderer . La GridStripRenderer classe personnalise trois aspects de l’apparence GridStrip du contrôle : GridStrip bordure, ToolStripButton bordure et ToolStripButton image. Pour obtenir une description complète du code, consultez 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.
internal class GridStripRenderer : ToolStripRenderer
{   
    // The style of the empty cell's text.
    private static StringFormat style = new StringFormat();

    // The thickness (width or height) of a 
    // ToolStripButton control's border.
    static int borderThickness = 2;

    // The main bitmap that is the source for the 
    // subimagesthat are assigned to individual 
    // ToolStripButton controls.
    private Bitmap bmp = null;

    // The brush that paints the background of 
    // the GridStrip control.
    private Brush backgroundBrush = null;

    // This is the static constructor. It initializes the
    // StringFormat for drawing the text in the empty cell.
    static GridStripRenderer()
    {
        style.Alignment = StringAlignment.Center;
        style.LineAlignment = StringAlignment.Center;
    }

    // This method initializes the GridStripRenderer by
    // creating the image that is used as the source for
    // the individual button images.
    protected override void Initialize(ToolStrip ts)
    {
        base.Initialize(ts);

        this.InitializeBitmap(ts);
    }

    // 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 override void InitializeItem(ToolStripItem item)
    {
        base.InitializeItem(item);

        GridStrip gs = item.Owner as GridStrip;

        // The empty cell does not receive a subimage.
        if ((item is ToolStripButton) &&
            (item != gs.EmptyCell))
        {
            // Copy the subimage from the appropriate 
            // part of the main image.
            Bitmap subImage = bmp.Clone(
                item.Bounds,
                PixelFormat.Undefined);

            // Assign the subimage to the ToolStripButton
            // control's Image property.
            item.Image = subImage;
        }
    }

    // This utility method creates the main image that
    // is the source for the subimages of the individual 
    // ToolStripButton controls.
    private void InitializeBitmap(ToolStrip toolStrip)
    {
        // Create the main bitmap, into which the image is drawn.
        this.bmp = new Bitmap(
            toolStrip.Size.Width,
            toolStrip.Size.Height);

        // Draw a fancy pattern. This could be any image or drawing.
        using (Graphics g = Graphics.FromImage(bmp))
        {
            // Draw smoothed lines.
            g.SmoothingMode = SmoothingMode.AntiAlias;
            
            // Draw the image. In this case, it is 
            // a number of concentric ellipses. 
            for (int i = 0; i < toolStrip.Size.Width; i += 8)
            {
                g.DrawEllipse(Pens.Blue, 0, 0, i, i);
            }
        }
    }

    // This method draws a border around the GridStrip control.
    protected override void OnRenderToolStripBorder(
        ToolStripRenderEventArgs e)
    {
        base.OnRenderToolStripBorder(e);

        ControlPaint.DrawFocusRectangle(
            e.Graphics,
            e.AffectedBounds,
            SystemColors.ControlDarkDark,
            SystemColors.ControlDarkDark);
    }

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

Remarques

Utilisez la ToolStripRenderer classe pour appliquer un style ou un thème particulier à un ToolStrip. Au lieu de peindre personnalisé un ToolStrip et les ToolStripItem objets qu’il contient, vous définissez la ToolStrip.Renderer propriété sur un objet qui hérite de ToolStripRenderer. La peinture spécifiée par l’objet ToolStripRenderer est appliquée au ToolStrip, ainsi que les éléments qu’il contient.

Vous pouvez effectuer une peinture personnalisée de ToolStrip plusieurs façons. Comme avec d’autres contrôles Windows Forms, les ToolStripToolStripItem méthodes et Paint événements substituables sont tous deux substituablesOnPaint. Comme avec la peinture régulière, le système de coordonnées est relatif à la zone cliente du contrôle ; autrement dit, le coin supérieur gauche du contrôle est 0, 0. Événement Paint et OnPaint méthode pour un ToolStripItem comportement semblable à d’autres événements de peinture de contrôle.

La ToolStripRenderer classe a des méthodes substituables pour peindre l’arrière-plan, l’arrière-plan de l’élément, l’image de l’élément, la flèche d’élément, le texte de l’élément et la ToolStripbordure du . Les arguments d’événement de ces méthodes exposent plusieurs propriétés telles que des rectangles, des couleurs et des formats de texte que vous pouvez ajuster selon vos besoins.

Pour ajuster seulement quelques aspects de la façon dont un élément est peint, vous remplacez généralement le ToolStripRenderer.

Si vous écrivez un nouvel élément et que vous souhaitez contrôler tous les aspects de la peinture, remplacez la OnPaint méthode. À partir de l’intérieur OnPaint, vous pouvez utiliser des méthodes à partir du ToolStripRenderer.

Par défaut, il ToolStrip est double mis en mémoire tampon, tirant parti du OptimizedDoubleBuffer paramètre.

Constructeurs

Nom Description
ToolStripRenderer()

Initialise une nouvelle instance de la classe ToolStripRenderer.

Champs

Nom Description
Offset2X

Obtient ou définit le multiplicateur de décalage pour deux fois le décalage le long de l’axe x.

Offset2Y

Obtient ou définit le multiplicateur de décalage pour deux fois le décalage le long de l’axe y.

Méthodes

Nom Description
CreateDisabledImage(Image)

Crée une copie à l’échelle grise d’une image donnée.

DrawArrow(ToolStripArrowRenderEventArgs)

Dessine une flèche sur un ToolStripItem.

DrawButtonBackground(ToolStripItemRenderEventArgs)

Dessine l’arrière-plan d’un ToolStripButton.

DrawDropDownButtonBackground(ToolStripItemRenderEventArgs)

Dessine l’arrière-plan d’un ToolStripDropDownButton.

DrawGrip(ToolStripGripRenderEventArgs)

Dessine une poignée de déplacement sur un ToolStrip.

DrawImageMargin(ToolStripRenderEventArgs)

Dessine l’espace autour d’une image sur un ToolStrip.

DrawItemBackground(ToolStripItemRenderEventArgs)

Dessine l’arrière-plan d’un ToolStripItem.

DrawItemCheck(ToolStripItemImageRenderEventArgs)

Dessine une image sur un ToolStripItem élément qui indique que l’élément est dans un état sélectionné.

DrawItemImage(ToolStripItemImageRenderEventArgs)

Dessine une image sur un ToolStripItem.

DrawItemText(ToolStripItemTextRenderEventArgs)

Dessine du texte sur un ToolStripItem.

DrawLabelBackground(ToolStripItemRenderEventArgs)

Dessine l’arrière-plan d’un ToolStripLabel.

DrawMenuItemBackground(ToolStripItemRenderEventArgs)

Dessine l’arrière-plan d’un ToolStripMenuItem.

DrawOverflowButtonBackground(ToolStripItemRenderEventArgs)

Dessine l’arrière-plan d’un bouton de dépassement de capacité.

DrawSeparator(ToolStripSeparatorRenderEventArgs)

Dessine un ToolStripSeparator.

DrawSplitButton(ToolStripItemRenderEventArgs)

Dessine un ToolStripSplitButton.

DrawStatusStripSizingGrip(ToolStripRenderEventArgs)

Dessine une poignée de dimensionnement.

DrawToolStripBackground(ToolStripRenderEventArgs)

Dessine l’arrière-plan d’un ToolStrip.

DrawToolStripBorder(ToolStripRenderEventArgs)

Dessine la bordure d’un ToolStrip.

DrawToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

Dessine l’arrière-plan du ToolStripContentPanel.

DrawToolStripPanelBackground(ToolStripPanelRenderEventArgs)

Dessine l’arrière-plan du ToolStripPanel.

DrawToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

Dessine l’arrière-plan du ToolStripStatusLabel.

Equals(Object)

Détermine si l’objet spécifié est égal à l’objet actuel.

(Hérité de Object)
GetHashCode()

Sert de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient la Type de l’instance actuelle.

(Hérité de Object)
Initialize(ToolStrip)

En cas de substitution dans une classe dérivée, fournit l’initialisation personnalisée de l’élément donné ToolStrip.

InitializeContentPanel(ToolStripContentPanel)

Initialise le ToolStripContentPanel.

InitializeItem(ToolStripItem)

En cas de substitution dans une classe dérivée, fournit l’initialisation personnalisée de l’élément donné ToolStripItem.

InitializePanel(ToolStripPanel)

Initialise le ToolStripPanel.

MemberwiseClone()

Crée une copie superficielle du Objectactuel.

(Hérité de Object)
OnRenderArrow(ToolStripArrowRenderEventArgs)

Déclenche l’événement RenderArrow.

OnRenderButtonBackground(ToolStripItemRenderEventArgs)

Déclenche l’événement RenderButtonBackground.

OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs)

Déclenche l’événement RenderDropDownButtonBackground.

OnRenderGrip(ToolStripGripRenderEventArgs)

Déclenche l’événement RenderGrip.

OnRenderImageMargin(ToolStripRenderEventArgs)

Dessine l’arrière-plan de l’élément.

OnRenderItemBackground(ToolStripItemRenderEventArgs)

Déclenche l’événement OnRenderItemBackground(ToolStripItemRenderEventArgs).

OnRenderItemCheck(ToolStripItemImageRenderEventArgs)

Déclenche l’événement RenderItemCheck.

OnRenderItemImage(ToolStripItemImageRenderEventArgs)

Déclenche l’événement RenderItemImage.

OnRenderItemText(ToolStripItemTextRenderEventArgs)

Déclenche l’événement RenderItemText.

OnRenderLabelBackground(ToolStripItemRenderEventArgs)

Déclenche l’événement RenderLabelBackground.

OnRenderMenuItemBackground(ToolStripItemRenderEventArgs)

Déclenche l’événement RenderMenuItemBackground.

OnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs)

Déclenche l’événement RenderOverflowButtonBackground.

OnRenderSeparator(ToolStripSeparatorRenderEventArgs)

Déclenche l’événement RenderSeparator.

OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs)

Déclenche l’événement OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs).

OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs)

Déclenche l’événement RenderStatusStripSizingGrip.

OnRenderToolStripBackground(ToolStripRenderEventArgs)

Déclenche l’événement RenderToolStripBackground.

OnRenderToolStripBorder(ToolStripRenderEventArgs)

Déclenche l’événement RenderToolStripBorder.

OnRenderToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

Déclenche l’événement RenderToolStripContentPanelBackground.

OnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs)

Déclenche l’événement RenderToolStripPanelBackground.

OnRenderToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

Déclenche l’événement RenderToolStripStatusLabelBackground.

ScaleArrowOffsetsIfNeeded()

Applique les valeurs et Offset2Y les Offset2X valeurs à la mise à l’échelle de l’icône de flèche, si la mise à l’échelle est requise en fonction des paramètres PPP de l’ordinateur.

ScaleArrowOffsetsIfNeeded(Int32)

Applique les valeurs et Offset2Y les valeurs à la Offset2X mise à l’échelle de l’icône de flèche en fonction de la valeur DPI spécifiée.

ToString()

Retourne une chaîne qui représente l’objet actuel.

(Hérité de Object)

Événements

Nom Description
RenderArrow

Se produit lorsqu’une flèche est ToolStripItem affichée.

RenderButtonBackground

Se produit lorsque l’arrière-plan d’un ToolStripButton est rendu.

RenderDropDownButtonBackground

Se produit lorsque l’arrière-plan d’un ToolStripDropDownButton est rendu.

RenderGrip

Se produit lorsque le handle de déplacement d’un ToolStrip élément est rendu.

RenderImageMargin

Dessine la marge entre une image et son conteneur.

RenderItemBackground

Se produit lorsque l’arrière-plan d’un ToolStripItem est rendu.

RenderItemCheck

Se produit lorsque l’image d’une sélection ToolStripItem est rendue.

RenderItemImage

Se produit lorsque l’image d’un ToolStripItem est rendue.

RenderItemText

Se produit lorsque le texte d’un objet ToolStripItem est rendu.

RenderLabelBackground

Se produit lorsque l’arrière-plan d’un ToolStripLabel est rendu.

RenderMenuItemBackground

Se produit lorsque l’arrière-plan d’un ToolStripMenuItem est rendu.

RenderOverflowButtonBackground

Se produit lorsque l’arrière-plan d’un bouton de dépassement de capacité est rendu.

RenderSeparator

Se produit lorsqu’un ToolStripSeparator rendu est effectué.

RenderSplitButtonBackground

Se produit lorsque l’arrière-plan d’un ToolStripSplitButton est rendu.

RenderStatusStripSizingGrip

Se produit lorsque le style d’affichage change.

RenderToolStripBackground

Se produit lorsque l’arrière-plan d’un ToolStrip est rendu.

RenderToolStripBorder

Se produit lorsque la bordure d’un est ToolStrip rendue.

RenderToolStripContentPanelBackground

Dessine l’arrière-plan d’un ToolStripContentPanel.

RenderToolStripPanelBackground

Dessine l’arrière-plan d’un ToolStripPanel.

RenderToolStripStatusLabelBackground

Dessine l’arrière-plan d’un ToolStripStatusLabel.

S’applique à

Voir aussi