Compartir a través de


Cómo: Habilitar los márgenes de comprobación y de imagen en los controles ContextMenuStrip

Actualización: noviembre 2007

Puede personalizar los objetos ToolStripMenuItem en el control MenuStrip con marcas de verificación e imágenes personalizadas.

Ejemplo

En el ejemplo de código siguiente se muestra cómo crear elementos de menú que tienen marcas de verificación e imágenes personalizadas.

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


...


' This code example demonstrates how to set the check
' and image margins for a ToolStripMenuItem.
Class Form5
   Inherits Form

   Public Sub New()
      ' Size the form to show three wide menu items.
      Me.Width = 500
      Me.Text = "ToolStripContextMenuStrip: Image and Check Margins"

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

      ' Create the ToolStripMenuItems for the MenuStrip control.
      Dim bothMargins As New ToolStripMenuItem("BothMargins")
      Dim imageMarginOnly As New ToolStripMenuItem("ImageMargin")
      Dim checkMarginOnly As New ToolStripMenuItem("CheckMargin")
      Dim noMargins As New ToolStripMenuItem("NoMargins")

      ' Customize the DropDowns menus.
      ' This ToolStripMenuItem has an image margin 
      ' and a check margin.
      bothMargins.DropDown = CreateCheckImageContextMenuStrip()
      CType(bothMargins.DropDown, ContextMenuStrip).ShowImageMargin = True
      CType(bothMargins.DropDown, ContextMenuStrip).ShowCheckMargin = True

      ' This ToolStripMenuItem has only an image margin.
      imageMarginOnly.DropDown = CreateCheckImageContextMenuStrip()
      CType(imageMarginOnly.DropDown, ContextMenuStrip).ShowImageMargin = True
      CType(imageMarginOnly.DropDown, ContextMenuStrip).ShowCheckMargin = False

      ' This ToolStripMenuItem has only a check margin.
      checkMarginOnly.DropDown = CreateCheckImageContextMenuStrip()
      CType(checkMarginOnly.DropDown, ContextMenuStrip).ShowImageMargin = False
      CType(checkMarginOnly.DropDown, ContextMenuStrip).ShowCheckMargin = True

      ' This ToolStripMenuItem has no image and no check margin.
      noMargins.DropDown = CreateCheckImageContextMenuStrip()
      CType(noMargins.DropDown, ContextMenuStrip).ShowImageMargin = False
      CType(noMargins.DropDown, ContextMenuStrip).ShowCheckMargin = False

      ' Populate the MenuStrip control with the ToolStripMenuItems.
      ms.Items.Add(bothMargins)
      ms.Items.Add(imageMarginOnly)
      ms.Items.Add(checkMarginOnly)
      ms.Items.Add(noMargins)

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

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

   ' This utility method creates a Bitmap for use in 
   ' a ToolStripMenuItem's image margin.
    Friend Function CreateSampleBitmap() As Bitmap

        ' The Bitmap is a smiley face.
        Dim sampleBitmap As New Bitmap(32, 32)
        Dim g As Graphics = Graphics.FromImage(sampleBitmap)

        Dim p As New Pen(ProfessionalColors.ButtonPressedBorder)
        Try
            ' Set the Pen width.
            p.Width = 4

            ' Set up the mouth geometry.
            Dim curvePoints() As Point = _
            {New Point(4, 14), New Point(16, 24), New Point(28, 14)}

            ' Draw the mouth.
            g.DrawCurve(p, curvePoints)

            ' Draw the eyes.
            g.DrawEllipse(p, New Rectangle(New Point(7, 4), New Size(3, 3)))
            g.DrawEllipse(p, New Rectangle(New Point(22, 4), New Size(3, 3)))
        Finally
            p.Dispose()
        End Try

        Return sampleBitmap
    End Function

   ' This utility method creates a ContextMenuStrip control
   ' that has four ToolStripMenuItems showing the four 
   ' possible combinations of image and check margins.
   Friend Function CreateCheckImageContextMenuStrip() As ContextMenuStrip
      ' Create a new ContextMenuStrip control.
      Dim checkImageContextMenuStrip As New ContextMenuStrip()

      ' Create a ToolStripMenuItem with a
      ' check margin and an image margin.
      Dim yesCheckYesImage As New ToolStripMenuItem("Check, Image")
      yesCheckYesImage.Checked = True
      yesCheckYesImage.Image = CreateSampleBitmap()

      ' Create a ToolStripMenuItem with no
      ' check margin and with an image margin.
      Dim noCheckYesImage As New ToolStripMenuItem("No Check, Image")
      noCheckYesImage.Checked = False
      noCheckYesImage.Image = CreateSampleBitmap()

      ' Create a ToolStripMenuItem with a
      ' check margin and without an image margin.
      Dim yesCheckNoImage As New ToolStripMenuItem("Check, No Image")
      yesCheckNoImage.Checked = True

      ' Create a ToolStripMenuItem with no
      ' check margin and no image margin.
      Dim noCheckNoImage As New ToolStripMenuItem("No Check, No Image")
      noCheckNoImage.Checked = False

      ' Add the ToolStripMenuItems to the ContextMenuStrip control.
      checkImageContextMenuStrip.Items.Add(yesCheckYesImage)
      checkImageContextMenuStrip.Items.Add(noCheckYesImage)
      checkImageContextMenuStrip.Items.Add(yesCheckNoImage)
      checkImageContextMenuStrip.Items.Add(noCheckNoImage)

      Return checkImageContextMenuStrip
    End Function
End Class
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;


...


// This code example demonstrates how to set the check
// and image margins for a ToolStripMenuItem.
class Form5 : Form
{
    public Form5()
    {
        // Size the form to show three wide menu items.
        this.Width = 500;
        this.Text = "ToolStripContextMenuStrip: Image and Check Margins";

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

        // Create the ToolStripMenuItems for the MenuStrip control.
        ToolStripMenuItem bothMargins = new ToolStripMenuItem("BothMargins");
        ToolStripMenuItem imageMarginOnly = new ToolStripMenuItem("ImageMargin");
        ToolStripMenuItem checkMarginOnly = new ToolStripMenuItem("CheckMargin");
        ToolStripMenuItem noMargins = new ToolStripMenuItem("NoMargins");

        // Customize the DropDowns menus.
        // This ToolStripMenuItem has an image margin 
        // and a check margin.
        bothMargins.DropDown = CreateCheckImageContextMenuStrip();
        ((ContextMenuStrip)bothMargins.DropDown).ShowImageMargin = true;
        ((ContextMenuStrip)bothMargins.DropDown).ShowCheckMargin = true;

        // This ToolStripMenuItem has only an image margin.
        imageMarginOnly.DropDown = CreateCheckImageContextMenuStrip();
        ((ContextMenuStrip)imageMarginOnly.DropDown).ShowImageMargin = true;
        ((ContextMenuStrip)imageMarginOnly.DropDown).ShowCheckMargin = false;

        // This ToolStripMenuItem has only a check margin.
        checkMarginOnly.DropDown = CreateCheckImageContextMenuStrip();
        ((ContextMenuStrip)checkMarginOnly.DropDown).ShowImageMargin = false;
        ((ContextMenuStrip)checkMarginOnly.DropDown).ShowCheckMargin = true;

        // This ToolStripMenuItem has no image and no check margin.
        noMargins.DropDown = CreateCheckImageContextMenuStrip();
        ((ContextMenuStrip)noMargins.DropDown).ShowImageMargin = false;
        ((ContextMenuStrip)noMargins.DropDown).ShowCheckMargin = false;

        // Populate the MenuStrip control with the ToolStripMenuItems.
        ms.Items.Add(bothMargins);
        ms.Items.Add(imageMarginOnly);
        ms.Items.Add(checkMarginOnly);
        ms.Items.Add(noMargins);

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

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

    // This utility method creates a Bitmap for use in 
    // a ToolStripMenuItem's image margin.
    internal Bitmap CreateSampleBitmap()
    {
        // The Bitmap is a smiley face.
        Bitmap sampleBitmap = new Bitmap(32, 32);
        Graphics g = Graphics.FromImage(sampleBitmap);

        using (Pen p = new Pen(ProfessionalColors.ButtonPressedBorder))
        {
            // Set the Pen width.
            p.Width = 4;

            // Set up the mouth geometry.
            Point[] curvePoints = new Point[]{
                new Point(4,14), 
                new Point(16,24), 
                new Point(28,14)};

            // Draw the mouth.
            g.DrawCurve(p, curvePoints);

            // Draw the eyes.
            g.DrawEllipse(p, new Rectangle(new Point(7, 4), new Size(3, 3)));
            g.DrawEllipse(p, new Rectangle(new Point(22, 4), new Size(3, 3)));
        }

        return sampleBitmap;
    }

    // This utility method creates a ContextMenuStrip control
    // that has four ToolStripMenuItems showing the four 
    // possible combinations of image and check margins.
    internal ContextMenuStrip CreateCheckImageContextMenuStrip()
    {
        // Create a new ContextMenuStrip control.
        ContextMenuStrip checkImageContextMenuStrip = new ContextMenuStrip();

        // Create a ToolStripMenuItem with a
        // check margin and an image margin.
        ToolStripMenuItem yesCheckYesImage = 
            new ToolStripMenuItem("Check, Image");
        yesCheckYesImage.Checked = true;
        yesCheckYesImage.Image = CreateSampleBitmap();

        // Create a ToolStripMenuItem with no
        // check margin and with an image margin.
        ToolStripMenuItem noCheckYesImage = 
            new ToolStripMenuItem("No Check, Image");
        noCheckYesImage.Checked = false;
        noCheckYesImage.Image = CreateSampleBitmap();

        // Create a ToolStripMenuItem with a
        // check margin and without an image margin.
        ToolStripMenuItem yesCheckNoImage = 
            new ToolStripMenuItem("Check, No Image");
        yesCheckNoImage.Checked = true;

        // Create a ToolStripMenuItem with no
        // check margin and no image margin.
        ToolStripMenuItem noCheckNoImage = 
            new ToolStripMenuItem("No Check, No Image");
        noCheckNoImage.Checked = false;

        // Add the ToolStripMenuItems to the ContextMenuStrip control.
        checkImageContextMenuStrip.Items.Add(yesCheckYesImage);
        checkImageContextMenuStrip.Items.Add(noCheckYesImage);
        checkImageContextMenuStrip.Items.Add(yesCheckNoImage);
        checkImageContextMenuStrip.Items.Add(noCheckNoImage);

        return checkImageContextMenuStrip;
    }
}

Establezca las propiedades ToolStripDropDownMenu.ShowCheckMargin y ToolStripDropDownMenu.ShowImageMargin para especificar cuándo aparecerán las marcas de verificación y las imágenes personalizadas en los elementos de menú.

Compilar el código

Este ejemplo requiere:

  • Referencias a los ensamblados System.Design, System.Drawing y System.Windows.Forms.

Para obtener información sobre la generación de este ejemplo desde la línea de comandos de Visual Basic o Visual C#, vea Generar desde la línea de comandos (Visual Basic) o Generar la línea de comandos con csc.exe. También puede generar este ejemplo en Visual Studio pegando el código en un proyecto nuevo.

Vea también

Referencia

ToolStripMenuItem

ToolStripDropDownMenu

MenuStrip

ToolStrip

Otros recursos

ToolStrip (Control de formularios Windows Forms)