Condividi tramite


ToolStripRenderer Classe

Definizione

Gestisce la funzionalità di disegno per ToolStrip gli oggetti.

public ref class ToolStripRenderer abstract
public abstract class ToolStripRenderer
type ToolStripRenderer = class
Public MustInherit Class ToolStripRenderer
Ereditarietà
ToolStripRenderer
Derivato

Esempio

Nell'esempio di codice seguente viene illustrato come implementare una classe personalizzata ToolStripRenderer . La GridStripRenderer classe personalizza tre aspetti dell'aspetto GridStrip del controllo: GridStrip bordo, ToolStripButton bordo e ToolStripButton immagine. Per un elenco di codice completo, vedere Procedura: Implementare un toolStripRenderer personalizzato.

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

Commenti

Utilizzare la ToolStripRenderer classe per applicare uno stile o un tema specifico a un oggetto ToolStrip. Anziché disegnare un ToolStrip oggetto e gli ToolStripItem oggetti contenuti, impostare la ToolStrip.Renderer proprietà su un oggetto che eredita da ToolStripRenderer. Il disegno specificato da viene applicato all'oggetto ToolStripRendererToolStrip, nonché agli elementi in esso contenuti.

È possibile eseguire un disegno personalizzato nei ToolStrip controlli in diversi modi. Come con altri controlli Windows Form, e dispongono entrambi di metodi ed eventi di cui è Paint possibile eseguire l'override ToolStripOnPaint.ToolStripItem Come per la pittura regolare, il sistema di coordinate è relativo all'area client del controllo; ovvero, l'angolo superiore sinistro del controllo è 0, 0. L'evento e OnPaint il Paint metodo per un ToolStripItem comportamento simile ad altri eventi di disegno del controllo.

La ToolStripRenderer classe dispone di metodi sottoponibili a override per disegnare lo sfondo, lo sfondo dell'elemento, l'immagine dell'elemento, la freccia dell'elemento, il testo dell'elemento e il bordo dell'oggetto ToolStrip. Gli argomenti dell'evento per questi metodi espongono diverse proprietà, ad esempio rettangoli, colori e formati di testo che è possibile modificare in base alle esigenze.

Per regolare solo alcuni aspetti della modalità di disegno di un elemento, in genere si esegue l'override di ToolStripRenderer.

Se si scrive un nuovo elemento e si desidera controllare tutti gli aspetti del disegno, eseguire l'override del OnPaint metodo . Dall'interno OnPaintdi è possibile usare i metodi di ToolStripRenderer.

Per impostazione predefinita, l'oggetto ToolStrip viene memorizzato nel doppio buffer, sfruttando l'impostazione OptimizedDoubleBuffer .

Costruttori

Nome Descrizione
ToolStripRenderer()

Inizializza una nuova istanza della classe ToolStripRenderer.

Campi

Nome Descrizione
Offset2X

Ottiene o imposta il moltiplicatore di offset per due volte l'offset lungo l'asse x.

Offset2Y

Ottiene o imposta il moltiplicatore di offset per due volte l'offset lungo l'asse y.

Metodi

Nome Descrizione
CreateDisabledImage(Image)

Crea una copia in scala di grigi di una determinata immagine.

DrawArrow(ToolStripArrowRenderEventArgs)

Disegna una freccia su un oggetto ToolStripItem.

DrawButtonBackground(ToolStripItemRenderEventArgs)

Disegna lo sfondo per un oggetto ToolStripButton.

DrawDropDownButtonBackground(ToolStripItemRenderEventArgs)

Disegna lo sfondo per un oggetto ToolStripDropDownButton.

DrawGrip(ToolStripGripRenderEventArgs)

Disegna un handle di spostamento su un oggetto ToolStrip.

DrawImageMargin(ToolStripRenderEventArgs)

Disegna lo spazio intorno a un'immagine in un oggetto ToolStrip.

DrawItemBackground(ToolStripItemRenderEventArgs)

Disegna lo sfondo per un oggetto ToolStripItem.

DrawItemCheck(ToolStripItemImageRenderEventArgs)

Disegna un'immagine su un ToolStripItem oggetto che indica che l'elemento è in uno stato selezionato.

DrawItemImage(ToolStripItemImageRenderEventArgs)

Disegna un'immagine su un oggetto ToolStripItem.

DrawItemText(ToolStripItemTextRenderEventArgs)

Disegna testo su un oggetto ToolStripItem.

DrawLabelBackground(ToolStripItemRenderEventArgs)

Disegna lo sfondo per un oggetto ToolStripLabel.

DrawMenuItemBackground(ToolStripItemRenderEventArgs)

Disegna lo sfondo per un oggetto ToolStripMenuItem.

DrawOverflowButtonBackground(ToolStripItemRenderEventArgs)

Disegna lo sfondo per un pulsante di overflow.

DrawSeparator(ToolStripSeparatorRenderEventArgs)

Disegna un oggetto ToolStripSeparator.

DrawSplitButton(ToolStripItemRenderEventArgs)

Disegna un oggetto ToolStripSplitButton.

DrawStatusStripSizingGrip(ToolStripRenderEventArgs)

Disegna un grip di ridimensionamento.

DrawToolStripBackground(ToolStripRenderEventArgs)

Disegna lo sfondo per un oggetto ToolStrip.

DrawToolStripBorder(ToolStripRenderEventArgs)

Disegna il bordo per un oggetto ToolStrip.

DrawToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

Disegna lo sfondo dell'oggetto ToolStripContentPanel.

DrawToolStripPanelBackground(ToolStripPanelRenderEventArgs)

Disegna lo sfondo dell'oggetto ToolStripPanel.

DrawToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

Disegna lo sfondo dell'oggetto ToolStripStatusLabel.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
Initialize(ToolStrip)

Quando sottoposto a override in una classe derivata, fornisce l'inizializzazione personalizzata dell'oggetto specificato ToolStrip.

InitializeContentPanel(ToolStripContentPanel)

Inizializza l'oggetto specificato ToolStripContentPanel.

InitializeItem(ToolStripItem)

Quando sottoposto a override in una classe derivata, fornisce l'inizializzazione personalizzata dell'oggetto specificato ToolStripItem.

InitializePanel(ToolStripPanel)

Inizializza l'oggetto specificato ToolStripPanel.

MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
OnRenderArrow(ToolStripArrowRenderEventArgs)

Genera l'evento RenderArrow.

OnRenderButtonBackground(ToolStripItemRenderEventArgs)

Genera l'evento RenderButtonBackground.

OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs)

Genera l'evento RenderDropDownButtonBackground.

OnRenderGrip(ToolStripGripRenderEventArgs)

Genera l'evento RenderGrip.

OnRenderImageMargin(ToolStripRenderEventArgs)

Disegna lo sfondo dell'elemento.

OnRenderItemBackground(ToolStripItemRenderEventArgs)

Genera l'evento OnRenderItemBackground(ToolStripItemRenderEventArgs).

OnRenderItemCheck(ToolStripItemImageRenderEventArgs)

Genera l'evento RenderItemCheck.

OnRenderItemImage(ToolStripItemImageRenderEventArgs)

Genera l'evento RenderItemImage.

OnRenderItemText(ToolStripItemTextRenderEventArgs)

Genera l'evento RenderItemText.

OnRenderLabelBackground(ToolStripItemRenderEventArgs)

Genera l'evento RenderLabelBackground.

OnRenderMenuItemBackground(ToolStripItemRenderEventArgs)

Genera l'evento RenderMenuItemBackground.

OnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs)

Genera l'evento RenderOverflowButtonBackground.

OnRenderSeparator(ToolStripSeparatorRenderEventArgs)

Genera l'evento RenderSeparator.

OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs)

Genera l'evento OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs).

OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs)

Genera l'evento RenderStatusStripSizingGrip.

OnRenderToolStripBackground(ToolStripRenderEventArgs)

Genera l'evento RenderToolStripBackground.

OnRenderToolStripBorder(ToolStripRenderEventArgs)

Genera l'evento RenderToolStripBorder.

OnRenderToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

Genera l'evento RenderToolStripContentPanelBackground.

OnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs)

Genera l'evento RenderToolStripPanelBackground.

OnRenderToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

Genera l'evento RenderToolStripStatusLabelBackground.

ScaleArrowOffsetsIfNeeded()

Applica i Offset2X valori e Offset2Y al ridimensionamento dell'icona freccia, se è necessario ridimensionare in base alle impostazioni DPI del computer.

ScaleArrowOffsetsIfNeeded(Int32)

Applica i Offset2X valori e Offset2Y al ridimensionamento dell'icona freccia in base al valore DPI specificato.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Eventi

Nome Descrizione
RenderArrow

Si verifica quando viene eseguito il rendering di una freccia su un ToolStripItem oggetto .

RenderButtonBackground

Si verifica quando viene eseguito il rendering dello sfondo di un oggetto ToolStripButton .

RenderDropDownButtonBackground

Si verifica quando viene eseguito il rendering dello sfondo di un oggetto ToolStripDropDownButton .

RenderGrip

Si verifica quando viene eseguito il rendering dell'handle di spostamento per un oggetto ToolStrip .

RenderImageMargin

Disegna il margine tra un'immagine e il relativo contenitore.

RenderItemBackground

Si verifica quando viene eseguito il rendering dello sfondo di un oggetto ToolStripItem .

RenderItemCheck

Si verifica quando viene eseguito il rendering dell'immagine per un oggetto selezionato ToolStripItem .

RenderItemImage

Si verifica quando viene eseguito il rendering dell'immagine per un oggetto ToolStripItem .

RenderItemText

Si verifica quando viene eseguito il rendering del testo per un oggetto ToolStripItem .

RenderLabelBackground

Si verifica quando viene eseguito il rendering dello sfondo di un oggetto ToolStripLabel .

RenderMenuItemBackground

Si verifica quando viene eseguito il rendering dello sfondo di un oggetto ToolStripMenuItem .

RenderOverflowButtonBackground

Si verifica quando viene eseguito il rendering dello sfondo di un pulsante di overflow.

RenderSeparator

Si verifica quando viene eseguito il rendering di un oggetto ToolStripSeparator .

RenderSplitButtonBackground

Si verifica quando viene eseguito il rendering dello sfondo di un oggetto ToolStripSplitButton .

RenderStatusStripSizingGrip

Si verifica quando lo stile di visualizzazione cambia.

RenderToolStripBackground

Si verifica quando viene eseguito il rendering dello sfondo di un oggetto ToolStrip .

RenderToolStripBorder

Si verifica quando viene eseguito il rendering del bordo di un oggetto ToolStrip .

RenderToolStripContentPanelBackground

Disegna lo sfondo di un oggetto ToolStripContentPanel.

RenderToolStripPanelBackground

Disegna lo sfondo di un oggetto ToolStripPanel.

RenderToolStripStatusLabelBackground

Disegna lo sfondo di un oggetto ToolStripStatusLabel.

Si applica a

Vedi anche