共用方式為


ToolStripRenderer 類別

定義

負責物件的繪製功能 ToolStrip

public ref class ToolStripRenderer abstract
public abstract class ToolStripRenderer
type ToolStripRenderer = class
Public MustInherit Class ToolStripRenderer
繼承
ToolStripRenderer
衍生

範例

以下程式碼範例示範如何實作自訂 ToolStripRenderer 類別。 該 GridStripRenderer 職業自訂控制外觀的三個面向 GridStripGridStrip 邊框、 ToolStripButton 邊框和 ToolStripButton 影像。 完整程式碼列表請參見 「如何實作自訂工具剝離渲染器」。

// 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。 你不是自訂繪製 a ToolStrip 及其 ToolStripItem 包含的物件,而是將屬性設定 ToolStrip.Renderer 為繼承自 ToolStripRenderer的物件。 由 ToolStripRenderer 所指定的繪畫會應用於 ToolStrip,以及其中包含的項目。

你可以用多種方式在控制中自訂繪製 ToolStrip 。 與其他 Windows Forms 控制項一樣,和 ToolStripToolStripItem 都有可 OnPaint 覆寫的方法與 Paint 事件。 與一般繪畫一樣,座標系是相對於控制器的客戶區域;也就是說,控制鍵的左上角是 0, 0。 Paint事件和OnPaint方法ToolStripItem的行為與其他控制繪製事件相似。

這個ToolStripRenderer類別有可覆寫的方法來繪製背景、物品背景、物品圖片、物品箭頭、物品文字和邊界。ToolStrip 這些方法的事件參數會顯示多種屬性,例如矩形、顏色和文字格式,你可以依需求調整。

若要調整物品塗裝的部分細節,通常會覆寫 ToolStripRenderer

如果你正在寫新作品,想要控制畫作的所有面向,可以覆寫這個 OnPaint 方法。 在 OnPaint內,你可以使用 。ToolStripRenderer

預設情況下,它 ToolStrip 是雙重緩衝,利用了這個 OptimizedDoubleBuffer 設定。

建構函式

名稱 Description
ToolStripRenderer()

初始化 ToolStripRenderer 類別的新執行個體。

欄位

名稱 Description
Offset2X

取得或設定偏移乘數為兩倍的偏移量,沿 x 軸方向。

Offset2Y

取得或設定偏移乘數,該倍率沿 y 軸偏移量為兩倍。

方法

名稱 Description
CreateDisabledImage(Image)

建立給定影像的灰階複製品。

DrawArrow(ToolStripArrowRenderEventArgs)

在 . 上畫箭頭 ToolStripItem

DrawButtonBackground(ToolStripItemRenderEventArgs)

為 繪製背景。ToolStripButton

DrawDropDownButtonBackground(ToolStripItemRenderEventArgs)

為 繪製背景。ToolStripDropDownButton

DrawGrip(ToolStripGripRenderEventArgs)

ToolStrip.

DrawImageMargin(ToolStripRenderEventArgs)

在 上 ToolStrip繪製圖像周圍的空間。

DrawItemBackground(ToolStripItemRenderEventArgs)

為 繪製背景。ToolStripItem

DrawItemCheck(ToolStripItemImageRenderEventArgs)

在 上 ToolStripItem 繪製圖像,表示該物品處於選中狀態。

DrawItemImage(ToolStripItemImageRenderEventArgs)

在 . 上繪製圖像 ToolStripItem

DrawItemText(ToolStripItemTextRenderEventArgs)

ToolStripItem在 .

DrawLabelBackground(ToolStripItemRenderEventArgs)

為 繪製背景。ToolStripLabel

DrawMenuItemBackground(ToolStripItemRenderEventArgs)

為 繪製背景。ToolStripMenuItem

DrawOverflowButtonBackground(ToolStripItemRenderEventArgs)

為溢出按鈕繪製背景。

DrawSeparator(ToolStripSeparatorRenderEventArgs)

畫出一個 ToolStripSeparator

DrawSplitButton(ToolStripItemRenderEventArgs)

畫出一個 ToolStripSplitButton

DrawStatusStripSizingGrip(ToolStripRenderEventArgs)

畫出尺寸握把。

DrawToolStripBackground(ToolStripRenderEventArgs)

為 繪製背景。ToolStrip

DrawToolStripBorder(ToolStripRenderEventArgs)

為 畫出邊界。ToolStrip

DrawToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

繪製了 的背景 ToolStripContentPanel

DrawToolStripPanelBackground(ToolStripPanelRenderEventArgs)

繪製了 的背景 ToolStripPanel

DrawToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

繪製了 的背景 ToolStripStatusLabel

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
Initialize(ToolStrip)

當在衍生類別中覆寫時,提供對給定 ToolStrip的自訂初始化。

InitializeContentPanel(ToolStripContentPanel)

初始化指定的 ToolStripContentPanel

InitializeItem(ToolStripItem)

當在衍生類別中覆寫時,提供對給定 ToolStripItem的自訂初始化。

InitializePanel(ToolStripPanel)

初始化指定的 ToolStripPanel

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
OnRenderArrow(ToolStripArrowRenderEventArgs)

引發 RenderArrow 事件。

OnRenderButtonBackground(ToolStripItemRenderEventArgs)

引發 RenderButtonBackground 事件。

OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs)

引發 RenderDropDownButtonBackground 事件。

OnRenderGrip(ToolStripGripRenderEventArgs)

引發 RenderGrip 事件。

OnRenderImageMargin(ToolStripRenderEventArgs)

繪製物品背景。

OnRenderItemBackground(ToolStripItemRenderEventArgs)

引發 OnRenderItemBackground(ToolStripItemRenderEventArgs) 事件。

OnRenderItemCheck(ToolStripItemImageRenderEventArgs)

引發 RenderItemCheck 事件。

OnRenderItemImage(ToolStripItemImageRenderEventArgs)

引發 RenderItemImage 事件。

OnRenderItemText(ToolStripItemTextRenderEventArgs)

引發 RenderItemText 事件。

OnRenderLabelBackground(ToolStripItemRenderEventArgs)

引發 RenderLabelBackground 事件。

OnRenderMenuItemBackground(ToolStripItemRenderEventArgs)

引發 RenderMenuItemBackground 事件。

OnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs)

引發 RenderOverflowButtonBackground 事件。

OnRenderSeparator(ToolStripSeparatorRenderEventArgs)

引發 RenderSeparator 事件。

OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs)

引發 OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs) 事件。

OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs)

引發 RenderStatusStripSizingGrip 事件。

OnRenderToolStripBackground(ToolStripRenderEventArgs)

引發 RenderToolStripBackground 事件。

OnRenderToolStripBorder(ToolStripRenderEventArgs)

引發 RenderToolStripBorder 事件。

OnRenderToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

引發 RenderToolStripContentPanelBackground 事件。

OnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs)

引發 RenderToolStripPanelBackground 事件。

OnRenderToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

引發 RenderToolStripStatusLabelBackground 事件。

ScaleArrowOffsetsIfNeeded()

如果需要根據電腦的 DPI 設定縮放,會套用 和 Offset2XOffset2Y 來縮放箭頭圖示。

ScaleArrowOffsetsIfNeeded(Int32)

將 和 Offset2Y 值套Offset2X用到根據指定的 DPI 值縮放箭頭圖示。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

事件

名稱 Description
RenderArrow

當箭頭被渲染時 ToolStripItem 會發生。

RenderButtonBackground

當 a ToolStripButton 的背景被渲染時會發生。

RenderDropDownButtonBackground

當 a ToolStripDropDownButton 的背景被渲染時會發生。

RenderGrip

當 a ToolStrip 的移動柄被渲染時會發生。

RenderImageMargin

畫出影像與容器之間的邊界。

RenderItemBackground

當 a ToolStripItem 的背景被渲染時會發生。

RenderItemCheck

當選取 ToolStripItem 的影像被渲染時,會發生這種情況。

RenderItemImage

當 a ToolStripItem 的影像被渲染時會發生。

RenderItemText

當 a ToolStripItem 的文字被渲染時,會發生這種情況。

RenderLabelBackground

當 a ToolStripLabel 的背景被渲染時會發生。

RenderMenuItemBackground

當 a ToolStripMenuItem 的背景被渲染時會發生。

RenderOverflowButtonBackground

當溢出按鈕的背景被渲染時會發生。

RenderSeparator

當 a ToolStripSeparator 被渲染時會發生。

RenderSplitButtonBackground

當 a ToolStripSplitButton 的背景被渲染時會發生。

RenderStatusStripSizingGrip

當顯示風格改變時會發生。

RenderToolStripBackground

當 a ToolStrip 的背景被渲染時會發生。

RenderToolStripBorder

當 a ToolStrip 的邊界被渲染時會發生。

RenderToolStripContentPanelBackground

繪製一個 ToolStripContentPanel.

RenderToolStripPanelBackground

繪製一個 ToolStripPanel.

RenderToolStripStatusLabelBackground

繪製一個 ToolStripStatusLabel.

適用於

另請參閱