Bagikan melalui


ToolStripRenderer Kelas

Definisi

Menangani fungsionalitas lukisan untuk ToolStrip objek.

public ref class ToolStripRenderer abstract
public abstract class ToolStripRenderer
type ToolStripRenderer = class
Public MustInherit Class ToolStripRenderer
Warisan
ToolStripRenderer
Turunan

Contoh

Contoh kode berikut menunjukkan cara mengimplementasikan kelas kustom ToolStripRenderer . Kelas ini GridStripRenderer menyesuaikan tiga aspek GridStrip tampilan kontrol: GridStrip batas, ToolStripButton batas, dan ToolStripButton gambar. Untuk daftar kode lengkap, lihat Cara: Menerapkan ToolStripRenderer Kustom.

// 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

Keterangan

ToolStripRenderer Gunakan kelas untuk menerapkan gaya atau tema tertentu ke ToolStrip. Daripada melukis ToolStrip kustom dan objek yang ToolStripItem dikandungnya, Anda mengatur ToolStrip.Renderer properti ke objek yang mewarisi dari ToolStripRenderer. Lukisan yang ditentukan oleh ToolStripRenderer diterapkan ke ToolStrip, serta item yang dikandungnya.

Anda dapat melakukan lukisan kustom dalam kontrol dengan ToolStrip beberapa cara. Seperti halnya kontrol Formulir Windows lainnya, ToolStrip dan ToolStripItem keduanya memiliki metode dan Paint peristiwa yang dapat OnPaint diganti. Seperti halnya lukisan reguler, sistem koordinat relatif terhadap area klien kontrol; artinya, sudut kiri atas kontrol adalah 0, 0. Peristiwa Paint dan OnPaint metode untuk perilaku ToolStripItem seperti peristiwa cat kontrol lainnya.

Kelas ToolStripRenderer ini memiliki metode yang dapat diganti untuk melukis latar belakang, latar belakang item, gambar item, panah item, teks item, dan batas ToolStrip. Argumen peristiwa untuk metode ini mengekspos beberapa properti seperti persegi panjang, warna, dan format teks yang dapat Anda sesuaikan sesuai keinginan.

Untuk menyesuaikan hanya beberapa aspek tentang bagaimana item dicat, Anda biasanya mengambil alih ToolStripRenderer.

Jika Anda menulis item baru dan ingin mengontrol semua aspek lukisan, ganti OnPaint metode . Dari dalam OnPaint, Anda dapat menggunakan metode dari ToolStripRenderer.

Secara default, ToolStrip di-buffer ganda, memanfaatkan OptimizedDoubleBuffer pengaturan.

Konstruktor

ToolStripRenderer()

Menginisialisasi instans baru kelas ToolStripRenderer.

Bidang

Offset2X

Mendapatkan atau mengatur pengali offset untuk dua kali offset di sepanjang sumbu x.

Offset2Y

Mendapatkan atau mengatur pengali offset untuk dua kali offset di sepanjang sumbu y.

Metode

CreateDisabledImage(Image)

Membuat salinan skala abu-abu dari gambar tertentu.

DrawArrow(ToolStripArrowRenderEventArgs)

Menggambar panah pada ToolStripItem.

DrawButtonBackground(ToolStripItemRenderEventArgs)

Menggambar latar belakang untuk .ToolStripButton

DrawDropDownButtonBackground(ToolStripItemRenderEventArgs)

Menggambar latar belakang untuk .ToolStripDropDownButton

DrawGrip(ToolStripGripRenderEventArgs)

Menggambar handel pemindahan pada ToolStrip.

DrawImageMargin(ToolStripRenderEventArgs)

Menggambar ruang di sekitar gambar pada ToolStrip.

DrawItemBackground(ToolStripItemRenderEventArgs)

Menggambar latar belakang untuk .ToolStripItem

DrawItemCheck(ToolStripItemImageRenderEventArgs)

Menggambar gambar pada yang ToolStripItem menunjukkan item berada dalam status yang dipilih.

DrawItemImage(ToolStripItemImageRenderEventArgs)

Menggambar gambar pada ToolStripItem.

DrawItemText(ToolStripItemTextRenderEventArgs)

Menggambar teks pada ToolStripItem.

DrawLabelBackground(ToolStripItemRenderEventArgs)

Menggambar latar belakang untuk .ToolStripLabel

DrawMenuItemBackground(ToolStripItemRenderEventArgs)

Menggambar latar belakang untuk .ToolStripMenuItem

DrawOverflowButtonBackground(ToolStripItemRenderEventArgs)

Menggambar latar belakang untuk tombol luapan.

DrawSeparator(ToolStripSeparatorRenderEventArgs)

ToolStripSeparatorMenggambar .

DrawSplitButton(ToolStripItemRenderEventArgs)

ToolStripSplitButtonMenggambar .

DrawStatusStripSizingGrip(ToolStripRenderEventArgs)

Menggambar pegangan ukuran.

DrawToolStripBackground(ToolStripRenderEventArgs)

Menggambar latar belakang untuk .ToolStrip

DrawToolStripBorder(ToolStripRenderEventArgs)

Menggambar batas untuk .ToolStrip

DrawToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

Menggambar latar belakang ToolStripContentPanel.

DrawToolStripPanelBackground(ToolStripPanelRenderEventArgs)

Menggambar latar belakang ToolStripPanel.

DrawToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

Menggambar latar belakang ToolStripStatusLabel.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan instans Type saat ini.

(Diperoleh dari Object)
Initialize(ToolStrip)

Ketika ditimpa di kelas turunan, menyediakan inisialisasi kustom dari yang diberikan ToolStrip.

InitializeContentPanel(ToolStripContentPanel)

Menginisialisasi ToolStripContentPanel.

InitializeItem(ToolStripItem)

Ketika ditimpa di kelas turunan, menyediakan inisialisasi kustom dari yang diberikan ToolStripItem.

InitializePanel(ToolStripPanel)

Menginisialisasi ToolStripPanel.

MemberwiseClone()

Membuat salinan dangkal dari yang saat ini Object.

(Diperoleh dari Object)
OnRenderArrow(ToolStripArrowRenderEventArgs)

Memunculkan kejadian RenderArrow.

OnRenderButtonBackground(ToolStripItemRenderEventArgs)

Memunculkan kejadian RenderButtonBackground.

OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs)

Memunculkan kejadian RenderDropDownButtonBackground.

OnRenderGrip(ToolStripGripRenderEventArgs)

Memunculkan kejadian RenderGrip.

OnRenderImageMargin(ToolStripRenderEventArgs)

Menggambar latar belakang item.

OnRenderItemBackground(ToolStripItemRenderEventArgs)

Memunculkan kejadian OnRenderItemBackground(ToolStripItemRenderEventArgs).

OnRenderItemCheck(ToolStripItemImageRenderEventArgs)

Memunculkan kejadian RenderItemCheck.

OnRenderItemImage(ToolStripItemImageRenderEventArgs)

Memunculkan kejadian RenderItemImage.

OnRenderItemText(ToolStripItemTextRenderEventArgs)

Memunculkan kejadian RenderItemText.

OnRenderLabelBackground(ToolStripItemRenderEventArgs)

Memunculkan kejadian RenderLabelBackground.

OnRenderMenuItemBackground(ToolStripItemRenderEventArgs)

Memunculkan kejadian RenderMenuItemBackground.

OnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs)

Memunculkan kejadian RenderOverflowButtonBackground.

OnRenderSeparator(ToolStripSeparatorRenderEventArgs)

Memunculkan kejadian RenderSeparator.

OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs)

Memunculkan kejadian OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs).

OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs)

Memunculkan kejadian RenderStatusStripSizingGrip.

OnRenderToolStripBackground(ToolStripRenderEventArgs)

Memunculkan kejadian RenderToolStripBackground.

OnRenderToolStripBorder(ToolStripRenderEventArgs)

Memunculkan kejadian RenderToolStripBorder.

OnRenderToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs)

Memunculkan kejadian RenderToolStripContentPanelBackground.

OnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs)

Memunculkan kejadian RenderToolStripPanelBackground.

OnRenderToolStripStatusLabelBackground(ToolStripItemRenderEventArgs)

Memunculkan kejadian RenderToolStripStatusLabelBackground.

ScaleArrowOffsetsIfNeeded()

Menerapkan nilai dan Offset2Y untuk menskalakan ikon panah, jika penskalaan Offset2X diperlukan berdasarkan pengaturan DPI komputer.

ScaleArrowOffsetsIfNeeded(Int32)

Menerapkan nilai dan Offset2Y untuk menskalakan Offset2X ikon panah berdasarkan nilai DPI yang ditentukan.

ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Acara

RenderArrow

Terjadi ketika panah pada ToolStripItem dirender.

RenderButtonBackground

Terjadi ketika latar belakang untuk dirender ToolStripButton .

RenderDropDownButtonBackground

Terjadi ketika latar belakang untuk dirender ToolStripDropDownButton .

RenderGrip

Terjadi ketika handel pemindahan untuk ToolStrip dirender.

RenderImageMargin

Menggambar margin antara gambar dan kontainernya.

RenderItemBackground

Terjadi ketika latar belakang untuk dirender ToolStripItem .

RenderItemCheck

Terjadi ketika gambar untuk yang dipilih ToolStripItem dirender.

RenderItemImage

Terjadi ketika gambar untuk ToolStripItem dirender.

RenderItemText

Terjadi ketika teks untuk dirender ToolStripItem .

RenderLabelBackground

Terjadi ketika latar belakang untuk dirender ToolStripLabel .

RenderMenuItemBackground

Terjadi ketika latar belakang untuk dirender ToolStripMenuItem .

RenderOverflowButtonBackground

Terjadi ketika latar belakang untuk tombol luapan dirender.

RenderSeparator

Terjadi ketika dirender ToolStripSeparator .

RenderSplitButtonBackground

Terjadi ketika latar belakang untuk dirender ToolStripSplitButton .

RenderStatusStripSizingGrip

Terjadi ketika gaya tampilan berubah.

RenderToolStripBackground

Terjadi ketika latar belakang untuk dirender ToolStrip .

RenderToolStripBorder

Terjadi ketika batas untuk dirender ToolStrip .

RenderToolStripContentPanelBackground

Menggambar latar belakang ToolStripContentPanel.

RenderToolStripPanelBackground

Menggambar latar belakang ToolStripPanel.

RenderToolStripStatusLabelBackground

Menggambar latar belakang ToolStripStatusLabel.

Berlaku untuk

Lihat juga