다음을 통해 공유


방법: 애플리케이션에 대한 ToolStrip 렌더러 설정

ToolStrip 컨트롤의 모양을 개별적으로 또는 애플리케이션의 모든 ToolStrip 컨트롤에 대해 사용자 지정할 수 있습니다.

예제

다음 코드 예제에서는 ToolStrip 컨트롤 및 MenuStrip 컨트롤에 사용자 지정 렌더러를 선택적으로 적용하는 방법을 보여 줍니다.

이 코드 예제를 사용하려면 애플리케이션을 컴파일 및 실행한 다음 ComboBox 컨트롤에서 사용자 지정 렌더링 범위를 선택합니다. 적용을 클릭하여 렌더러를 설정합니다.

사용자 지정 메뉴 항목 렌더링을 보려면 ComboBox 컨트롤에서 MenuStrip 옵션을 선택하고 적용을 클릭한 다음 파일 메뉴 항목을 엽니다.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Drawing
// This example demonstrates how to apply a
// custom professional renderer to an individual
// ToolStrip or to the application as a whole.
class Form6 : Form
{
    ComboBox targetComboBox = new ComboBox();

    public Form6()
    {
        // Alter the renderer at the top level.

        // Create and populate a new ToolStrip control.
        ToolStrip ts = new ToolStrip();
        ts.Name = "ToolStrip";
        ts.Items.Add("Apples");
        ts.Items.Add("Oranges");
        ts.Items.Add("Pears");

        // Create a new menustrip with a new window.
        MenuStrip ms = new MenuStrip();
        ms.Name = "MenuStrip";
        ms.Dock = DockStyle.Top;

        // add top level items
        ToolStripMenuItem fileMenuItem = new ToolStripMenuItem("File");
        ms.Items.Add(fileMenuItem);
        ms.Items.Add("Edit");
        ms.Items.Add("View");
        ms.Items.Add("Window");

        // Add subitems to the "File" menu.
        fileMenuItem.DropDownItems.Add("Open");
        fileMenuItem.DropDownItems.Add("Save");
        fileMenuItem.DropDownItems.Add("Save As...");
        fileMenuItem.DropDownItems.Add("-");
        fileMenuItem.DropDownItems.Add("Exit");

        // Add a Button control to apply renderers.
        Button applyButton = new Button();
        applyButton.Text = "Apply Custom Renderer";
        applyButton.Click += new EventHandler(applyButton_Click);

        // Add the ComboBox control for choosing how
        // to apply the renderers.
        targetComboBox.Items.Add("All");
        targetComboBox.Items.Add("MenuStrip");
        targetComboBox.Items.Add("ToolStrip");
        targetComboBox.Items.Add("Reset");

        // Create and set up a TableLayoutPanel control.
        TableLayoutPanel tlp = new TableLayoutPanel();
        tlp.Dock = DockStyle.Fill;
        tlp.RowCount = 1;
        tlp.ColumnCount = 2;
        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent));
        tlp.Controls.Add(applyButton);
        tlp.Controls.Add(targetComboBox);

        // Create a GroupBox for the TableLayoutPanel control.
        GroupBox gb = new GroupBox();
        gb.Text = "Apply Renderers";
        gb.Dock = DockStyle.Fill;
        gb.Controls.Add(tlp);

        // Add the GroupBox to the form.
        this.Controls.Add(gb);

        // Add the ToolStrip to the form's 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 "Apply Renderers" button is clicked.
    // Depending on the value selected in a ComboBox control,
    // it applies a custom renderer selectively to
    // individual MenuStrip or ToolStrip controls,
    // or it applies a custom renderer to the
    // application as a whole.
    void applyButton_Click(object sender, EventArgs e)
    {
        ToolStrip ms = ToolStripManager.FindToolStrip("MenuStrip");
        ToolStrip ts = ToolStripManager.FindToolStrip("ToolStrip");

        if (targetComboBox.SelectedItem != null)
        {
            switch (targetComboBox.SelectedItem.ToString())
            {
                case "Reset":
                {
                    ms.RenderMode = ToolStripRenderMode.ManagerRenderMode;
                    ts.RenderMode = ToolStripRenderMode.ManagerRenderMode;

                    // Set the default RenderMode to Professional.
                    ToolStripManager.RenderMode = ToolStripManagerRenderMode.Professional;

                    break;
                }

                case "All":
                {
                    ms.RenderMode = ToolStripRenderMode.ManagerRenderMode;
                    ts.RenderMode = ToolStripRenderMode.ManagerRenderMode;

                    // Assign the custom renderer at the application level.
                    ToolStripManager.Renderer = new CustomProfessionalRenderer();

                    break;
                }

                case "MenuStrip":
                {
                    // Assign the custom renderer to the MenuStrip control only.
                    ms.Renderer = new CustomProfessionalRenderer();

                    break;
                }

                case "ToolStrip":
                {
                    // Assign the custom renderer to the ToolStrip control only.
                    ts.Renderer = new CustomProfessionalRenderer();

                    break;
                }
            }
        }
    }
}

// This type demonstrates a custom renderer. It overrides the
// OnRenderMenuItemBackground and OnRenderButtonBackground methods
// to customize the backgrounds of MenuStrip items and ToolStrip buttons.
class CustomProfessionalRenderer : ToolStripProfessionalRenderer
{
    protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
    {
        if (e.Item.Selected)
        {
            using (Brush b = new SolidBrush(ProfessionalColors.SeparatorLight))
            {
                e.Graphics.FillEllipse(b, e.Item.ContentRectangle);
            }
        }
        else
        {
            using (Pen p = new Pen(ProfessionalColors.SeparatorLight))
            {
                e.Graphics.DrawEllipse(p, e.Item.ContentRectangle);
            }
        }
    }

    protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
    {
        Rectangle r = Rectangle.Inflate(e.Item.ContentRectangle, -2, -2);

        if (e.Item.Selected)
        {
            using (Brush b = new SolidBrush(ProfessionalColors.SeparatorLight))
            {
                e.Graphics.FillRectangle(b, r);
            }
        }
        else
        {
            using (Pen p = new Pen(ProfessionalColors.SeparatorLight))
            {
                e.Graphics.DrawRectangle(p, r);
            }
        }
    }
}
' This example demonstrates how to apply a 
' custom professional renderer to an individual
' ToolStrip or to the application as a whole.
Class Form6
   Inherits Form
   Private targetComboBox As New ComboBox()
   
   
    Public Sub New()

        ' Alter the renderer at the top level.
        ' Create and populate a new ToolStrip control.
        Dim ts As New ToolStrip()
        ts.Name = "ToolStrip"
        ts.Items.Add("Apples")
        ts.Items.Add("Oranges")
        ts.Items.Add("Pears")

        ' Create a new menustrip with a new window.
        Dim ms As New MenuStrip()
        ms.Name = "MenuStrip"
        ms.Dock = DockStyle.Top

        ' add top level items
        Dim fileMenuItem As New ToolStripMenuItem("File")
        ms.Items.Add(fileMenuItem)
        ms.Items.Add("Edit")
        ms.Items.Add("View")
        ms.Items.Add("Window")

        ' Add subitems to the "File" menu.
        fileMenuItem.DropDownItems.Add("Open")
        fileMenuItem.DropDownItems.Add("Save")
        fileMenuItem.DropDownItems.Add("Save As...")
        fileMenuItem.DropDownItems.Add("-")
        fileMenuItem.DropDownItems.Add("Exit")

        ' Add a Button control to apply renderers.
        Dim applyButton As New Button()
        applyButton.Text = "Apply Custom Renderer"
        AddHandler applyButton.Click, AddressOf applyButton_Click

        ' Add the ComboBox control for choosing how
        ' to apply the renderers.
        targetComboBox.Items.Add("All")
        targetComboBox.Items.Add("MenuStrip")
        targetComboBox.Items.Add("ToolStrip")
        targetComboBox.Items.Add("Reset")

        ' Create and set up a TableLayoutPanel control.
        Dim tlp As New TableLayoutPanel()
        tlp.Dock = DockStyle.Fill
        tlp.RowCount = 1
        tlp.ColumnCount = 2
        tlp.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
        tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Percent))
        tlp.Controls.Add(applyButton)
        tlp.Controls.Add(targetComboBox)

        ' Create a GroupBox for the TableLayoutPanel control.
        Dim gb As New GroupBox()
        gb.Text = "Apply Renderers"
        gb.Dock = DockStyle.Fill
        gb.Controls.Add(tlp)

        ' Add the GroupBox to the form.
        Me.Controls.Add(gb)

        ' Add the ToolStrip to the form's 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 "Apply Renderers" button is clicked.
    ' Depending on the value selected in a ComboBox 
    ' control, it applies a custom renderer selectively
    ' to individual MenuStrip or ToolStrip controls,
    ' or it applies a custom renderer to the 
    ' application as a whole.
    Sub applyButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim ms As ToolStrip = ToolStripManager.FindToolStrip("MenuStrip")
        Dim ts As ToolStrip = ToolStripManager.FindToolStrip("ToolStrip")

        If targetComboBox.SelectedItem IsNot Nothing Then

            Select Case targetComboBox.SelectedItem.ToString()
                Case "Reset"
                    ms.RenderMode = ToolStripRenderMode.ManagerRenderMode
                    ts.RenderMode = ToolStripRenderMode.ManagerRenderMode

                    ' Set the default RenderMode to Professional.
                    ToolStripManager.RenderMode = ToolStripManagerRenderMode.Professional

                    Exit Select

                Case "All"
                    ms.RenderMode = ToolStripRenderMode.ManagerRenderMode
                    ts.RenderMode = ToolStripRenderMode.ManagerRenderMode

                    ' Assign the custom renderer at the application level.
                    ToolStripManager.Renderer = New CustomProfessionalRenderer()

                    Exit Select

                Case "MenuStrip"
                    ' Assign the custom renderer to the MenuStrip control only.
                    ms.Renderer = New CustomProfessionalRenderer()

                    Exit Select

                Case "ToolStrip"
                    ' Assign the custom renderer to the ToolStrip control only.
                    ts.Renderer = New CustomProfessionalRenderer()

                    Exit Select
            End Select

        End If
    End Sub
End Class

' This type demonstrates a custom renderer. It overrides the
' OnRenderMenuItemBackground and OnRenderButtonBackground methods
' to customize the backgrounds of MenuStrip items and ToolStrip buttons.
Class CustomProfessionalRenderer
   Inherits ToolStripProfessionalRenderer
   
   Protected Overrides Sub OnRenderMenuItemBackground(e As ToolStripItemRenderEventArgs)
      If e.Item.Selected Then
         Dim b = New SolidBrush(ProfessionalColors.SeparatorLight)
         Try
            e.Graphics.FillEllipse(b, e.Item.ContentRectangle)
         Finally
            b.Dispose()
         End Try
      Else
         Dim p As New Pen(ProfessionalColors.SeparatorLight)
         Try
            e.Graphics.DrawEllipse(p, e.Item.ContentRectangle)
         Finally
            p.Dispose()
         End Try
      End If
    End Sub

   Protected Overrides Sub OnRenderButtonBackground(e As ToolStripItemRenderEventArgs)
      Dim r As Rectangle = Rectangle.Inflate(e.Item.ContentRectangle, - 2, - 2)
      
      If e.Item.Selected Then
         Dim b = New SolidBrush(ProfessionalColors.SeparatorLight)
         Try
            e.Graphics.FillRectangle(b, r)
         Finally
            b.Dispose()
         End Try
      Else
         Dim p As New Pen(ProfessionalColors.SeparatorLight)
         Try
            e.Graphics.DrawRectangle(p, r)
         Finally
            p.Dispose()
         End Try
      End If
    End Sub
End Class

ToolStripManager.Renderer 속성을 설정하여 애플리케이션의 모든 ToolStrip 컨트롤에 사용자 지정 렌더러를 적용합니다.

ToolStrip.Renderer 속성을 설정하여 개별 ToolStrip 컨트롤에 사용자 지정 렌더러를 적용합니다.

코드 컴파일

이 예제에는 다음 사항이 필요합니다.

  • System.Design, System.Drawing 및 System.Windows.Forms 어셈블리에 대한 참조

참고 항목