ToolStripRenderer Třída

Definice

Zpracovává funkci malování objektů ToolStrip .

public ref class ToolStripRenderer abstract
public abstract class ToolStripRenderer
type ToolStripRenderer = class
Public MustInherit Class ToolStripRenderer
Dědičnost
ToolStripRenderer
Odvozené

Příklady

Následující příklad kódu ukazuje, jak implementovat vlastní ToolStripRenderer třídu. Třída GridStripRenderer přizpůsobí tři aspekty GridStrip vzhledu ovládacího prvku: GridStrip ohraničení, ToolStripButton ohraničení a ToolStripButton obrázek. Úplný výpis kódu najdete v tématu Postupy: Implementace vlastního 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

Poznámky

ToolStripRenderer Použijte třídu k použití určitého stylu nebo motivu na objekt .ToolStrip Místo vlastního malování objektu ToolStrip a ToolStripItem objektů, které obsahuje, nastavíte ToolStrip.Renderer vlastnost na objekt, který dědí z ToolStripRendererobjektu . Obraz určený objektem ToolStripRenderer se použije na ToolStripobjekt a také na položky, které obsahuje.

Vlastní malování v ToolStrip ovládacích prvcích můžete provádět několika způsoby. Stejně jako u jiných ovládacích prvků model Windows Forms i oba ToolStripToolStripItem mají přepisovatelné OnPaint metody a Paint události. Stejně jako u běžné malby je souřadnicový systém relativní k klientské oblasti ovládacího prvku; to znamená, že levý horní roh ovládacího prvku je 0, 0. Událost Paint a OnPaint metoda chování ToolStripItem jako jiné ovládací prvky malování události.

Třída ToolStripRenderer obsahuje přepisovatelné metody pro malování pozadí, pozadí položky, obrázku položky, šipky položky, textu položky a ohraničení objektu ToolStrip. Argumenty událostí pro tyto metody zpřístupňují několik vlastností, jako jsou obdélníky, barvy a textové formáty, které můžete upravit podle potřeby.

Pokud chcete upravit jenom několik aspektů způsobu malování položky, obvykle přepíšete ToolStripRenderer.

Pokud píšete novou položku a chcete mít kontrolu nad všemi aspekty malby, přepište metodu OnPaint . V rámci OnPaintmůžete použít metody z .ToolStripRenderer

Ve výchozím nastavení ToolStrip je dvojitá vyrovnávací paměť, s využitím OptimizedDoubleBuffer tohoto nastavení.

Konstruktory

ToolStripRenderer()

Inicializuje novou instanci ToolStripRenderer třídy .

Pole

Offset2X

Získá nebo nastaví násobitel posunu pro dvojnásobek posunu podél osy x.

Offset2Y

Získá nebo nastaví násobitel posunu pro dvojnásobek posunu podél osy y.

Metody

CreateDisabledImage(Image)

Vytvoří kopii daného obrázku v šedém měřítku.

DrawArrow(ToolStripArrowRenderEventArgs)

Nakreslí šipku ToolStripItemna .

DrawButtonBackground(ToolStripItemRenderEventArgs)

Nakreslí pozadí objektu ToolStripButton.

DrawDropDownButtonBackground(ToolStripItemRenderEventArgs)

Nakreslí pozadí objektu ToolStripDropDownButton.

DrawGrip(ToolStripGripRenderEventArgs)

Nakreslí úchyt pro přesunutí na ToolStrip.

DrawImageMargin(ToolStripRenderEventArgs)

Nakreslí prostor kolem obrázku na objektu ToolStrip.

DrawItemBackground(ToolStripItemRenderEventArgs)

Nakreslí pozadí objektu ToolStripItem.

DrawItemCheck(ToolStripItemImageRenderEventArgs)

Nakreslí obrázek na ToolStripItem objekt, který označuje, že položka je ve vybraném stavu.

DrawItemImage(ToolStripItemImageRenderEventArgs)

Nakreslí obrázek na objekt .ToolStripItem

DrawItemText(ToolStripItemTextRenderEventArgs)

Nakreslí text na ToolStripItem.

DrawLabelBackground(ToolStripItemRenderEventArgs)

Nakreslí pozadí objektu ToolStripLabel.

DrawMenuItemBackground(ToolStripItemRenderEventArgs)

Nakreslí pozadí objektu ToolStripMenuItem.

DrawOverflowButtonBackground(ToolStripItemRenderEventArgs)

Nakreslí pozadí tlačítka přetečení.

DrawSeparator(ToolStripSeparatorRenderEventArgs)

Nakreslí .ToolStripSeparator

DrawSplitButton(ToolStripItemRenderEventArgs)

Nakreslí .ToolStripSplitButton

DrawStatusStripSizingGrip(ToolStripRenderEventArgs)

Nakreslí úchyt pro změnu velikosti.

DrawToolStripBackground(ToolStripRenderEventArgs)

Nakreslí pozadí objektu ToolStrip.

DrawToolStripBorder(ToolStripRenderEventArgs)

Nakreslí ohraničení pro ToolStrip.

DrawToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

Nakreslí pozadí objektu ToolStripContentPanel.

DrawToolStripPanelBackground(ToolStripPanelRenderEventArgs)

Nakreslí pozadí objektu ToolStripPanel.

DrawToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

Nakreslí pozadí objektu ToolStripStatusLabel.

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí hashovací funkce.

(Zděděno od Object)
GetType()

Type Získá z aktuální instance.

(Zděděno od Object)
Initialize(ToolStrip)

Při přepsání v odvozené třídě poskytuje vlastní inicializaci daného ToolStripobjektu .

InitializeContentPanel(ToolStripContentPanel)

Inicializuje zadaný parametr ToolStripContentPanel.

InitializeItem(ToolStripItem)

Při přepsání v odvozené třídě poskytuje vlastní inicializaci daného ToolStripItemobjektu .

InitializePanel(ToolStripPanel)

Inicializuje zadaný parametr ToolStripPanel.

MemberwiseClone()

Vytvoří mělkou kopii aktuálního Objectsouboru .

(Zděděno od Object)
OnRenderArrow(ToolStripArrowRenderEventArgs)

RenderArrow Vyvolá událost.

OnRenderButtonBackground(ToolStripItemRenderEventArgs)

RenderButtonBackground Vyvolá událost.

OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs)

RenderDropDownButtonBackground Vyvolá událost.

OnRenderGrip(ToolStripGripRenderEventArgs)

RenderGrip Vyvolá událost.

OnRenderImageMargin(ToolStripRenderEventArgs)

Nakreslí pozadí položky.

OnRenderItemBackground(ToolStripItemRenderEventArgs)

OnRenderItemBackground(ToolStripItemRenderEventArgs) Vyvolá událost.

OnRenderItemCheck(ToolStripItemImageRenderEventArgs)

RenderItemCheck Vyvolá událost.

OnRenderItemImage(ToolStripItemImageRenderEventArgs)

RenderItemImage Vyvolá událost.

OnRenderItemText(ToolStripItemTextRenderEventArgs)

RenderItemText Vyvolá událost.

OnRenderLabelBackground(ToolStripItemRenderEventArgs)

RenderLabelBackground Vyvolá událost.

OnRenderMenuItemBackground(ToolStripItemRenderEventArgs)

RenderMenuItemBackground Vyvolá událost.

OnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs)

RenderOverflowButtonBackground Vyvolá událost.

OnRenderSeparator(ToolStripSeparatorRenderEventArgs)

RenderSeparator Vyvolá událost.

OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs)

OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs) Vyvolá událost.

OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs)

RenderStatusStripSizingGrip Vyvolá událost.

OnRenderToolStripBackground(ToolStripRenderEventArgs)

RenderToolStripBackground Vyvolá událost.

OnRenderToolStripBorder(ToolStripRenderEventArgs)

RenderToolStripBorder Vyvolá událost.

OnRenderToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

RenderToolStripContentPanelBackground Vyvolá událost.

OnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs)

RenderToolStripPanelBackground Vyvolá událost.

OnRenderToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

RenderToolStripStatusLabelBackground Vyvolá událost.

ScaleArrowOffsetsIfNeeded()

Offset2X Použije hodnoty a Offset2Y pro změnu velikosti ikony šipky, pokud je požadováno měřítko na základě nastavení DPI počítače.

ScaleArrowOffsetsIfNeeded(Int32)

Offset2X Použije hodnoty a Offset2Y ke změně velikosti ikony šipky na základě zadané hodnoty DPI.

ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Událost

RenderArrow

Vyvolá se při vykreslení šipky na objektu ToolStripItem .

RenderButtonBackground

Vyvolá se při vykreslení pozadí objektu ToolStripButton .

RenderDropDownButtonBackground

Vyvolá se při vykreslení pozadí objektu ToolStripDropDownButton .

RenderGrip

Vyvolá se při vykreslení úchytu ToolStrip pro přesunutí.

RenderImageMargin

Nakreslí okraj mezi obrázkem a jeho kontejnerem.

RenderItemBackground

Vyvolá se při vykreslení pozadí objektu ToolStripItem .

RenderItemCheck

Vyvolá se při vykreslení obrázku vybraného ToolStripItem objektu.

RenderItemImage

Vyvolá se při vykreslení obrázku pro .ToolStripItem

RenderItemText

Vyvolá se při vykreslení textu pro .ToolStripItem

RenderLabelBackground

Vyvolá se při vykreslení pozadí objektu ToolStripLabel .

RenderMenuItemBackground

Vyvolá se při vykreslení pozadí objektu ToolStripMenuItem .

RenderOverflowButtonBackground

Vyvolá se při vykreslení pozadí pro tlačítko přetečení.

RenderSeparator

Vyvolá se při vykreslení ToolStripSeparator .

RenderSplitButtonBackground

Vyvolá se při vykreslení pozadí objektu ToolStripSplitButton .

RenderStatusStripSizingGrip

Vyvolá se při změně stylu zobrazení.

RenderToolStripBackground

Vyvolá se při vykreslení pozadí objektu ToolStrip .

RenderToolStripBorder

Vyvolá se při vykreslení ohraničení pro .ToolStrip

RenderToolStripContentPanelBackground

Nakreslí pozadí objektu ToolStripContentPanel.

RenderToolStripPanelBackground

Nakreslí pozadí objektu ToolStripPanel.

RenderToolStripStatusLabelBackground

Nakreslí pozadí objektu ToolStripStatusLabel.

Platí pro

Viz také