c#之menustrip_toolstripmenuitem小記

wisdomone1發表於2011-08-28
//如下示例程式碼會顯示一個選單,共有4個小選單項,每個選單項各對應4個選單項

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication6
{
    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.
             //dropdown事件
            bothMargins.DropDown = CreateCheckImageContextMenuStrip();
          
                //contextmenustrip類的學習
            ((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.
              //透過menustrip.items.add用選單窗器類新增子物件 子選單項 ToolStripMenuItem
             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.
               //把選單容器物件menustrip新增到窗體中 form.controls.add
            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的用法在此何義
            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;
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-706041/,如需轉載,請註明出處,否則將追究法律責任。

相關文章