c#之menustrip_toolstripmenuitem小记

//如下示例代码会显示一个菜单,共有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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9240380/viewspace-706041/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值