ToolStripRenderer 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
處理 ToolStrip 物件的繪製功能。
public ref class ToolStripRenderer abstract
public abstract class ToolStripRenderer
type ToolStripRenderer = class
Public MustInherit Class ToolStripRenderer
- 繼承
-
ToolStripRenderer
- 衍生
範例
下列程式碼範例示範如何實作自訂 ToolStripRenderer 類別。 類別 GridStripRenderer
會自訂控制面板的 GridStrip
三個層面: GridStrip
框線、 ToolStripButton 框線和 ToolStripButton 影像。 如需完整的程式代碼清單,請參閱 如何:實作自訂 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
備註
ToolStripRenderer使用 類別將特定樣式或主題套用至 ToolStrip 。 您不需要自訂繪製 ToolStrip 和 ToolStripItem 它所包含的物件,而是將 ToolStrip.Renderer 屬性設定為繼承自 ToolStripRenderer 的物件。 指定的 ToolStripRenderer 繪製會套用至 ToolStrip ,以及它所包含的專案。
您可以透過數種方式在控制項中 ToolStrip 執行自訂繪製。 與其他Windows Forms控制項一樣, ToolStrip 和 都有可 OnPaint
覆寫的方法和 Paint
ToolStripItem 事件。 如同一般繪製,座標系統相對於控制項的工作區;也就是說,控制項的左上角是 0,0。 的事件 Paint
和 OnPaint
方法 ToolStripItem 的行為就像其他控制項繪製事件一樣。
類別 ToolStripRenderer 具有繪製背景、專案背景、專案影像、專案箭號、專案文字和框線的 ToolStrip 可覆寫方法。 這些方法的事件引數會公開數個屬性,例如您可以視需要調整的矩形、色彩和文字格式。
若要只調整專案繪製方式的幾個層面,您通常會覆寫 ToolStripRenderer 。
如果您要撰寫新專案,並想要控制繪製的所有層面,請覆寫 OnPaint
方法。 從 內 OnPaint
,您可以使用 來自 ToolStripRenderer 的方法。
根據預設,會 ToolStrip 緩衝兩次,並利用 OptimizedDoubleBuffer 設定。
建構函式
ToolStripRenderer() |
初始化 ToolStripRenderer 類別的新執行個體。 |
欄位
Offset2X |
取得或設定沿著 X 軸位移的兩倍位移乘數。 |
Offset2Y |
取得或設定沿著 Y 軸位移的兩倍位移乘數。 |
方法
事件
RenderArrow |
發生於呈現 ToolStripItem 的箭號時。 |
RenderButtonBackground |
發生於呈現 ToolStripButton 的背景時。 |
RenderDropDownButtonBackground |
發生於呈現 ToolStripDropDownButton 的背景時。 |
RenderGrip |
發生於呈現 ToolStrip 的移動控制軸時。 |
RenderImageMargin |
繪製影像與其容器之間的邊界。 |
RenderItemBackground |
發生於呈現 ToolStripItem 的背景時。 |
RenderItemCheck |
發生於呈現已選取 ToolStripItem 的影像時。 |
RenderItemImage |
發生於呈現 ToolStripItem 的影像時。 |
RenderItemText |
發生於呈現 ToolStripItem 的文字時。 |
RenderLabelBackground |
發生於呈現 ToolStripLabel 的背景時。 |
RenderMenuItemBackground |
發生於呈現 ToolStripMenuItem 的背景時。 |
RenderOverflowButtonBackground |
發生於呈現溢位按鈕的背景時。 |
RenderSeparator |
發生於呈現 ToolStripSeparator 時。 |
RenderSplitButtonBackground |
發生於呈現 ToolStripSplitButton 的背景時。 |
RenderStatusStripSizingGrip |
發生於顯示樣式變更時。 |
RenderToolStripBackground |
發生於呈現 ToolStrip 的背景時。 |
RenderToolStripBorder |
發生於呈現 ToolStrip 的框線時。 |
RenderToolStripContentPanelBackground |
繪製 ToolStripContentPanel 的背景。 |
RenderToolStripPanelBackground |
繪製 ToolStripPanel 的背景。 |
RenderToolStripStatusLabelBackground |
繪製 ToolStripStatusLabel 的背景。 |