Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
Tradução
Original
Este tópico ainda não foi avaliado como - Avalie este tópico

Classe ToolStripItem

Representa a classe base abstrata que gerencia eventos e o layout para todos os elementos que um ToolStrip ou ToolStripDropDown pode conter.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (em System.Windows.Forms.dll)
public abstract class ToolStripItem : Component, 
	IDropTarget, IComponent, IDisposable

A ToolStripItem é um elemento, sistema autônomo um botão, caixa de combinação, caixa de texto ou rótulo que pode estar contido em um ToolStrip controle ou um ToolStripDropDown controle, que é semelhante a um menu de atalho do Windows. The ToolStrip classe gerencia a pintura e teclado e mouse de entrada, incluindo do tipo arrastar e soltar de entrada, para esses elementos e o ToolStripItem classe gerencia eventos e layout em próprios elementos.

ToolStripItem classes, herdam diretamente do ToolStripItem, ou indiretamente herdam de ToolStripItem por meio de ToolStripControlHost ou ToolStripDropDownItem.

ToolStripItem controles devem estar contidas em um ToolStrip, MenuStrip, StatusStrip, ou ContextMenuStrip e não podem ser adicionados diretamente a um formulário. As várias classes de contêiner foram projetadas para conter um subconjunto apropriado de ToolStripItem controles.

Observação:   Um determinado ToolStripItem não é possível ter mais de um pai ToolStrip. Você deve copiar do ToolStripItem e adicioná-lo para Outros ToolStrip controles.

A tabela a seguir mostra os elementos que derivam de ToolStripItem classe e que, portanto, pode ser hospedado em um ToolStrip ou ToolStripDropDown.

Elemento

Descrição

ToolStripButton

Um botão de barra de ferramentas que oferece suporte a imagens e texto.

ToolStripLabel

Um rótulo de texto usado normalmente em uma BAR de status ou ToolStrip sistema autônomo um comentário ou título.

ToolStripSeparator

Um espaço não-selecionável ou espaço com uma BAR vertical que agrupa visualmente elementos.

ToolStripControlHost

A ToolStripItem que hospeda um ToolStripComboBox, ToolStripTextBox, ToolStripProgressBar, outros controles Windows Forms ou controles personalizados.

A ToolStripComboBox é uma caixa de texto em que o usuário pode digitar texto, juntamente com uma lista na qual o usuário pode selecionar texto preencha a caixa de texto.

A ToolStripTextBox permite que o usuário inserir texto.

A ToolStripProgressBar representa um controle da BAR de progresso Windows contido em um StatusStrip.

ToolStripDropDownItem

A ToolStripItem que hospeda um ToolStripMenuItem, ToolStripSplitButton, e ToolStripDropDownButton.

A ToolStripMenuItem uma opção selecionável é exibida em um menu ou menu de contexto.

A ToolStripSplitButton é uma combinação de um botão regular e um botão drop-down.

A ToolStripDropDownButton é um botão que oferece suporte à funcionalidade de lista suspensa.

ToolStripStatusLabel

Um painel em um StatusStrip controle.

O exemplo de código a seguir demonstra como implementar um personalizado ToolStripItem controle.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace RolloverItemDemoLib
{
    // This class implements a ToolStripItem that highlights// its border and text when the mouse enters enters its// client rectangle. It has a clickable state which is// exposed through the Clicked property and displayed// by highlighting or graying out the item's image. publicclass RolloverItem : ToolStripItem
    {
        privatebool clickedValue = false;
        privatebool rolloverValue = false;

        private Rectangle imageRect;
        private Rectangle textRect;

        // For brevity, this implementation limits the possible // TextDirection values to ToolStripTextDirection.Horizontal. publicoverride ToolStripTextDirection TextDirection
        {
            get
            {
                returnbase.TextDirection;
            }
            set
            {
                if (value == ToolStripTextDirection.Horizontal)
                {
                    base.TextDirection = value;
                }
                else
                {
                    thrownew ArgumentException(
                        "RolloverItem supports only horizontal text.");
                }
            }
        }

        // For brevity, this implementation limits the possible // TextImageRelation values to ImageBeforeText and TextBeforeImage. publicnew TextImageRelation TextImageRelation
        {
            get
            {
                returnbase.TextImageRelation;
            }

            set
            {
                if (value == TextImageRelation.ImageBeforeText || 
                    value == TextImageRelation.TextBeforeImage)
                {
                    base.TextImageRelation = value;
                }
                else
                {
                    thrownew ArgumentException(
                        "Unsupported TextImageRelation value.");
                }
            }
        }

        // This property returns true if the mouse is // inside the client rectangle.publicbool Rollover
        {
            get
            {
                returnthis.rolloverValue;
            }   
        }

        // This property returns true if the item // has been toggled into the clicked state.// Clicking again toggles it to the // unclicked state.publicbool Clicked
        {
            get
            {
                returnthis.clickedValue;
            }
        }

        // The method defines the behavior of the Click event.// It simply toggles the state of the clickedValue field.protectedoverridevoid OnClick(EventArgs e)
        {
            base.OnClick(e);

            this.clickedValue ^= true;
        }

        // The method defines the behavior of the DoubleClick // event. It shows a MessageBox with the item's text.protectedoverridevoid OnDoubleClick(EventArgs e)
        {
            base.OnDoubleClick(e);

            string msg = String.Format("Item: {0}", this.Text);

            MessageBox.Show(msg);
        }

        // This method defines the behavior of the MouseEnter event.// It sets the state of the rolloverValue field to true and// tells the control to repaint.protectedoverridevoid OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);

            this.rolloverValue = true;

            this.Invalidate();
        }

        // This method defines the behavior of the MouseLeave event.// It sets the state of the rolloverValue field to false and// tells the control to repaint.protectedoverridevoid OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            this.rolloverValue = false;

            this.Invalidate();
        }

        // This method defines the painting behavior of the control.// It performs the following operations://// Computes the layout of the item's image and text.// Draws the item's background image.// Draws the item's image.// Draws the item's text.//// Drawing operations are implemented in the // RolloverItemRenderer class.protectedoverridevoid OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (this.Owner != null)
            {
                // Find the dimensions of the image and the text // areas of the item. this.ComputeImageAndTextLayout();

                // Draw the background. This includes drawing a highlighted // border when the mouse is in the client area.
                ToolStripItemRenderEventArgs ea = new ToolStripItemRenderEventArgs(
                     e.Graphics,
                     this);
                this.Owner.Renderer.DrawItemBackground(ea);

                // Draw the item's image. 
                ToolStripItemImageRenderEventArgs irea =
                    new ToolStripItemImageRenderEventArgs(
                    e.Graphics,
                    this,
                    imageRect );
                this.Owner.Renderer.DrawItemImage(irea);

                // If the item is on a drop-down, give its// text a different highlighted color.
                Color highlightColor = 
                    this.IsOnDropDown ?
                    Color.Salmon : SystemColors.ControlLightLight;

                // Draw the text, and highlight it if the // the rollover state is true.
                ToolStripItemTextRenderEventArgs rea =
                    new ToolStripItemTextRenderEventArgs(
                    e.Graphics,
                    this,
                    base.Text,
                    textRect,
                    this.rolloverValue ? highlightColor : base.ForeColor,
                    base.Font,
                    base.TextAlign);
                this.Owner.Renderer.DrawItemText(rea);
            }
        }

        // This utility method computes the layout of the // RolloverItem control's image area and the text area.// For brevity, only the following settings are // supported://// ToolStripTextDirection.Horizontal// TextImageRelation.ImageBeforeText // TextImageRelation.ImageBeforeText// // It would not be difficult to support vertical text// directions and other image/text relationships.privatevoid ComputeImageAndTextLayout()
        {
            Rectangle cr = base.ContentRectangle;
            Image img = base.Owner.ImageList.Images[base.ImageKey];

            // Compute the center of the item's ContentRectangle.int centerY = (cr.Height - img.Height) / 2;

            // Find the dimensions of the image and the text // areas of the item. The text occupies the space // not filled by the image. if (base.TextImageRelation == TextImageRelation.ImageBeforeText &&
                base.TextDirection == ToolStripTextDirection.Horizontal)
            {
                imageRect = new Rectangle(
                    base.ContentRectangle.Left,
                    centerY,
                    base.Image.Width,
                    base.Image.Height);

                textRect = new Rectangle(
                    imageRect.Width,
                    base.ContentRectangle.Top,
                    base.ContentRectangle.Width - imageRect.Width,
                    base.ContentRectangle.Height);
            }
            elseif (base.TextImageRelation == TextImageRelation.TextBeforeImage &&
                     base.TextDirection == ToolStripTextDirection.Horizontal)
            {
                imageRect = new Rectangle(
                    base.ContentRectangle.Right - base.Image.Width,
                    centerY,
                    base.Image.Width,
                    base.Image.Height);

                textRect = new Rectangle(
                    base.ContentRectangle.Left,
                    base.ContentRectangle.Top,
                    imageRect.X,
                    base.ContentRectangle.Bottom);
            }
        }
    }

    #region RolloverItemRenderer

    // This is the custom renderer for the RolloverItem control.// It draws a border around the item when the mouse is// in the item's client area. It also draws the item's image// in an inactive state (grayed out) until the user clicks// the item to toggle its "clicked" state.
    internalclass RolloverItemRenderer : ToolStripSystemRenderer
    {
        protectedoverridevoid OnRenderItemImage(
            ToolStripItemImageRenderEventArgs e)
        {
            base.OnRenderItemImage(e);

            RolloverItem item = e.Item as RolloverItem;

            // If the ToolSTripItem is of type RolloverItem, // perform custom rendering for the image.if (item != null)
            {
                if (item.Clicked)
                {
                    // The item is in the clicked state, so // draw the image as usual.
                    e.Graphics.DrawImage(
                        e.Image,
                        e.ImageRectangle.X,
                        e.ImageRectangle.Y);
                }
                else
                {
                    // In the unclicked state, gray out the image.
                    ControlPaint.DrawImageDisabled(
                        e.Graphics,
                        e.Image,
                        e.ImageRectangle.X,
                        e.ImageRectangle.Y,
                        item.BackColor);
                }
            }
        }

        // This method defines the behavior for rendering the// background of a ToolStripItem. If the item is a// RolloverItem, it paints the item's BackgroundImage // centered in the client area. If the mouse is in the // item's client area, a border is drawn around it.// If the item is on a drop-down or if it is on the// overflow, a gradient is painted in the background.protectedoverridevoid OnRenderItemBackground(
            ToolStripItemRenderEventArgs e)
        {
            base.OnRenderItemBackground(e);

            RolloverItem item = e.Item as RolloverItem;

            // If the ToolSTripItem is of type RolloverItem, // perform custom rendering for the background.if (item != null)
            {
                if (item.Placement == ToolStripItemPlacement.Overflow ||
                    item.IsOnDropDown)
                {
                    using (LinearGradientBrush b = new LinearGradientBrush(
                        item.ContentRectangle,
                        Color.Salmon,
                        Color.DarkRed,
                        0f,
                        false))
                    {
                        e.Graphics.FillRectangle(b, item.ContentRectangle);
                    }
                }

                // The RolloverItem control only supports // the ImageLayout.Center setting for the// BackgroundImage property.if (item.BackgroundImageLayout == ImageLayout.Center)
                {
                    // Get references to the item's ContentRectangle// and BackgroundImage, for convenience.
                    Rectangle cr = item.ContentRectangle;
                    Image bgi = item.BackgroundImage;

                    // Compute the center of the item's ContentRectangle.int centerX = (cr.Width - bgi.Width) / 2;
                    int centerY = (cr.Height - bgi.Height) / 2;

                    // If the item is selected, draw the background// image as usual. Otherwise, draw it as disabled.if (item.Selected)
                    {
                        e.Graphics.DrawImage(bgi, centerX, centerY);
                    }
                    else
                    {
                        ControlPaint.DrawImageDisabled(
                                e.Graphics,
                                bgi,
                                centerX,
                                centerY,
                                item.BackColor);
                    }
                }

                // If the item is in the rollover state, // draw a border around it.if (item.Rollover)
                {
                    ControlPaint.DrawFocusRectangle(
                        e.Graphics,
                        item.ContentRectangle);
                }
            }
        }

    #endregion

    }

    // This form tests various features of the RolloverItem// control. RolloverItem conrols are created and added// to the form's ToolStrip. They are also created and // added to a button's ContextMenuStrip. The behavior// of the RolloverItem control differs depending on // the type of parent control.publicclass RolloverItemTestForm : Form
    {
        private System.Windows.Forms.ToolStrip toolStrip1;
        private System.Windows.Forms.Button button1;

        private string infoIconKey = "Information icon";
        private string handIconKey = "Hand icon";
        private string exclIconKey = "Exclamation icon";
        private string questionIconKey = "Question icon";
        private string warningIconKey = "Warning icon ";

        private System.ComponentModel.IContainer components = null;

        public RolloverItemTestForm()
        {
            InitializeComponent();

            // Set up the form's ToolStrip control.
            InitializeToolStrip();

            // Set up the ContextMenuStrip for the button.
            InitializeContextMenu();
        }

        // This utility method initializes the ToolStrip control's // image list. For convenience, icons from the SystemIcons // class are used for this demonstration, but any images// could be used.privatevoid InitializeImageList(ToolStrip ts)
        {
            if (ts.ImageList == null)
            {
                ts.ImageList = new ImageList();
                ts.ImageList.ImageSize = SystemIcons.Exclamation.Size;

                ts.ImageList.Images.Add(
                    this.infoIconKey,
                    SystemIcons.Information);

                ts.ImageList.Images.Add(
                    this.handIconKey,
                    SystemIcons.Hand);

                ts.ImageList.Images.Add(
                    this.exclIconKey,
                    SystemIcons.Exclamation);

                ts.ImageList.Images.Add(
                    this.questionIconKey,
                    SystemIcons.Question);

                ts.ImageList.Images.Add(
                    this.warningIconKey,
                    SystemIcons.Warning);
            }
        }

        privatevoid InitializeToolStrip()
        {
            this.InitializeImageList(this.toolStrip1);

            this.toolStrip1.Renderer = new RolloverItemRenderer();

            RolloverItem item = this.CreateRolloverItem(
                this.toolStrip1,
                "RolloverItem on ToolStrip",
                this.Font,
                infoIconKey,
                TextImageRelation.ImageBeforeText,
                exclIconKey);

            this.toolStrip1.Items.Add(item);

            item = this.CreateRolloverItem(
                this.toolStrip1,
                "RolloverItem on ToolStrip",
                this.Font,
                infoIconKey,
                TextImageRelation.ImageBeforeText,
                exclIconKey);

            this.toolStrip1.Items.Add(item);
        }

        privatevoid InitializeContextMenu()
        {
            Font f = new System.Drawing.Font(
                "Arial",
                18f,
                FontStyle.Bold);

            ContextMenuStrip cms = new ContextMenuStrip();
            this.InitializeImageList(cms);

            cms.Renderer = new RolloverItemRenderer();
            cms.AutoSize = true;
            cms.ShowCheckMargin = false;
            cms.ShowImageMargin = false;

            RolloverItem item = this.CreateRolloverItem(
                cms,
                "RolloverItem on ContextMenuStrip",
                f,
                handIconKey,
                TextImageRelation.ImageBeforeText,
                exclIconKey);

            cms.Items.Add(item);

            item = this.CreateRolloverItem(
                cms,
                "Another RolloverItem on ContextMenuStrip",
                f,
                questionIconKey,
                TextImageRelation.ImageBeforeText,
                exclIconKey);

            cms.Items.Add(item);

            item = this.CreateRolloverItem(
                cms,
                "And another RolloverItem on ContextMenuStrip",
                f,
                warningIconKey,
                TextImageRelation.ImageBeforeText,
                exclIconKey);

            cms.Items.Add(item);

            cms.Closing += new ToolStripDropDownClosingEventHandler(cms_Closing);

            this.button1.ContextMenuStrip = cms;
        }

        // This method handles the ContextMenuStrip // control's Closing event. It prevents the // RolloverItem from closing the drop-down  // when the item is clicked.void cms_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
            {
                e.Cancel = true;
            }
        }

        // This method handles the Click event for the button.// it selects the first item in the ToolStrip control// by using the ToolStripITem.Select method.privatevoid button1_Click(object sender, EventArgs e)
        {
            RolloverItem item = this.toolStrip1.Items[0] as RolloverItem;

            if (item != null)
            {
                item.Select();

                this.Invalidate();
            }
        }

        // This utility method creates a RolloverItem // and adds it to a ToolStrip control.private RolloverItem CreateRolloverItem(
            ToolStrip owningToolStrip,
            string txt,
            Font f,
            string imgKey,
            TextImageRelation tir,
            string backImgKey)
        {
            RolloverItem item = new RolloverItem();

            item.Alignment = ToolStripItemAlignment.Left;
            item.AllowDrop = false;
            item.AutoSize = true;

            item.BackgroundImage = owningToolStrip.ImageList.Images[backImgKey];
            item.BackgroundImageLayout = ImageLayout.Center;
            item.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            item.DoubleClickEnabled = true;
            item.Enabled = true;
            item.Font = f;

            // These assignments are equivalent. Each assigns an// image from the owning toolstrip's image list.
            item.ImageKey = imgKey;
            //item.Image = owningToolStrip.ImageList.Images[infoIconKey];//item.ImageIndex = owningToolStrip.ImageList.Images.IndexOfKey(infoIconKey);
            item.ImageScaling = ToolStripItemImageScaling.None;

            item.Owner = owningToolStrip;
            item.Padding = new Padding(2);
            item.Text = txt;
            item.TextAlign = ContentAlignment.MiddleLeft;
            item.TextDirection = ToolStripTextDirection.Horizontal;
            item.TextImageRelation = tir;

            return item;
        }

        protectedoverridevoid Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        privatevoid InitializeComponent()
        {
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // // toolStrip1// this.toolStrip1.AllowItemReorder = true;
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(845, 25);
            this.toolStrip1.TabIndex = 0;
            this.toolStrip1.Text = "toolStrip1";
            // // button1// this.button1.Location = new System.Drawing.Point(12, 100);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(86, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Click to select";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // // RolloverItemTestForm// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 14F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.ClientSize = new System.Drawing.Size(845, 282);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.toolStrip1);
            this.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "RolloverItemTestForm";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
    }

    staticclass Program
    {   
        [STAThread]
        staticvoid Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new RolloverItemTestForm());
        }
    }

}


Quaisquer membros static (Shared no Visual Basic) públicos deste tipo são thread-safe. Não há garantia de que qualquer membro de instância seja thread-safe.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

o.NET Framework e.NET Compact Framework não oferecem suporte a todas as versões de cada plataforma. Para obter uma lista de versões suportadas, consulte Requisitos de sistema do .NET framework.

.NET Framework

Compatível com: 3.5, 3.0, 2.0
Isso foi útil para você?
(1500 caracteres restantes)
Conteúdo da Comunidade Adicionar