using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Reflection; using System.ComponentModel; namespace WinFormLib { public class MenuClone { //使用例子 //private void Form1_Load(object sender, EventArgs e) //{ // // this.contextMenuStrip1.Items.Clear(); // MenuClone mc = new MenuClone(); // this.contextMenuStrip1.Items.Add(mc.CloneToolStripMenuItem(文件ToolStripMenuItem1, true)); //} //private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e) //{ // 退出ToolStripMenuItem1.Checked = !退出ToolStripMenuItem1.Checked; // this.contextMenuStrip1.Items.Clear(); // MenuClone mc = new MenuClone(); // this.contextMenuStrip1.Items.Add(mc.CloneToolStripMenuItem(文件ToolStripMenuItem1, true)); //} private ToolStripMenuItem DestToolStripMenuItem = new ToolStripMenuItem(); private bool IsAddSourceMenuClick = false; ////// 克隆某项菜单 IsDropDownItemsClone参数指定是否克隆该项菜单的子菜单 只克隆 Name ,Enabel,Text,Checked,CheckState属性和click事件 另外加了一 个click事件方法用于同步以上 属性 //////源菜单///是否克隆子菜单///public ToolStripMenuItem CloneToolStripMenuItem(ToolStripMenuItem SourceMenu, bool IsDropDownItemsClone) { if (!IsDropDownItemsClone) { ToolStripMenuItem item = new ToolStripMenuItem(); item.Name = SourceMenu.Name; item.Text = SourceMenu.Text; item.Checked = SourceMenu.Checked; item.CheckState = SourceMenu.CheckState; item.Enabled = SourceMenu.Enabled; PropertyInfo propertyInfo = (typeof(ToolStripItem)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic); EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(SourceMenu, null); FieldInfo fieldInfo = (typeof(ToolStripItem)).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic); if (fieldInfo != null) { Delegate d = eventHandlerList[fieldInfo.GetValue(null)]; if (d != null) { foreach (Delegate de in d.GetInvocationList()) { item.Click += (EventHandler)de; // MessageBox.Show(de.Method.Name); } } } if (IsAddSourceMenuClick == false) //如果没有添加自定义事件 { // SourceMenu.Click+=new EventHandler(SourceMenu_Click); // item.Click += new EventHandler(SourceMenu_Click); IsAddSourceMenuClick = true; } return item; } else { if (SourceMenu.OwnerItem == null) //如果是根菜单 // if (DestToolStripMenuItem.Name != SourceMenu.Name) DestToolStripMenuItem = this.CloneToolStripMenuItem(SourceMenu, false); for (int i = 0; i < SourceMenu.DropDownItems.Count; i++) { ToolStripMenuItem t = (ToolStripMenuItem)SourceMenu.DropDownItems[i]; if (((ToolStripMenuItem)t.OwnerItem).OwnerItem == null) //如果上一级的上一级没有父菜单 { // if (!DestToolStripMenuItem.DropDownItems.ContainsKey(t.Name)) DestToolStripMenuItem.DropDownItems.Add(this.CloneToolStripMenuItem(t, false)); } else { ToolStripMenuItem fatherItem = ((ToolStripMenuItem)(DestToolStripMenuItem.DropDownItems.Find(((ToolStripMenuItem)t.OwnerItem).Name, true)[0])); // if (!fatherItem.DropDownItems.ContainsKey(t.Name)) fatherItem.DropDownItems.Add(this.CloneToolStripMenuItem(t, false)); } if (t.HasDropDownItems) { CloneToolStripMenuItem(t, true); } } return DestToolStripMenuItem; } } ////// 为对象ContextMenuStrip,MenuStrip,ToolStripSplitButton,ToolStripDropDownButton, ToolStripMenuItem添加克隆菜单 不包含根菜单 同时会覆盖原菜单Name相同的菜单 ////////////public void CloneToolStripMenuItem(ToolStripMenuItem SourceMenu, object obj, int StartIndex) { for (int i = StartIndex; i < SourceMenu.DropDownItems.Count + StartIndex; i++) { ToolStripMenuItem t = (ToolStripMenuItem)SourceMenu.DropDownItems[i - StartIndex]; if (((ToolStripMenuItem)t.OwnerItem).OwnerItem == null) //如果上一级的上一级没有父菜单 { if (obj is ContextMenuStrip) { if ((obj as ContextMenuStrip).Items.ContainsKey(t.Name)) (obj as ContextMenuStrip).Items.RemoveByKey(t.Name); (obj as ContextMenuStrip).Items.Insert(i, this.CloneToolStripMenuItem(t, false)); } if (obj is MenuStrip) { if ((obj as MenuStrip).Items.ContainsKey(t.Name)) (obj as MenuStrip).Items.RemoveByKey(t.Name); (obj as MenuStrip).Items.Insert(i, this.CloneToolStripMenuItem(t, false)); } if (obj is ToolStripSplitButton) { if ((obj as ToolStripSplitButton).DropDownItems.ContainsKey(t.Name)) (obj as ToolStripSplitButton).DropDownItems.RemoveByKey(t.Name); (obj as ToolStripSplitButton).DropDownItems.Insert(i, this.CloneToolStripMenuItem(t, false)); } if (obj is ToolStripDropDownButton) { if ((obj as ToolStripDropDownButton).DropDownItems.ContainsKey(t.Name)) (obj as ToolStripDropDownButton).DropDownItems.RemoveByKey(t.Name); (obj as ToolStripDropDownButton).DropDownItems.Insert(i, this.CloneToolStripMenuItem(t, false)); } if (obj is ToolStripMenuItem) { if ((obj as ToolStripMenuItem).DropDownItems.ContainsKey(t.Name)) (obj as ToolStripMenuItem).DropDownItems.RemoveByKey(t.Name); (obj as ToolStripMenuItem).DropDownItems.Insert(i, this.CloneToolStripMenuItem(t, false)); } } else //添加子菜单 { if (obj is ContextMenuStrip) { ToolStripMenuItem fatherItem = ((ToolStripMenuItem)((obj as ContextMenuStrip).Items.Find(((ToolStripMenuItem)t.OwnerItem).Name, true)[0])); if (fatherItem.DropDownItems.ContainsKey(t.Name)) fatherItem.DropDownItems.RemoveByKey(t.Name); fatherItem.DropDownItems.Add(this.CloneToolStripMenuItem(t, false)); } if (obj is MenuStrip) { ToolStripMenuItem fatherItem = ((ToolStripMenuItem)((obj as MenuStrip).Items.Find(((ToolStripMenuItem)t.OwnerItem).Name, true)[0])); if (fatherItem.DropDownItems.ContainsKey(t.Name)) fatherItem.DropDownItems.RemoveByKey(t.Name); fatherItem.DropDownItems.Add(this.CloneToolStripMenuItem(t, false)); } if (obj is ToolStripSplitButton) { ToolStripMenuItem fatherItem = ((ToolStripMenuItem)((obj as ToolStripSplitButton).DropDownItems.Find(((ToolStripMenuItem)t.OwnerItem).Name, true)[0])); if (fatherItem.DropDownItems.ContainsKey(t.Name)) fatherItem.DropDownItems.RemoveByKey(t.Name); fatherItem.DropDownItems.Add(this.CloneToolStripMenuItem(t, false)); } if (obj is ToolStripDropDownButton) { ToolStripMenuItem fatherItem = ((ToolStripMenuItem)((obj as ToolStripDropDownButton).DropDownItems.Find(((ToolStripMenuItem)t.OwnerItem).Name, true)[0])); if (fatherItem.DropDownItems.ContainsKey(t.Name)) fatherItem.DropDownItems.RemoveByKey(t.Name); fatherItem.DropDownItems.Add(this.CloneToolStripMenuItem(t, false)); } if (obj is ToolStripMenuItem) { ToolStripMenuItem fatherItem = ((ToolStripMenuItem)((obj as ToolStripMenuItem).DropDownItems.Find(((ToolStripMenuItem)t.OwnerItem).Name, true)[0])); if (fatherItem.DropDownItems.ContainsKey(t.Name)) fatherItem.DropDownItems.RemoveByKey(t.Name); fatherItem.DropDownItems.Add(this.CloneToolStripMenuItem(t, false)); } } if (t.HasDropDownItems) { CloneToolStripMenuItem(t, obj, 0); } } } ////// 在最开始处添加菜单 /////////public void CloneToolStripMenuItem(ToolStripMenuItem SourceMenu, object obj) { CloneToolStripMenuItem(SourceMenu, obj, 0); } } }