Comment : définir le convertisseur ToolStrip au moment de l'exécution

Vous pouvez personnaliser l'apparence de votre contrôle ToolStrip en créant une classe ProfessionalColorTable personnalisée.

Exemple

Le premier exemple de code montre comment créer une classe ProfessionalColorTable personnalisée. Cette classe définit des dégradés pour une MenuStrip et un contrôle ToolStrip.

Pour utiliser cet exemple de code, compilez et exécutez l'application, puis cliquez sur Modifier les couleurs pour appliquer les dégradés définis dans la classe ProfessionalColorTable personnalisée.

Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Drawing


...


' This code example demonstrates how to use a ProfessionalRenderer
' to define custom professional colors at runtime.
Class Form2
   Inherits Form

   Public Sub New()
      ' Create a new ToolStrip control.
      Dim ts As New ToolStrip()

      ' Populate the ToolStrip control.
      ts.Items.Add("Apples")
      ts.Items.Add("Oranges")
      ts.Items.Add("Pears")
      ts.Items.Add("Change Colors", Nothing, New EventHandler(AddressOf ChangeColors_Click))

      ' Create a new MenuStrip.
      Dim ms As New MenuStrip()

      ' Dock the MenuStrip control to the top of the form.
      ms.Dock = DockStyle.Top

      ' Add the top-level menu items.
      ms.Items.Add("File")
      ms.Items.Add("Edit")
      ms.Items.Add("View")
      ms.Items.Add("Window")

      ' Add the ToolStrip to Controls collection.
      Me.Controls.Add(ts)

      ' Add the MenuStrip control last.
      ' This is important for correct placement in the z-order.
      Me.Controls.Add(ms)
    End Sub

   ' This event handler is invoked when the "Change colors"
   ' ToolStripItem is clicked. It assigns the Renderer
   ' property for the ToolStrip control.
    Sub ChangeColors_Click(ByVal sender As Object, ByVal e As EventArgs)
        ToolStripManager.Renderer = New ToolStripProfessionalRenderer(New CustomProfessionalColors())
    End Sub
End Class

' This class defines the gradient colors for 
' the MenuStrip and the ToolStrip.
Class CustomProfessionalColors
   Inherits ProfessionalColorTable

   Public Overrides ReadOnly Property ToolStripGradientBegin() As Color
      Get
         Return Color.BlueViolet
      End Get 
   End Property

   Public Overrides ReadOnly Property ToolStripGradientMiddle() As Color
      Get
         Return Color.CadetBlue
      End Get 
   End Property

   Public Overrides ReadOnly Property ToolStripGradientEnd() As Color
      Get
         Return Color.CornflowerBlue
      End Get 
   End Property

   Public Overrides ReadOnly Property MenuStripGradientBegin() As Color
      Get
         Return Color.Salmon
      End Get 
   End Property

   Public Overrides ReadOnly Property MenuStripGradientEnd() As Color
      Get
         Return Color.OrangeRed
      End Get
    End Property

End Class
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;


...


// This code example demonstrates how to use a ProfessionalRenderer
// to define custom professional colors at runtime.
class Form2 : Form
{
    public Form2()
    {
        // Create a new ToolStrip control.
        ToolStrip ts = new ToolStrip();

        // Populate the ToolStrip control.
        ts.Items.Add("Apples");
        ts.Items.Add("Oranges");
        ts.Items.Add("Pears");
        ts.Items.Add(
            "Change Colors", 
            null, 
            new EventHandler(ChangeColors_Click));

        // Create a new MenuStrip.
        MenuStrip ms = new MenuStrip();

        // Dock the MenuStrip control to the top of the form.
        ms.Dock = DockStyle.Top;

        // Add the top-level menu items.
        ms.Items.Add("File");
        ms.Items.Add("Edit");
        ms.Items.Add("View");
        ms.Items.Add("Window");

        // Add the ToolStrip to Controls collection.
        this.Controls.Add(ts);

        // Add the MenuStrip control last.
        // This is important for correct placement in the z-order.
        this.Controls.Add(ms);
    }

    // This event handler is invoked when the "Change colors"
    // ToolStripItem is clicked. It assigns the Renderer
    // property for the ToolStrip control.
    void ChangeColors_Click(object sender, EventArgs e)
    {
        ToolStripManager.Renderer = 
            new ToolStripProfessionalRenderer(new CustomProfessionalColors());
    }
}

// This class defines the gradient colors for 
// the MenuStrip and the ToolStrip.
class CustomProfessionalColors : ProfessionalColorTable
{
    public override Color ToolStripGradientBegin
    { get { return Color.BlueViolet; } }

    public override Color ToolStripGradientMiddle
    { get { return Color.CadetBlue; } }

    public override Color ToolStripGradientEnd
    { get { return Color.CornflowerBlue; } }

    public override Color MenuStripGradientBegin
    { get { return Color.Salmon; } }

    public override Color MenuStripGradientEnd
    { get { return Color.OrangeRed; } }
}

Définition d'une classe ProfessionalColorTable personnalisée

Les dégradés personnalisés sont définis dans la classe CustomProfessionalColors.

' This class defines the gradient colors for 
' the MenuStrip and the ToolStrip.
Class CustomProfessionalColors
   Inherits ProfessionalColorTable

   Public Overrides ReadOnly Property ToolStripGradientBegin() As Color
      Get
         Return Color.BlueViolet
      End Get 
   End Property

   Public Overrides ReadOnly Property ToolStripGradientMiddle() As Color
      Get
         Return Color.CadetBlue
      End Get 
   End Property

   Public Overrides ReadOnly Property ToolStripGradientEnd() As Color
      Get
         Return Color.CornflowerBlue
      End Get 
   End Property

   Public Overrides ReadOnly Property MenuStripGradientBegin() As Color
      Get
         Return Color.Salmon
      End Get 
   End Property

   Public Overrides ReadOnly Property MenuStripGradientEnd() As Color
      Get
         Return Color.OrangeRed
      End Get
    End Property

End Class
// This class defines the gradient colors for 
// the MenuStrip and the ToolStrip.
class CustomProfessionalColors : ProfessionalColorTable
{
    public override Color ToolStripGradientBegin
    { get { return Color.BlueViolet; } }

    public override Color ToolStripGradientMiddle
    { get { return Color.CadetBlue; } }

    public override Color ToolStripGradientEnd
    { get { return Color.CornflowerBlue; } }

    public override Color MenuStripGradientBegin
    { get { return Color.Salmon; } }

    public override Color MenuStripGradientEnd
    { get { return Color.OrangeRed; } }
}

Assignation d'un convertisseur personnalisé

Créez un nouveau ToolStripProfessionalRenderer avec une classe CustomProfessionalColors et assignez-le à la propriété ToolStripManager.Renderer.

' This event handler is invoked when the "Change colors"
' ToolStripItem is clicked. It assigns the Renderer
' property for the ToolStrip control.
 Sub ChangeColors_Click(ByVal sender As Object, ByVal e As EventArgs)
     ToolStripManager.Renderer = New ToolStripProfessionalRenderer(New CustomProfessionalColors())
 End Sub
// This event handler is invoked when the "Change colors"
// ToolStripItem is clicked. It assigns the Renderer
// property for the ToolStrip control.
void ChangeColors_Click(object sender, EventArgs e)
{
    ToolStripManager.Renderer = 
        new ToolStripProfessionalRenderer(new CustomProfessionalColors());
}

Compilation du code

Cet exemple nécessite :

  • Références aux assemblys System.Design, System.Drawing et System.Windows.Forms.

Pour plus d'informations sur la génération de cet exemple à partir de la ligne de commande pour Visual Basic ou Visual C#, consultez Génération à partir de la ligne de commande (Visual Basic) ou Génération à partir de la ligne de commande avec csc.exe. Vous pouvez aussi générer cet exemple dans Visual Studio en collant le code dans un nouveau projet. Pour plus d'informations, consultez Comment : compiler et exécuter un exemple complet de code Windows Forms à l'aide de Visual Studio et Comment : compiler et exécuter un exemple complet de code Windows Forms à l'aide de Visual Studio et Comment : compiler et exécuter un exemple complet de code Windows Forms à l'aide de Visual Studio et Comment : compiler et exécuter un exemple complet de code Windows Forms à l'aide de Visual Studio et Comment : compiler et exécuter un exemple complet de code Windows Forms à l'aide de Visual Studio.

Voir aussi

Référence

ToolStripManager

ProfessionalColorTable

MenuStrip

ToolStrip

ToolStripProfessionalRenderer

Autres ressources

ToolStrip, contrôle (Windows Forms)