pub struct ContextMenu { /* private fields */ }Expand description
Builder for displaying a Windows Explorer context menu.
Create one via ContextMenu::new, optionally configure it with
extended or owner, then
call show / show_at to
display the menu, or enumerate to list items
without showing anything.
§Example
use win_context_menu::{init_com, ContextMenu, ShellItems};
let _com = init_com()?;
let items = ShellItems::from_path(r"C:\Windows\notepad.exe")?;
let selected = ContextMenu::new(items)?.extended(true).show()?;
if let Some(sel) = selected {
sel.execute()?;
}Implementations§
Source§impl ContextMenu
impl ContextMenu
Sourcepub fn new(items: ShellItems) -> Result<Self>
pub fn new(items: ShellItems) -> Result<Self>
Create a new context menu builder for the given shell items.
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 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}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}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 extended(self, yes: bool) -> Self
pub fn extended(self, yes: bool) -> Self
Enable extended verbs (equivalent to Shift+right-click).
Extended menus expose additional items like “Copy as path” or “Open PowerShell window here” that are normally hidden.
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!("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 owner(self, hwnd: isize) -> Self
pub fn owner(self, hwnd: isize) -> Self
Set an explicit owner window handle (as a raw isize / HWND).
If not set, a hidden helper window is created automatically. Set this when embedding the menu in an existing GUI application (e.g., Electron or a native Win32 app) so the menu is owned by your main window.
Sourcepub fn show_at(self, x: i32, y: i32) -> Result<Option<SelectedItem>>
pub fn show_at(self, x: i32, y: i32) -> Result<Option<SelectedItem>>
Show the context menu at the specified screen coordinates.
Returns Ok(Some(item)) if the user selected an item, or Ok(None) if
the menu was dismissed without a selection.
Sourcepub fn show(self) -> Result<Option<SelectedItem>>
pub fn show(self) -> Result<Option<SelectedItem>>
Show the context menu at the current cursor position.
Convenience wrapper around show_at.
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!("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}More examples
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}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}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 enumerate(&self) -> Result<Vec<MenuItem>>
pub fn enumerate(&self) -> Result<Vec<MenuItem>>
Enumerate all menu items without showing the menu.
Returns a flat list of MenuItem structs (submenus are nested inside
the submenu field). Useful for building custom UIs or for testing.
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}Sourcepub fn invoke_verb(&self, verb: &str) -> Result<()>
pub fn invoke_verb(&self, verb: &str) -> Result<()>
Invoke a shell verb directly without showing the menu.
This is useful for programmatically executing commands like “copy”, “cut”, or “paste” in response to keyboard shortcuts.
§Example
use win_context_menu::{init_com, ContextMenu, ShellItems};
let _com = init_com()?;
let items = ShellItems::from_path(r"C:\some\file.txt")?;
ContextMenu::new(items)?.invoke_verb("copy")?;