共用方式為


如何:自訂繪製 ToolStrip 控制項

ToolStrip 控制項具有下列相關聯轉譯 (繪製) 類別的項目:

若要對 ToolStrip 自訂繪圖 (也稱為主控描繪),您可以覆寫其中一個轉譯器類別,並變更轉譯邏輯的層面。

下列程序會說明自訂繪圖的各種層面。

在提供的轉譯器之間切換

變更 Office 樣式框線

注意

該方法有用於 ToolStripRendererToolStripSystemRendererToolStripProfessionalRenderer 的版本。

變更 ProfessionalColorTable

  • 覆寫 ProfessionalColorTable 和變更您想要的色彩。

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            var colorTable = new MyColorTable();
            toolStrip1.Renderer = new ToolStripProfessionalRenderer(colorTable);
        }
    
        class MyColorTable: ProfessionalColorTable
        {
            public override System.Drawing.Color ButtonPressedGradientBegin => Color.Red;
            public override System.Drawing.Color ButtonPressedGradientMiddle => Color.Blue;
            public override System.Drawing.Color ButtonPressedGradientEnd => Color.Green;
            public override System.Drawing.Color ButtonSelectedGradientBegin => Color.Yellow;
            public override System.Drawing.Color ButtonSelectedGradientMiddle => Color.Orange;
            public override System.Drawing.Color ButtonSelectedGradientEnd => Color.Violet;
        }
    }
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim colorTable As New MyColorTable
            ToolStrip1.Renderer = New ToolStripProfessionalRenderer(colorTable)
        End Sub
    
        Class MyColorTable
            Inherits ProfessionalColorTable
    
            Public Overrides ReadOnly Property ButtonPressedGradientBegin() As System.Drawing.Color
                Get
                    Return Color.Red
                End Get
            End Property
    
            Public Overrides ReadOnly Property ButtonPressedGradientMiddle() As System.Drawing.Color
                Get
                    Return Color.Blue
                End Get
            End Property
    
            Public Overrides ReadOnly Property ButtonPressedGradientEnd() As System.Drawing.Color
                Get
                    Return Color.Green
                End Get
            End Property
    
            Public Overrides ReadOnly Property ButtonSelectedGradientBegin() As System.Drawing.Color
                Get
                    Return Color.Yellow
                End Get
            End Property
    
            Public Overrides ReadOnly Property ButtonSelectedGradientMiddle() As System.Drawing.Color
                Get
                    Return Color.Orange
                End Get
            End Property
    
            Public Overrides ReadOnly Property ButtonSelectedGradientEnd() As System.Drawing.Color
                Get
                    Return Color.Violet
                End Get
            End Property
        End Class
    End Class
    

變更所有 ToolStrips 的轉譯

  1. 使用 ToolStripManager.RenderMode 屬性來選擇提供的轉譯器之其中一種。

  2. 使用 ToolStripManager.Renderer 來指派自訂轉譯器。

  3. 確保 ToolStrip.RenderMode 設定為 ManagerRenderMode 的預設值。

關閉 Office 色彩

關閉一個 ToolStrip 的 Office 色彩

  • 使用與下列程式碼範例類似的程式碼。

    ProfessionalColorTable colorTable = new ProfessionalColorTable();
    colorTable.UseSystemColors = true;
    toolStrip1.Renderer = new ToolStripProfessionalRenderer(colorTable);
    
    Dim colorTable As New ProfessionalColorTable
    colorTable.UseSystemColors = True
    ToolStrip1.Renderer = new ToolStripProfessionalRenderer(colorTable)
    

另請參閱