public static void ConstructCustomMenu(IntPtr menu, CustomMenuStructure[] customItems, uint idCmdFirst, out List <IntPtr> menuPtrConstructed, out List <IntPtr> imgPtrConstructed) { menuPtrConstructed = new List <IntPtr>(); imgPtrConstructed = new List <IntPtr>(); for (int i = 0; i < customItems.Length; i++) { CustomMenuStructure item = customItems[i]; if (item.IsFolder) { IntPtr newPopup = ShellAPI.CreatePopupMenu(); List <IntPtr> submenuPtrConstructed, subimgPtrConstructed; ConstructCustomMenu(newPopup, item.Items.ToArray(), idCmdFirst, out submenuPtrConstructed, out subimgPtrConstructed); insertMenu(menu, idCmdFirst, i, newPopup, item, ref imgPtrConstructed); menuPtrConstructed.Add(newPopup); menuPtrConstructed.AddRange(submenuPtrConstructed); imgPtrConstructed.AddRange(subimgPtrConstructed); } else if (item.Text == "---") { ShellAPI.InsertMenu(menu, (uint)i, ShellAPI.MFT.BYPOSITION | ShellAPI.MFT.SEPARATOR, 0, ""); } else { insertMenu(menu, idCmdFirst, i, IntPtr.Zero, item, ref imgPtrConstructed); } } }
internal static void ConstructCustomMenu(IntPtr menu, CustomMenuStructure[] customItems, out List <IntPtr> menuPtrConstructed) { menuPtrConstructed = new List <IntPtr>(); for (int i = 0; i < customItems.Length; i++) { CustomMenuStructure item = customItems[i]; if (item.IsFolder) { IntPtr newPopup = ShellAPI.CreatePopupMenu(); menuPtrConstructed.Add(newPopup); ShellAPI.InsertMenu(menu, (uint)i, ShellAPI.MFT.BYPOSITION | ShellAPI.MFT.POPUP, newPopup, item.Text); List <IntPtr> submenuPtrConstructed; ConstructCustomMenu(newPopup, item.Items.ToArray(), out submenuPtrConstructed); menuPtrConstructed.AddRange(submenuPtrConstructed); } else if (item.Text == "---") { ShellAPI.InsertMenu(menu, (uint)i, ShellAPI.MFT.BYPOSITION | ShellAPI.MFT.SEPARATOR, IntPtr.Zero, ""); } else { ShellAPI.MFT extraFlag = 0; if (item.Checked) { extraFlag |= ShellAPI.MFT.CHECKED; } ShellAPI.InsertMenu(menu, (uint)i, ShellAPI.MFT.BYPOSITION | extraFlag, new UIntPtr(ShellAPI.CMD_LAST + item.ID + 1), item.Text); } } }
public string Popup(FileSystemInfoEx[] items, Point pt) { if (items.Length > 0 && !_contextMenuVisible) { //0.15: Fixed ShellFolder not freed correctly. try { using (ShellFolder2 parentShellFolder = items[0].Parent != null ? items[0].Parent.ShellFolder : DirectoryInfoEx.DesktopDirectory.ShellFolder) try { _contextMenuVisible = true; ///Debug.WriteLine(items[0].Parent.FullName); IntPtr ptrContextMenu = IntPtr.Zero; IntPtr ptrContextMenu2 = IntPtr.Zero; IntPtr PtrContextMenu3 = IntPtr.Zero; IntPtr contextMenu = IntPtr.Zero; List <IntPtr> menuPtrConstructed = new List <IntPtr>(); List <IntPtr> imgPtrConstructed = new List <IntPtr>(); PIDL[] pidls = IOTools.GetPIDL(items, true); if (ContextMenuHelper.GetIContextMenu(parentShellFolder, IOTools.GetPIDLPtr(pidls), out ptrContextMenu, out _iContextMenu)) { try { queryMenuItemsEventArgs = new QueryMenuItemsEventArgs(items); if (OnQueryMenuItems != null) { OnQueryMenuItems(this, queryMenuItemsEventArgs); } contextMenu = ShellAPI.CreatePopupMenu(); if (queryMenuItemsEventArgs.QueryContextMenu) { _iContextMenu.QueryContextMenu(contextMenu, 0, ShellAPI.CMD_FIRST, ShellAPI.CMD_LAST, ShellAPI.CMF.EXPLORE | ShellAPI.CMF.CANRENAME | ((Control.ModifierKeys & Keys.Shift) != 0 ? ShellAPI.CMF.EXTENDEDVERBS : 0)); } #region obsolute //for (uint i = 0; i < queryMenuItemsEventArgs.ExtraMenuItems.Length; i++) //{ // string caption = queryMenuItemsEventArgs.ExtraMenuItems[i]; // if (caption != "---") // { // ShellAPI.InsertMenu(contextMenu, i, ShellAPI.MFT.BYPOSITION, // ShellAPI.CMD_LAST + i + 1, caption); // if (queryMenuItemsEventArgs.DefaultItem == i) // ShellAPI.SetMenuDefaultItem(contextMenu, i, true); // } // else ShellAPI.InsertMenu(contextMenu, i, ShellAPI.MFT.BYPOSITION | // ShellAPI.MFT.SEPARATOR, 0, "-"); //} #endregion //0.11: Added ContextMenuWrapper OnQueryMenuItems event now support multilevel directory. (e.g. @"Tools\Add") ContextMenuHelperEx.ConstructCustomMenu(contextMenu, queryMenuItemsEventArgs.ExtraMenuItems, out menuPtrConstructed, out imgPtrConstructed); if (queryMenuItemsEventArgs.QueryContextMenu2) { try { Marshal.QueryInterface(ptrContextMenu, ref ShellAPI.IID_IContextMenu2, out ptrContextMenu2); _iContextMenu2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown( ptrContextMenu2, typeof(IContextMenu2)); } catch (Exception) { } } if (queryMenuItemsEventArgs.QueryContextMenu3) { try { Marshal.QueryInterface(ptrContextMenu, ref ShellAPI.IID_IContextMenu3, out PtrContextMenu3); _iContextMenu3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown( PtrContextMenu3, typeof(IContextMenu3)); } catch (Exception) { } } uint GMDI_USEDISABLED = 0x0001; //uint GMDI_GOINTOPOPUPS = 0x0002; uint intDefaultItem = (uint)ShellAPI.GetMenuDefaultItem(contextMenu, false, GMDI_USEDISABLED); string strDefaultCommand = intDefaultItem >= ShellAPI.CMD_FIRST ? ContextMenuHelper.GetCommandString(_iContextMenu, intDefaultItem - ShellAPI.CMD_FIRST, true) : null; if (queryMenuItemsEventArgs.QueryContextMenu) //No need to Disable if query is not carried out in first place. { //0.11: Added queryMenuItemsEventArgs.GrayedItems / HiddenItems ContextMenuHelperEx.DisableMenuItems(contextMenu, _iContextMenu, queryMenuItemsEventArgs.GrayedItems, ContextMenuHelperEx.DisabledMethods.Gray); ContextMenuHelperEx.DisableMenuItems(contextMenu, _iContextMenu, queryMenuItemsEventArgs.HiddenItems, ContextMenuHelperEx.DisabledMethods.Remove); } //0.17: Added DefaultItem and DefaultCommand in BeforePopup bool cont = true; if (OnBeforePopup != null) { BeforePopupEventArgs args = new BeforePopupEventArgs (contextMenu, _iContextMenu, intDefaultItem - ShellAPI.CMD_FIRST, strDefaultCommand); OnBeforePopup(this, args); cont = args.ContinuePopup; } if (cont) { //0.18 Fixed Context menu disappear in some case. (By cwharmon) //http://www.codeproject.com/KB/files/DirectoryInfoEx.aspx#xx3475961xx ShellAPI.SetForegroundWindow(this.Handle); uint selected = ShellAPI.TrackPopupMenuEx(contextMenu, ShellAPI.TPM.RETURNCMD, pt.X, pt.Y, this.Handle, IntPtr.Zero); uint msg = 0; ShellAPI.PostMessage(this.Handle, msg, IntPtr.Zero, IntPtr.Zero); if (OnMouseHover != null) { OnMouseHover(this, new MouseHoverEventArgs("", "", 0)); } if (selected >= ShellAPI.CMD_LAST) { return(removeCheckedSymbol(queryMenuItemsEventArgs.ExtraMenuItems[selected - ShellAPI.CMD_LAST])); } if (selected >= ShellAPI.CMD_FIRST) { string command = ContextMenuHelper.GetCommandString(_iContextMenu, selected - ShellAPI.CMD_FIRST, true); if (command == null) { return(null); } if (OnBeforeInvokeCommand != null) { InvokeCommandEventArgs args = new InvokeCommandEventArgs(command, "", selected, items); OnBeforeInvokeCommand(this, args); if (!args.ContinueInvoke) { return(command); } } if (command == "rename") { return("rename"); } else { //if (items.Length == 1 && items[0] is DirectoryInfoEx) ContextMenuHelper.InvokeCommand(_iContextMenu, selected - ShellAPI.CMD_FIRST, (items[0].Parent != null) ? items[0].Parent.FullName : items[0].FullName, pt); //else //ContextMenuHelper.InvokeCommand(items[0].Parent, // IOTools.GetPIDLPtr(items, true), selected - ShellAPI.CMD_FIRST, // pt); } } } } finally { IOTools.FreePIDL(pidls); if (_iContextMenu != null) { Marshal.ReleaseComObject(_iContextMenu); _iContextMenu = null; } if (_iContextMenu2 != null) { Marshal.ReleaseComObject(_iContextMenu2); _iContextMenu2 = null; } if (_iContextMenu3 != null) { Marshal.ReleaseComObject(_iContextMenu3); _iContextMenu3 = null; } foreach (IntPtr menuPtr in menuPtrConstructed) { ShellAPI.DestroyMenu(menuPtr); } menuPtrConstructed.Clear(); if (contextMenu != null) { ShellAPI.DestroyMenu(contextMenu); } if (ptrContextMenu != IntPtr.Zero) { Marshal.Release(ptrContextMenu); } if (ptrContextMenu2 != IntPtr.Zero) { Marshal.Release(ptrContextMenu2); } if (PtrContextMenu3 != IntPtr.Zero) { Marshal.Release(PtrContextMenu3); } } } } finally { _contextMenuVisible = false; } } catch { } } return(null); }
/// <summary> /// When the mouse goes up on an node and suspendContextMenu is true, this method will show the /// ContextMenu for that node and after the user selects an item, it will execute that command. /// </summary> void FolderView_MouseUp(object sender, MouseEventArgs e) { if (suspendContextMenu || contextMenuVisible) { suspendContextMenu = false; return; } TreeViewHitTestInfo hitTest = br.FolderView.HitTest(e.Location); contextMenuVisible = true; if (e.Button == MouseButtons.Right && (hitTest.Location == TreeViewHitTestLocations.Image || hitTest.Location == TreeViewHitTestLocations.Label || hitTest.Location == TreeViewHitTestLocations.StateImage)) { #region Fields ShellItem item = (ShellItem)hitTest.Node.Tag; IntPtr contextMenu = IntPtr.Zero, iContextMenuPtr = IntPtr.Zero, iContextMenuPtr2 = IntPtr.Zero, iContextMenuPtr3 = IntPtr.Zero; IShellFolder parentShellFolder = (item.ParentItem != null) ? item.ParentItem.ShellFolder : item.ShellFolder; #endregion #region Show / Invoke try { if (ContextMenuHelper.GetIContextMenu(parentShellFolder, new IntPtr[] { item.PIDLRel.Ptr }, out iContextMenuPtr, out iContextMenu)) { contextMenu = ShellAPI.CreatePopupMenu(); iContextMenu.QueryContextMenu( contextMenu, 0, ShellAPI.CMD_FIRST, ShellAPI.CMD_LAST, ShellAPI.CMF.EXPLORE | ShellAPI.CMF.CANRENAME | ((Control.ModifierKeys & Keys.Shift) != 0 ? ShellAPI.CMF.EXTENDEDVERBS : 0)); string topInvoke = hitTest.Node.IsExpanded ? "Collapse" : "Expand"; ShellAPI.MFT extraFlag = (hitTest.Node.Nodes.Count > 0) ? 0 : ShellAPI.MFT.GRAYED; ShellAPI.InsertMenu(contextMenu, 0, ShellAPI.MFT.BYPOSITION | extraFlag, (int)CMD_CUSTOM.ExpandCollapse, topInvoke); ShellAPI.InsertMenu(contextMenu, 1, ShellAPI.MFT.BYPOSITION | ShellAPI.MFT.SEPARATOR, 0, "-"); ShellAPI.SetMenuDefaultItem( contextMenu, 0, true); Marshal.QueryInterface(iContextMenuPtr, ref ShellAPI.IID_IContextMenu2, out iContextMenuPtr2); Marshal.QueryInterface(iContextMenuPtr, ref ShellAPI.IID_IContextMenu3, out iContextMenuPtr3); try { iContextMenu2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(iContextMenuPtr2, typeof(IContextMenu2)); iContextMenu3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(iContextMenuPtr3, typeof(IContextMenu3)); } catch (Exception) { } Point ptInvoke = br.FolderView.PointToScreen(e.Location); uint selected = ShellAPI.TrackPopupMenuEx( contextMenu, ShellAPI.TPM.RETURNCMD, ptInvoke.X, ptInvoke.Y, this.Handle, IntPtr.Zero); br.OnContextMenuMouseHover(new ContextMenuMouseHoverEventArgs(string.Empty)); if (selected == (int)CMD_CUSTOM.ExpandCollapse) { if (hitTest.Node.IsExpanded) { hitTest.Node.Collapse(true); } else { hitTest.Node.Expand(); } } else if (selected >= ShellAPI.CMD_FIRST) { string command = ContextMenuHelper.GetCommandString( iContextMenu, selected - ShellAPI.CMD_FIRST, true); if (command == "rename") { br.FolderView.LabelEdit = true; hitTest.Node.BeginEdit(); } else { ContextMenuHelper.InvokeCommand( iContextMenu, selected - ShellAPI.CMD_FIRST, (item.ParentItem != null) ? ShellItem.GetRealPath(item.ParentItem) : ShellItem.GetRealPath(item), ptInvoke); } } } } #endregion catch (Exception) { } #region Finally finally { if (iContextMenu != null) { Marshal.ReleaseComObject(iContextMenu); iContextMenu = null; } if (iContextMenu2 != null) { Marshal.ReleaseComObject(iContextMenu2); iContextMenu2 = null; } if (iContextMenu3 != null) { Marshal.ReleaseComObject(iContextMenu3); iContextMenu3 = null; } if (contextMenu != null) { ShellAPI.DestroyMenu(contextMenu); } if (iContextMenuPtr != IntPtr.Zero) { Marshal.Release(iContextMenuPtr); } if (iContextMenuPtr2 != IntPtr.Zero) { Marshal.Release(iContextMenuPtr2); } if (iContextMenuPtr3 != IntPtr.Zero) { Marshal.Release(iContextMenuPtr3); } } #endregion } contextMenuVisible = false; }