public static ContextMenu GetContextMenuForFiles(string[] filePathList, Shell32.CMF flags, Func <string, bool> itemFilter = null) { List <ShellItem> shellItems = new List <ShellItem>(); try { foreach (var fp in filePathList.Where(x => !string.IsNullOrEmpty(x))) { shellItems.Add(new ShellItem(fp)); } return(GetContextMenuForFiles(shellItems.ToArray(), flags, itemFilter)); } catch (ArgumentException) { // Return empty context menu return(null); } finally { foreach (var si in shellItems) { si.Dispose(); } } }
public static ContextMenu GetContextMenuForFiles(string[] filePathList, Shell32.CMF flags, Func <string, bool> itemFilter = null) { List <ShellItem> shellItems = new List <ShellItem>(); try { foreach (var fp in filePathList.Where(x => !string.IsNullOrEmpty(x))) { shellItems.Add(ShellFolderExtensions.GetShellItemFromPathOrPidl(fp)); } return(GetContextMenuForFiles(shellItems.ToArray(), flags, itemFilter)); } catch (Exception ex) when(ex is ArgumentException || ex is FileNotFoundException) { // Return empty context menu return(null); } finally { foreach (var si in shellItems) { si.Dispose(); } } }
public static ContextMenu GetContextMenuForFiles(ShellItem[] shellItems, Shell32.CMF flags, Func <string, bool> itemFilter = null) { if (shellItems == null || !shellItems.Any()) { return(null); } using var sf = shellItems.First().Parent; // HP: the items are all in the same folder Shell32.IContextMenu menu = sf.GetChildrenUIObjects <Shell32.IContextMenu>(null, shellItems); var hMenu = User32.CreatePopupMenu(); menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags); var contextMenu = new ContextMenu(menu, hMenu); ContextMenu.EnumMenuItems(menu, hMenu, contextMenu.Items, itemFilter); return(contextMenu); }
private static ContextMenu GetContextMenuForFolder(ShellFolder shellFolder, Shell32.CMF flags, Func <string, bool> itemFilter = null) { if (shellFolder == null) { return(null); } var sv = shellFolder.GetViewObject <Shell32.IShellView>(null); Shell32.IContextMenu menu = sv.GetItemObject <Shell32.IContextMenu>(Shell32.SVGIO.SVGIO_BACKGROUND); var hMenu = User32.CreatePopupMenu(); menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags); var contextMenu = new ContextMenu(menu, hMenu, new[] { shellFolder.ParsingName }); ContextMenu.EnumMenuItems(menu, hMenu, contextMenu.Items, itemFilter); return(contextMenu); }
public static ContextMenu GetContextMenuForFolder(string folderPath, Shell32.CMF flags, Func <string, bool> itemFilter = null) { ShellFolder fsi = null; try { fsi = new ShellFolder(folderPath); return(GetContextMenuForFolder(fsi, flags, itemFilter)); } catch (Exception ex) when(ex is ArgumentException || ex is FileNotFoundException) { // Return empty context menu return(null); } finally { fsi?.Dispose(); } }