MenuItem

private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            MessageBox.Show("打开测试");

            //Close();

        }



        private void button1_Click(object sender, EventArgs e)

        {

            //表示 System.Windows.Forms.MenuStrip 或 System.Windows.Forms.ContextMenuStrip 上显示的可选选项。 尽管 System.Windows.Forms.ToolStripMenuItem 替换了早期版本的 System.Windows.Forms.MenuItem 控件并添加了功能;但是也可选择保留 System.Windows.Forms.MenuItem 以备向后兼容和将来使用。

            ToolStripMenuItem ms =(ToolStripMenuItem)menuStrip1.Items[0];//获取菜单项

            foreach (ToolStripItem item in ms.DropDownItems)

            {

                item.Enabled = true;

                item.Font=new Font("楷体",12,FontStyle.Bold);

            }

        }



        private void button2_Click(object sender, EventArgs e)

        {

            ToolStripMenuItem ms = (ToolStripMenuItem) menuStrip1.Items[0];

            foreach (ToolStripMenuItem item in ms.DropDownItems)

            {

                item.Enabled = false;

            }



        }



        private void Form1_Load(object sender, EventArgs e)

        {

            //创建级联菜单  每一个菜单项都是一个ToolStripMnubItem对象

            ToolStripMenuItem ms =(ToolStripMenuItem)menuStrip1.Items[1];

            ToolStripMenuItem ts1=new ToolStripMenuItem("关闭文件选项");

            //ToolStripMenuItem ts2=new ToolStripMenuItem("关闭文件选项");

            //ToolStripMenuItem ts3=new ToolStripMenuItem("关闭所有选项");

            ToolStripMenuItem[]ts=new ToolStripMenuItem[]

                {

                    new ToolStripMenuItem("关闭文件选项"),

                    new ToolStripMenuItem("关闭所有选项")

                };

            ms.DropDownItems.Add(ts1);

            ms.DropDownItems.AddRange(ts);

        }


MSDN:http://msdn.microsoft.com/zh-cn/library/vstudio/system.windows.forms.menustrip(v=vs.90).aspx

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;



namespace _274ContextMenuStrip

{

    public partial class Form1 : Form

    {

        private string address;

        public Form1()

        {

            InitializeComponent();

            address = Environment.CurrentDirectory;//记录地址

        }

        private void 打开新窗体ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            Form2 fm=new Form2();

            fm.MdiParent = this;

            fm.Show();

            fm.Resize+=new EventHandler(f_Resize);

        }



        void f_Resize(object sender,EventArgs e)

        {

            Form2 fm = (Form2)sender;//将Form2作为对象传进来

            ToolStripMenuItem item = new ToolStripMenuItem();

            for (int i = 0; i < fm.contextMenuStrip1.Items.Count; )

            {

                item.DropDownItems.Add(fm.contextMenuStrip1.Items[i]);//添加鼠标右键

            }

            this.contextMenuStrip2.Items.AddRange(

                    new System.Windows.Forms.ToolStripItem[] { item }

                );

        }

        private void button1_Click(object sender, EventArgs e)

        {

            MessageBox.Show(sender.ToString());//测试验证 sender对象

        }



        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }



        private void 关闭所有ToolStripMenuItem_Click(object sender, EventArgs e)

        {

           

        }



        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            openFileDialog1.FileName = "*.jpg";//限定打开文件类型

            openFileDialog1.ShowDialog();//弹出选择对话框

            StreamWriter s=new StreamWriter(address+"\\History.ini",true);//初始化新实例

            s.WriteLine(openFileDialog1.FileName);//写入

            s.Flush();//清除流

            s.Close();//关闭流

            ShowWindows(openFileDialog1.FileName);

        }



        private void ShowWindows(string fileName)

        {

            Image p = Image.FromFile(fileName);

            Form f=new Form();

            f.MdiParent = this;

            f.BackgroundImage = p;

            f.Show();

        }



        private void Form1_Load(object sender, EventArgs e)

        {

            this.IsMdiContainer = true;

            

            文件ToolStripMenuItem.DropDownItems[1].Image = imageList1.Images[0];



            using (StreamReader sr = new StreamReader(address + "\\History.ini"))//创建流读取器对象

            {

                int i = 文件ToolStripMenuItem.DropDownItems.Count - 2;//得到菜单项索引

                while (sr.Peek() >= 0)//循环读取流中的文本

                {

                    ToolStripMenuItem menuItem = new ToolStripMenuItem(sr.ReadLine());//创建菜单项对象

                    this.文件ToolStripMenuItem.DropDownItems.Insert(i, menuItem);//向菜单中添加新项

                    i++;//增加索引位置

                    menuItem.Click += new EventHandler(menuItem_Click);//添加单击事件

                }

            }

        }



        private void menuItem_Click(object sender, EventArgs e)

        {

            ToolStripMenuItem menu = (ToolStripMenuItem) sender;

            ShowWindows(menu.Text);

        }



        private void acToolStripMenuItem_Click(object sender, EventArgs e)

        {

            

        }



      

    }

}

 

你可能感兴趣的:(menu)