public void ShowContextMenu(Point pointScreen) { IntPtr intPtr = IntPtr.Zero; IntPtr zero = IntPtr.Zero; IntPtr zero2 = IntPtr.Zero; IntPtr zero3 = IntPtr.Zero; try { if (null == this._arrPIDLs) { this.ReleaseAll(); } else if (!this.GetContextMenuInterfaces(this._oParentFolder, this._arrPIDLs, out zero)) { this.ReleaseAll(); } else { intPtr = ShellContextMenu.CreatePopupMenu(); int num = this._oContextMenu.QueryContextMenu(intPtr, 0u, 1u, 30000u, ShellContextMenu.CMF.EXPLORE | (((Control.ModifierKeys & Keys.Shift) != Keys.None) ? ShellContextMenu.CMF.EXTENDEDVERBS : ShellContextMenu.CMF.NORMAL)); Marshal.QueryInterface(zero, ref ShellContextMenu.IID_IContextMenu2, out zero2); Marshal.QueryInterface(zero, ref ShellContextMenu.IID_IContextMenu3, out zero3); this._oContextMenu2 = (ShellContextMenu.IContextMenu2)Marshal.GetTypedObjectForIUnknown(zero2, typeof(ShellContextMenu.IContextMenu2)); this._oContextMenu3 = (ShellContextMenu.IContextMenu3)Marshal.GetTypedObjectForIUnknown(zero3, typeof(ShellContextMenu.IContextMenu3)); uint num2 = ShellContextMenu.TrackPopupMenuEx(intPtr, ShellContextMenu.TPM.RETURNCMD, pointScreen.X, pointScreen.Y, base.Handle, IntPtr.Zero); ShellContextMenu.DestroyMenu(intPtr); intPtr = IntPtr.Zero; if (num2 != 0u) { this.InvokeCommand(this._oContextMenu, num2, this._strParentFolder, pointScreen); } } } catch { throw; } finally { if (intPtr != IntPtr.Zero) { ShellContextMenu.DestroyMenu(intPtr); } if (zero != IntPtr.Zero) { Marshal.Release(zero); } if (zero2 != IntPtr.Zero) { Marshal.Release(zero2); } if (zero3 != IntPtr.Zero) { Marshal.Release(zero3); } this.ReleaseAll(); } }
private ShellContextMenu.IShellFolder GetDesktopFolder() { IntPtr zero = IntPtr.Zero; if (null == this._oDesktopFolder) { int num = ShellContextMenu.SHGetDesktopFolder(out zero); if (0 != num) { throw new ShellContextMenuException("Failed to get the desktop shell folder"); } this._oDesktopFolder = (ShellContextMenu.IShellFolder)Marshal.GetTypedObjectForIUnknown(zero, typeof(ShellContextMenu.IShellFolder)); } return(this._oDesktopFolder); }
private void InvokeContextMenuDefault(FileInfo[] arrFI) { this.ReleaseAll(); IntPtr intPtr = IntPtr.Zero; IntPtr zero = IntPtr.Zero; try { this._arrPIDLs = this.GetPIDLs(arrFI); if (null == this._arrPIDLs) { this.ReleaseAll(); } else if (!this.GetContextMenuInterfaces(this._oParentFolder, this._arrPIDLs, out zero)) { this.ReleaseAll(); } else { intPtr = ShellContextMenu.CreatePopupMenu(); int num = this._oContextMenu.QueryContextMenu(intPtr, 0u, 1u, 30000u, ShellContextMenu.CMF.DEFAULTONLY | (((Control.ModifierKeys & Keys.Shift) != Keys.None) ? ShellContextMenu.CMF.EXTENDEDVERBS : ShellContextMenu.CMF.NORMAL)); uint menuDefaultItem = (uint)ShellContextMenu.GetMenuDefaultItem(intPtr, false, 0u); if (menuDefaultItem >= 1u) { this.InvokeCommand(this._oContextMenu, menuDefaultItem, arrFI[0].DirectoryName, Control.MousePosition); } ShellContextMenu.DestroyMenu(intPtr); intPtr = IntPtr.Zero; } } catch { throw; } finally { if (intPtr != IntPtr.Zero) { ShellContextMenu.DestroyMenu(intPtr); } this.ReleaseAll(); } }
private ShellContextMenu.IShellFolder GetParentFolder(string folderName) { ShellContextMenu.IShellFolder result; if (null == this._oParentFolder) { ShellContextMenu.IShellFolder desktopFolder = this.GetDesktopFolder(); if (null == desktopFolder) { result = null; return(result); } IntPtr zero = IntPtr.Zero; uint num = 0u; ShellContextMenu.SFGAO sFGAO = (ShellContextMenu.SFGAO) 0u; int num2 = desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, folderName, ref num, out zero, ref sFGAO); if (0 != num2) { result = null; return(result); } IntPtr intPtr = Marshal.AllocCoTaskMem(524); Marshal.WriteInt32(intPtr, 0, 0); num2 = this._oDesktopFolder.GetDisplayNameOf(zero, ShellContextMenu.SHGNO.FORPARSING, intPtr); StringBuilder stringBuilder = new StringBuilder(260); ShellContextMenu.StrRetToBuf(intPtr, zero, stringBuilder, 260); Marshal.FreeCoTaskMem(intPtr); intPtr = IntPtr.Zero; this._strParentFolder = stringBuilder.ToString(); IntPtr zero2 = IntPtr.Zero; num2 = desktopFolder.BindToObject(zero, IntPtr.Zero, ref ShellContextMenu.IID_IShellFolder, out zero2); Marshal.FreeCoTaskMem(zero); if (0 != num2) { result = null; return(result); } this._oParentFolder = (ShellContextMenu.IShellFolder)Marshal.GetTypedObjectForIUnknown(zero2, typeof(ShellContextMenu.IShellFolder)); } result = this._oParentFolder; return(result); }
public int ShowContextMenu(Point pointScreen, string[] AddedItems) { IntPtr intPtr = IntPtr.Zero; IntPtr zero = IntPtr.Zero; IntPtr zero2 = IntPtr.Zero; IntPtr zero3 = IntPtr.Zero; int num = -1; int result; try { if (null == this._arrPIDLs) { this.ReleaseAll(); result = -2; } else if (!this.GetContextMenuInterfaces(this._oParentFolder, this._arrPIDLs, out zero)) { this.ReleaseAll(); result = -2; } else { intPtr = ShellContextMenu.CreatePopupMenu(); int num2 = this._oContextMenu.QueryContextMenu(intPtr, 0u, 1u, 30000u, ShellContextMenu.CMF.EXPLORE | (((Control.ModifierKeys & Keys.Shift) != Keys.None) ? ShellContextMenu.CMF.EXTENDEDVERBS : ShellContextMenu.CMF.NORMAL)); Marshal.QueryInterface(zero, ref ShellContextMenu.IID_IContextMenu2, out zero2); Marshal.QueryInterface(zero, ref ShellContextMenu.IID_IContextMenu3, out zero3); this._oContextMenu2 = (ShellContextMenu.IContextMenu2)Marshal.GetTypedObjectForIUnknown(zero2, typeof(ShellContextMenu.IContextMenu2)); this._oContextMenu3 = (ShellContextMenu.IContextMenu3)Marshal.GetTypedObjectForIUnknown(zero3, typeof(ShellContextMenu.IContextMenu3)); for (int i = 0; i < AddedItems.Length; i++) { ShellAPI.InsertMenu(intPtr, (uint)i, ShellAPI.MFT.BYPOSITION, (uint)(30001 + i), AddedItems[i]); } ShellAPI.InsertMenu(intPtr, (uint)AddedItems.Length, ShellAPI.MFT.SEPARATOR | ShellAPI.MFT.BYPOSITION, (uint)(30001 + AddedItems.Length), "-"); uint num3 = ShellContextMenu.TrackPopupMenuEx(intPtr, ShellContextMenu.TPM.RETURNCMD, pointScreen.X, pointScreen.Y, base.Handle, IntPtr.Zero); ShellContextMenu.DestroyMenu(intPtr); intPtr = IntPtr.Zero; if (AddedItems.Length > 0 && num3 >= 30001u && (ulong)num3 <= (ulong)((long)(AddedItems.Length - 1 + 30001))) { num = (int)(num3 - 30001u); } else if (num3 != 0u) { this.InvokeCommand(this._oContextMenu, num3, this._strParentFolder, pointScreen); } result = num; } } catch { Console.WriteLine("Error in Context Menu generation"); result = -1; } finally { if (intPtr != IntPtr.Zero) { ShellContextMenu.DestroyMenu(intPtr); } if (zero != IntPtr.Zero) { Marshal.Release(zero); } if (zero2 != IntPtr.Zero) { Marshal.Release(zero2); } if (zero3 != IntPtr.Zero) { Marshal.Release(zero3); } this.ReleaseAll(); } return(result); }