private void PerformClick() { SWF.ToolStripItem item = (SWF.ToolStripItem)Provider.Component; if (item.Owner != null && item.Owner.InvokeRequired) { item.Owner.BeginInvoke(new SWF.MethodInvoker(PerformClick)); return; } var currentParent = item.OwnerItem as SWF.ToolStripMenuItem; // Invoking without a visible parent results in exceptions if (currentParent != null && !currentParent.DropDown.Visible) { return; } var dropdown = item as SWF.ToolStripDropDownItem; bool hide = false; if (dropdown != null) { hide = dropdown.Pressed; } // Make sure selection changes, or else another item's // dropdown menu might still appear. if (item.Owner != null) { item.Select(); } item.PerformClick(); // PerformClick does _not_ show/hide the DropDown, so // we must do this manually. On Vista, clicking the // button appears to both Show the drop down and // Perform a click, so we emulate that behavior. if (dropdown != null && !(item is SWF.ToolStripSplitButton)) { if (hide) { dropdown.HideDropDown(); } else { dropdown.ShowDropDown(); } } }
/// <summary> /// 当鼠标点击,激活该菜单项 /// </summary> public bool PerformClick(string name) { try { System.Windows.Forms.ToolStripItem menuItem = MenuTableItem(name); if (menuItem == null) { return(false); } menuItem.PerformClick(); return(true); } catch (Exception ex) { MapWinGIS.Utility.Logger.Dbg("在clsMenu.PerformClick(" + name + ")中发生一个错误:" + ex.ToString()); } return(false); }
/// <summary> /// Activates correct menu item by recursively going through the whole menu structure. Called from OnClick and recursively calls itself. /// </summary> /// <param name="item"></param> /// <param name="next"></param> /// <param name="forward"></param> /// <returns></returns> private bool OnClick_RecursivelyActivate(ToolStripItem item, ref bool next, bool forward) { if (!(item is ToolStripMenuItem)) return false; if (((ToolStripMenuItem)item).DropDownItems.Count > 0) { for (int i = 0; i < ((ToolStripMenuItem)item).DropDownItems.Count; i++) if (OnClick_RecursivelyActivate(((ToolStripMenuItem)item).DropDownItems[forward ? i : ((ToolStripMenuItem)item).DropDownItems.Count - 1 - i], ref next, forward)) return true; } else { if (next) { item.PerformClick(); return true; } next = item.Selected || next; } return false; }
private bool PerformMenuClick(ToolStripItem menuItem, Keys shortCut) { if (menuItem is ToolStripMenuItem) { if ((menuItem as ToolStripMenuItem).ShortcutKeys == shortCut) { menuItem.PerformClick(); return true; } } if (menuItem is ToolStripDropDownItem) { foreach (ToolStripItem item in (menuItem as ToolStripDropDownItem).DropDownItems) { if (PerformMenuClick(item, shortCut)) { return true; } } } return false; }