private void ToolStrip_ItemClick(object sender, ToolStripItemClickedEventArgs e) { ToolStripDropDownItem clickedItem = e.ClickedItem as ToolStripDropDownItem; if (((clickedItem != null) && (clickedItem.DropDownItems.Count <= 0)) && (clickedItem.Tag is MenuItemInfo)) { MenuItemInfo tag = (MenuItemInfo) clickedItem.Tag; bool handled = false; if (this.OnExecuteVerb != null) { IntPtr pszName = Marshal.AllocHGlobal(0x100); try { this.ContextMenu.GetCommandString(tag.Info.wID - 1, GCS.VERBA, 0, pszName, 0x100); ExecuteVerbEventArgs args = new ExecuteVerbEventArgs(Marshal.PtrToStringAnsi(pszName)); this.OnExecuteVerb(this, args); handled = args.Handled; } catch (SystemException) { } finally { Marshal.FreeHGlobal(pszName); } } if (!handled) { ContextMenuStrip owner = sender as ContextMenuStrip; if (owner == null) { for (ToolStripItem item2 = sender as ToolStripItem; (owner == null) && (item2 != null); item2 = item2.OwnerItem) { owner = item2.Owner as ContextMenuStrip; } } Debug.Assert(owner != null); owner.Closed -= new ToolStripDropDownClosedEventHandler(this.ContextMenuStrip_Closed); owner.Hide(); try { CMINVOKECOMMANDINFOEX cminvokecommandinfoex; cminvokecommandinfoex = new CMINVOKECOMMANDINFOEX { cbSize = Marshal.SizeOf(cminvokecommandinfoex), lpVerb = (IntPtr) (tag.Info.wID - 1), lpVerbW = (IntPtr) (tag.Info.wID - 1) }; if (!string.IsNullOrEmpty(this.ParentName)) { cminvokecommandinfoex.lpDirectory = this.ParentName; cminvokecommandinfoex.lpDirectoryW = this.ParentName; } cminvokecommandinfoex.fMask = ((CMIC.ASYNCOK | CMIC.PTINVOKE | CMIC.UNICODE) | (((Control.ModifierKeys & Keys.Control) > Keys.None) ? CMIC.CONTROL_DOWN : ((CMIC) 0))) | (((Control.ModifierKeys & Keys.Shift) > Keys.None) ? CMIC.SHIFT_DOWN : ((CMIC) 0)); cminvokecommandinfoex.ptInvoke = Cursor.Position; cminvokecommandinfoex.nShow = SW.SW_SHOWNORMAL; cminvokecommandinfoex.hwnd = this.Owner.Handle; this.ContextMenu.InvokeCommand(ref cminvokecommandinfoex); } finally { this.ItemContainer.Dispose(); } } } }
public static bool ExecuteVerb(IWin32Window owner, string verb, string parentName, IContextMenu contextMenu) { if (contextMenu == null) { return false; } CMINVOKECOMMANDINFOEX structure = new CMINVOKECOMMANDINFOEX(); try { structure.cbSize = Marshal.SizeOf(structure); if (verb != null) { structure.lpVerb = Marshal.StringToHGlobalAnsi(verb); structure.lpVerbW = Marshal.StringToHGlobalUni(verb); } if (!string.IsNullOrEmpty(parentName)) { structure.lpDirectory = parentName; structure.lpDirectoryW = parentName; } if (owner != null) { structure.hwnd = owner.Handle; } structure.fMask = (CMIC.UNICODE | (((Control.ModifierKeys & Keys.Control) > Keys.None) ? CMIC.CONTROL_DOWN : ((CMIC) 0))) | (((Control.ModifierKeys & Keys.Shift) > Keys.None) ? CMIC.SHIFT_DOWN : ((CMIC) 0)); structure.nShow = SW.SW_SHOWNORMAL; contextMenu.InvokeCommand(ref structure); Marshal.ReleaseComObject(contextMenu); } finally { Marshal.FreeHGlobal(structure.lpVerb); Marshal.FreeHGlobal(structure.lpVerbW); } return true; }