pub struct ShellItems { /* private fields */ }Expand description
Resolved shell items ready to create a context menu from.
Holds the parent IShellFolder and one or more child PIDLs. For
background menus (right-clicking empty space inside a folder), child_pidls
is empty and the parent itself is the folder.
Create instances via ShellItems::from_path, ShellItems::from_paths,
or ShellItems::folder_background.
Implementations§
Source§impl ShellItems
impl ShellItems
Sourcepub fn from_path(path: impl AsRef<Path>) -> Result<Self>
pub fn from_path(path: impl AsRef<Path>) -> Result<Self>
Resolve a single file or folder path to shell items.
§Errors
Returns Error::ParsePath if the path does not exist or cannot be
resolved by the shell.
Examples found in repository?
3fn main() -> win_context_menu::Result<()> {
4 let _com = init_com()?;
5
6 let path = std::env::args()
7 .nth(1)
8 .unwrap_or_else(|| r"C:\Windows\notepad.exe".to_string());
9
10 println!("Enumerating context menu items for: {}\n", path);
11
12 let items = ShellItems::from_path(&path)?;
13 let menu = ContextMenu::new(items)?;
14 let entries = menu.enumerate()?;
15
16 print_items(&entries, 0);
17
18 Ok(())
19}More examples
3fn main() -> win_context_menu::Result<()> {
4 let _com = init_com()?;
5
6 let path = std::env::args()
7 .nth(1)
8 .unwrap_or_else(|| r"C:\Windows\notepad.exe".to_string());
9
10 println!("Showing context menu for: {}", path);
11
12 let items = ShellItems::from_path(&path)?;
13 let menu = ContextMenu::new(items)?;
14
15 match menu.show()? {
16 Some(selected) => {
17 println!(
18 "Selected: {} (verb: {:?})",
19 selected.menu_item().label,
20 selected.menu_item().command_string
21 );
22 selected.execute()?;
23 }
24 None => println!("No item selected."),
25 }
26
27 Ok(())
28}3fn main() -> win_context_menu::Result<()> {
4 let _com = init_com()?;
5
6 let path = std::env::args()
7 .nth(1)
8 .unwrap_or_else(|| r"C:\Windows\notepad.exe".to_string());
9
10 println!("Showing extended context menu (Shift+right-click) for: {}", path);
11
12 let items = ShellItems::from_path(&path)?;
13 let menu = ContextMenu::new(items)?.extended(true);
14
15 match menu.show()? {
16 Some(selected) => {
17 println!(
18 "Selected: {} (verb: {:?})",
19 selected.menu_item().label,
20 selected.menu_item().command_string
21 );
22 selected.execute()?;
23 }
24 None => println!("No item selected."),
25 }
26
27 Ok(())
28}Sourcepub fn from_paths(paths: &[impl AsRef<Path>]) -> Result<Self>
pub fn from_paths(paths: &[impl AsRef<Path>]) -> Result<Self>
Resolve multiple paths. All paths must share the same parent folder.
This is the multi-select equivalent — the resulting context menu will act on all specified items at once (like selecting several files in Explorer, then right-clicking).
§Errors
Error::NoCommonParentifpathsis empty or the paths do not share a common parent folder.Error::ParsePathif any path cannot be resolved.
Examples found in repository?
3fn main() -> win_context_menu::Result<()> {
4 let _com = init_com()?;
5
6 let args: Vec<String> = std::env::args().skip(1).collect();
7 let paths: Vec<String> = if args.is_empty() {
8 vec![
9 r"C:\Windows\notepad.exe".to_string(),
10 r"C:\Windows\regedit.exe".to_string(),
11 ]
12 } else {
13 args
14 };
15
16 println!("Showing context menu for {} items:", paths.len());
17 for p in &paths {
18 println!(" - {}", p);
19 }
20
21 let items = ShellItems::from_paths(&paths)?;
22 let menu = ContextMenu::new(items)?;
23
24 match menu.show()? {
25 Some(selected) => {
26 println!(
27 "Selected: {} (verb: {:?})",
28 selected.menu_item().label,
29 selected.menu_item().command_string
30 );
31 selected.execute()?;
32 }
33 None => println!("No item selected."),
34 }
35
36 Ok(())
37}Sourcepub fn folder_background(folder: impl AsRef<Path>) -> Result<Self>
pub fn folder_background(folder: impl AsRef<Path>) -> Result<Self>
Create shell items for the background of a folder.
This is equivalent to right-clicking the empty space inside a folder in Explorer (rather than right-clicking a specific file). The resulting context menu typically offers “New”, “Paste”, “View”, etc.
§Errors
Returns Error::ParsePath if the folder path cannot be resolved.
Examples found in repository?
3fn main() -> win_context_menu::Result<()> {
4 let _com = init_com()?;
5
6 let folder = std::env::args()
7 .nth(1)
8 .unwrap_or_else(|| r"C:\Windows".to_string());
9
10 println!("Showing background context menu for folder: {}", folder);
11
12 let items = ShellItems::folder_background(&folder)?;
13 let menu = ContextMenu::new(items)?;
14
15 match menu.show()? {
16 Some(selected) => {
17 println!(
18 "Selected: {} (verb: {:?})",
19 selected.menu_item().label,
20 selected.menu_item().command_string
21 );
22 selected.execute()?;
23 }
24 None => println!("No item selected."),
25 }
26
27 Ok(())
28}