ToolStripRenderer Klasa

Definicja

Obsługuje funkcje malowania obiektów ToolStrip .

public ref class ToolStripRenderer abstract
public abstract class ToolStripRenderer
type ToolStripRenderer = class
Public MustInherit Class ToolStripRenderer
Dziedziczenie
ToolStripRenderer
Pochodne

Przykłady

W poniższym przykładzie kodu pokazano, jak zaimplementować klasę niestandardową ToolStripRenderer . Klasa GridStripRenderer dostosowuje trzy aspekty wyglądu GridStrip kontrolki: GridStrip obramowanie, ToolStripButton obramowanie i ToolStripButton obraz. Aby uzyskać pełną listę kodu, zobacz How to: Implement a Custom ToolStripRenderer (Instrukcje: implementowanie niestandardowego elementu 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

Uwagi

ToolStripRenderer Użyj klasy , aby zastosować określony styl lub motyw do klasy ToolStrip. Zamiast malowania niestandardowego obiektu ToolStrip i ToolStripItem zawartych w nim obiektów należy ustawić ToolStrip.Renderer właściwość na obiekt dziedziczony z ToolStripRendererelementu . Obraz określony przez obiekt ToolStripRenderer jest stosowany do ToolStripelementu , a także elementów, które zawiera.

Niestandardowe malowanie można wykonywać na ToolStrip kilka sposobów. Podobnie jak w przypadku innych kontrolek Windows Forms, ToolStripToolStripItem obie te metody i mają metody i Paint zdarzenia, które można OnPaint zastąpić. Podobnie jak w przypadku zwykłego malowania, układ współrzędnych jest powiązany z obszarem klienta kontrolki; oznacza to, że lewy górny róg kontrolki to 0, 0. Zdarzenie Paint i OnPaint metoda dla zachowania ToolStripItem się jak inne zdarzenia farby sterującej.

Klasa ToolStripRenderer ma metody zastępowalne do malowania tła, tła elementu, obrazu elementu, strzałki elementu, tekstu elementu i obramowania ToolStripelementu . Argumenty zdarzeń dla tych metod uwidaczniają kilka właściwości, takich jak prostokąty, kolory i formaty tekstu, które można dostosować zgodnie z potrzebami.

Aby dostosować tylko kilka aspektów sposobu malowania elementu, zazwyczaj zastępujesz element ToolStripRenderer.

Jeśli piszesz nowy element i chcesz kontrolować wszystkie aspekty obrazu, przesłoń metodę OnPaint . Z poziomu OnPaintprogramu można użyć metod z klasy ToolStripRenderer.

Domyślnie element ToolStrip jest dwukrotnie buforowany, korzystając z OptimizedDoubleBuffer ustawienia .

Konstruktory

ToolStripRenderer()

Inicjuje nowe wystąpienie klasy ToolStripRenderer.

Pola

Offset2X

Pobiera lub ustawia mnożnik przesunięcia dla dwukrotnego przesunięcia wzdłuż osi x.

Offset2Y

Pobiera lub ustawia mnożnik przesunięcia dla dwukrotnego przesunięcia wzdłuż osi y.

Metody

CreateDisabledImage(Image)

Tworzy kopię obrazu w skali szarej.

DrawArrow(ToolStripArrowRenderEventArgs)

Rysuje strzałkę na obiekcie ToolStripItem.

DrawButtonBackground(ToolStripItemRenderEventArgs)

Rysuje tło dla .ToolStripButton

DrawDropDownButtonBackground(ToolStripItemRenderEventArgs)

Rysuje tło dla .ToolStripDropDownButton

DrawGrip(ToolStripGripRenderEventArgs)

Rysuje uchwyt przenoszenia na obiekcie ToolStrip.

DrawImageMargin(ToolStripRenderEventArgs)

Rysuje przestrzeń wokół obrazu na obiekcie ToolStrip.

DrawItemBackground(ToolStripItemRenderEventArgs)

Rysuje tło dla .ToolStripItem

DrawItemCheck(ToolStripItemImageRenderEventArgs)

Rysuje obraz na obiekcie ToolStripItem , który wskazuje, że element jest w wybranym stanie.

DrawItemImage(ToolStripItemImageRenderEventArgs)

Rysuje obraz na obiekcie ToolStripItem.

DrawItemText(ToolStripItemTextRenderEventArgs)

Rysuje tekst na obiekcie ToolStripItem.

DrawLabelBackground(ToolStripItemRenderEventArgs)

Rysuje tło dla .ToolStripLabel

DrawMenuItemBackground(ToolStripItemRenderEventArgs)

Rysuje tło dla .ToolStripMenuItem

DrawOverflowButtonBackground(ToolStripItemRenderEventArgs)

Rysuje tło przycisku przepełnienia.

DrawSeparator(ToolStripSeparatorRenderEventArgs)

Rysuje element ToolStripSeparator.

DrawSplitButton(ToolStripItemRenderEventArgs)

Rysuje element ToolStripSplitButton.

DrawStatusStripSizingGrip(ToolStripRenderEventArgs)

Rysuje uchwyt rozmiaru.

DrawToolStripBackground(ToolStripRenderEventArgs)

Rysuje tło dla .ToolStrip

DrawToolStripBorder(ToolStripRenderEventArgs)

Rysuje obramowanie dla obiektu ToolStrip.

DrawToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

Rysuje tło obiektu ToolStripContentPanel.

DrawToolStripPanelBackground(ToolStripPanelRenderEventArgs)

Rysuje tło obiektu ToolStripPanel.

DrawToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

Rysuje tło obiektu ToolStripStatusLabel.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
Initialize(ToolStrip)

Po przesłonięciu w klasie pochodnej program zapewnia inicjowanie niestandardowe danego elementu ToolStrip.

InitializeContentPanel(ToolStripContentPanel)

Inicjuje określony ToolStripContentPanelelement .

InitializeItem(ToolStripItem)

Po przesłonięciu w klasie pochodnej program zapewnia inicjowanie niestandardowe danego elementu ToolStripItem.

InitializePanel(ToolStripPanel)

Inicjuje określony ToolStripPanelelement .

MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
OnRenderArrow(ToolStripArrowRenderEventArgs)

RenderArrow Zgłasza zdarzenie.

OnRenderButtonBackground(ToolStripItemRenderEventArgs)

RenderButtonBackground Zgłasza zdarzenie.

OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs)

RenderDropDownButtonBackground Zgłasza zdarzenie.

OnRenderGrip(ToolStripGripRenderEventArgs)

RenderGrip Zgłasza zdarzenie.

OnRenderImageMargin(ToolStripRenderEventArgs)

Rysuje tło elementu.

OnRenderItemBackground(ToolStripItemRenderEventArgs)

OnRenderItemBackground(ToolStripItemRenderEventArgs) Zgłasza zdarzenie.

OnRenderItemCheck(ToolStripItemImageRenderEventArgs)

RenderItemCheck Zgłasza zdarzenie.

OnRenderItemImage(ToolStripItemImageRenderEventArgs)

RenderItemImage Zgłasza zdarzenie.

OnRenderItemText(ToolStripItemTextRenderEventArgs)

RenderItemText Zgłasza zdarzenie.

OnRenderLabelBackground(ToolStripItemRenderEventArgs)

RenderLabelBackground Zgłasza zdarzenie.

OnRenderMenuItemBackground(ToolStripItemRenderEventArgs)

RenderMenuItemBackground Zgłasza zdarzenie.

OnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs)

RenderOverflowButtonBackground Zgłasza zdarzenie.

OnRenderSeparator(ToolStripSeparatorRenderEventArgs)

RenderSeparator Zgłasza zdarzenie.

OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs)

OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs) Zgłasza zdarzenie.

OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs)

RenderStatusStripSizingGrip Zgłasza zdarzenie.

OnRenderToolStripBackground(ToolStripRenderEventArgs)

RenderToolStripBackground Zgłasza zdarzenie.

OnRenderToolStripBorder(ToolStripRenderEventArgs)

RenderToolStripBorder Zgłasza zdarzenie.

OnRenderToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

RenderToolStripContentPanelBackground Zgłasza zdarzenie.

OnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs)

RenderToolStripPanelBackground Zgłasza zdarzenie.

OnRenderToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

RenderToolStripStatusLabelBackground Zgłasza zdarzenie.

ScaleArrowOffsetsIfNeeded()

Offset2X Stosuje wartości i Offset2Y do skalowania ikony strzałki, jeśli skalowanie jest wymagane na podstawie ustawień DPI komputera.

ScaleArrowOffsetsIfNeeded(Int32)

Offset2X Stosuje wartości i Offset2Y do skalowania ikony strzałki na podstawie określonej wartości DPI.

ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Zdarzenia

RenderArrow

Występuje, gdy strzałka na obiekcie ToolStripItem jest renderowana.

RenderButtonBackground

Występuje, gdy tło dla wyrenderowane ToolStripButton jest.

RenderDropDownButtonBackground

Występuje, gdy tło dla wyrenderowane ToolStripDropDownButton jest.

RenderGrip

Występuje, gdy uchwyt przenoszenia dla wyrenderowany ToolStrip jest.

RenderImageMargin

Rysuje margines między obrazem a jego kontenerem.

RenderItemBackground

Występuje, gdy tło dla wyrenderowane ToolStripItem jest.

RenderItemCheck

Występuje, gdy obraz wybranego ToolStripItem jest renderowany.

RenderItemImage

Występuje, gdy obraz dla elementu ToolStripItem jest renderowany.

RenderItemText

Występuje, gdy tekst elementu ToolStripItem jest renderowany.

RenderLabelBackground

Występuje, gdy tło dla wyrenderowane ToolStripLabel jest.

RenderMenuItemBackground

Występuje, gdy tło dla wyrenderowane ToolStripMenuItem jest.

RenderOverflowButtonBackground

Występuje, gdy tło przycisku przepełnienia jest renderowane.

RenderSeparator

Występuje, gdy ToolStripSeparator element jest renderowany.

RenderSplitButtonBackground

Występuje, gdy tło dla wyrenderowane ToolStripSplitButton jest.

RenderStatusStripSizingGrip

Występuje, gdy zmieni się styl wyświetlania.

RenderToolStripBackground

Występuje, gdy tło dla wyrenderowane ToolStrip jest.

RenderToolStripBorder

Występuje, gdy obramowanie obiektu ToolStrip jest renderowane.

RenderToolStripContentPanelBackground

Rysuje tło obiektu ToolStripContentPanel.

RenderToolStripPanelBackground

Rysuje tło obiektu ToolStripPanel.

RenderToolStripStatusLabelBackground

Rysuje tło obiektu ToolStripStatusLabel.

Dotyczy

Zobacz też