ToolStripRenderer Classe
Definizione
public ref class ToolStripRenderer abstract
public abstract class ToolStripRenderer
type ToolStripRenderer = class
Public MustInherit Class ToolStripRenderer
- Ereditarietà
-
ToolStripRenderer
- Derivato
Esempi
L'esempio di codice seguente illustra come implementare una classe ToolStripRenderer personalizzata.The following code example demonstrates how to implement a custom ToolStripRenderer class. La classe GridStripRenderer
Personalizza tre aspetti dell'aspetto del controllo GridStrip
: GridStrip
Border, ToolStripButton Border e ToolStripButton image.The GridStripRenderer
class customizes three aspects of the GridStrip
control's appearance: GridStrip
border, ToolStripButton border, and ToolStripButton image. Per un elenco di codici completo, vedere [How per: Implementare un oggetto ToolStripRenderer personalizzato @ no__t-0.For a full code listing, see How to: Implement a Custom ToolStripRenderer.
// This class implements a custom ToolStripRenderer for the
// GridStrip control. It customizes three aspects of the
// GridStrip control's appearance: GridStrip border,
// ToolStripButton border, and ToolStripButton image.
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
Usare la classe ToolStripRenderer per applicare uno stile o tema particolare a un ToolStrip.Use the ToolStripRenderer class to apply a particular style or theme to a ToolStrip. Anziché disegnare in modo personalizzato un ToolStrip e gli oggetti ToolStripItem in esso contenuti, impostare la proprietà ToolStrip.Renderer su un oggetto che eredita da ToolStripRenderer.Rather than custom painting a ToolStrip and the ToolStripItem objects it contains, you set the ToolStrip.Renderer property to an object that inherits from ToolStripRenderer. Il disegno specificato dall'ToolStripRenderer viene applicato al ToolStrip, oltre che agli elementi che contiene.The painting specified by the ToolStripRenderer is applied to the ToolStrip, as well as the items it contains.
È possibile eseguire il disegno personalizzato in controlli ToolStrip in diversi modi.You can do custom painting in ToolStrip controls in several ways. Come per gli altri controlli Windows Forms, ToolStrip e ToolStripItem hanno entrambi i metodi OnPaint
sottoponibili a override e gli eventi Paint
.As with other Windows Forms controls, the ToolStrip and ToolStripItem both have overridable OnPaint
methods and Paint
events. Come nel caso del disegno normale, il sistema di coordinate è relativo all'area client del controllo; ovvero l'angolo superiore sinistro del controllo è 0, 0.As with regular painting, the coordinate system is relative to the client area of the control; that is, the upper left-hand corner of the control is 0, 0. L'evento Paint
e il metodo OnPaint
per un ToolStripItem si comportano come altri eventi di disegno del controllo.The Paint
event and OnPaint
method for a ToolStripItem behave like other control paint events.
La classe ToolStripRenderer ha 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 del ToolStrip.The ToolStripRenderer class has overridable methods for painting the background, item background, item image, item arrow, item text, and border of the ToolStrip. Gli argomenti dell'evento per questi metodi espongono diverse proprietà, ad esempio rettangoli, colori e formati di testo che è possibile modificare secondo le esigenze.The event arguments for these methods expose several properties such as rectangles, colors, and text formats that you can adjust as desired.
Per modificare solo alcuni aspetti della modalità di disegno di un elemento, è in genere necessario eseguire l'override del ToolStripRenderer.To adjust just a few aspects of how an item is painted, you typically override the ToolStripRenderer.
Se si sta scrivendo un nuovo elemento e si desidera controllare tutti gli aspetti del disegno, eseguire l'override del metodo OnPaint
.If you are writing a new item and want to control all aspects of the painting, override the OnPaint
method. Dall'interno OnPaint
è possibile usare i metodi del ToolStripRenderer.From within OnPaint
, you can use methods from the ToolStripRenderer.
Per impostazione predefinita, il ToolStrip è con doppio buffer, sfruttando l'impostazione OptimizedDoubleBuffer.By default, the ToolStrip is double buffered, taking advantage of the OptimizedDoubleBuffer setting.
Costruttori
ToolStripRenderer() |
Inizializza una nuova istanza della classe ToolStripRenderer.Initializes a new instance of the ToolStripRenderer class. |
Campi
Offset2X |
Ottiene o imposta il moltiplicatore offset per un valore doppio dell'offset lungo l'asse x.Gets or sets the offset multiplier for twice the offset along the x-axis. |
Offset2Y |
Ottiene o imposta il moltiplicatore offset per un valore doppio dell'offset lungo l'asse y.Gets or sets the offset multiplier for twice the offset along the y axis. |
Metodi
Eventi
RenderArrow |
Viene generato quando viene eseguito il rendering di una freccia su un oggetto ToolStripItem.Occurs when an arrow on a ToolStripItem is rendered. |
RenderButtonBackground |
Viene generato quando viene eseguito il rendering dello sfondo per un oggetto ToolStripButton.Occurs when the background for a ToolStripButton is rendered. |
RenderDropDownButtonBackground |
Viene generato quando viene eseguito il rendering dello sfondo per un oggetto ToolStripDropDownButton.Occurs when the background for a ToolStripDropDownButton is rendered. |
RenderGrip |
Viene generato quando viene eseguito il rendering dell'handle di spostamento di un oggetto ToolStrip.Occurs when the move handle for a ToolStrip is rendered. |
RenderImageMargin |
Disegna il margine presente tra un'immagine e il relativo contenitore.Draws the margin between an image and its container. |
RenderItemBackground |
Viene generato quando viene eseguito il rendering dello sfondo per un oggetto ToolStripItem.Occurs when the background for a ToolStripItem is rendered. |
RenderItemCheck |
Viene generato quando viene eseguito il rendering dell'immagine di un oggetto ToolStripItem selezionato.Occurs when the image for a selected ToolStripItem is rendered. |
RenderItemImage |
Viene generato quando viene eseguito il rendering dell'immagine per un oggetto ToolStripItem.Occurs when the image for a ToolStripItem is rendered. |
RenderItemText |
Viene generato quando viene eseguito il rendering del testo per un oggetto ToolStripItem.Occurs when the text for a ToolStripItem is rendered. |
RenderLabelBackground |
Viene generato quando viene eseguito il rendering dello sfondo per un oggetto ToolStripLabel.Occurs when the background for a ToolStripLabel is rendered. |
RenderMenuItemBackground |
Viene generato quando viene eseguito il rendering dello sfondo per un oggetto ToolStripMenuItem.Occurs when the background for a ToolStripMenuItem is rendered. |
RenderOverflowButtonBackground |
Si verifica quando viene eseguito il rendering dello sfondo per un pulsante di overflow.Occurs when the background for an overflow button is rendered. |
RenderSeparator |
Viene generato quando viene eseguito il rendering di un oggetto ToolStripSeparator.Occurs when a ToolStripSeparator is rendered. |
RenderSplitButtonBackground |
Viene generato quando viene eseguito il rendering dello sfondo per un oggetto ToolStripSplitButton.Occurs when the background for a ToolStripSplitButton is rendered. |
RenderStatusStripSizingGrip |
Si verifica quando viene modificato lo stile di visualizzazione.Occurs when the display style changes. |
RenderToolStripBackground |
Viene generato quando viene eseguito il rendering dello sfondo per un oggetto ToolStrip.Occurs when the background for a ToolStrip is rendered. |
RenderToolStripBorder |
Viene generato quando viene eseguito il rendering del bordo per un oggetto ToolStrip.Occurs when the border for a ToolStrip is rendered. |
RenderToolStripContentPanelBackground |
Disegna lo sfondo di un oggetto ToolStripContentPanel.Draws the background of a ToolStripContentPanel. |
RenderToolStripPanelBackground |
Disegna lo sfondo di un oggetto ToolStripPanel.Draws the background of a ToolStripPanel. |
RenderToolStripStatusLabelBackground |
Disegna lo sfondo di un oggetto ToolStripStatusLabel.Draws the background of a ToolStripStatusLabel. |