How do you show the Windows Explorer context menu from a C# application?
Asked Answered
P

3

19

I have a file listing in my application and I would like to allow people to right-click on an item and show the Windows Explorer context menu. I'm assuming I would need to use the IContextMenu interface, but I'm not really sure where to start.

Philosopher answered 16/1, 2009 at 17:28 Comment(0)
M
8

There's a very good tutorial (albeit in C++) about hosting an IContextMenu on Raymond Chen's blog in 11 parts (in order):

  1. Initial foray
  2. Displaying the context menu
  3. Invocation location
  4. Key context
  5. Handling menu messages
  6. Displaying menu help
  7. Invoking the default verb
  8. Optimizing for the default command
  9. Adding custom commands
  10. Composite extensions - groundwork
  11. Composite extensions - composition
Motch answered 19/1, 2009 at 8:50 Comment(1)
This is perfect. Not in C#, but it explained how to do it, and that's all I need.Philosopher
S
13

I have written a library that might be able to help you. You could use the controls provided by the library, or if you don't want to do that, looking through the code may give you an answer.

You can find the library at: http://gong-shell.sourceforge.net/

Please let me know if this helped!

Supination answered 16/1, 2009 at 17:37 Comment(1)
This solution is apparently for .NET 2.0 only :(Caiman
M
8

There's a very good tutorial (albeit in C++) about hosting an IContextMenu on Raymond Chen's blog in 11 parts (in order):

  1. Initial foray
  2. Displaying the context menu
  3. Invocation location
  4. Key context
  5. Handling menu messages
  6. Displaying menu help
  7. Invoking the default verb
  8. Optimizing for the default command
  9. Adding custom commands
  10. Composite extensions - groundwork
  11. Composite extensions - composition
Motch answered 19/1, 2009 at 8:50 Comment(1)
This is perfect. Not in C#, but it explained how to do it, and that's all I need.Philosopher
B
6

I found a great Code Project article that encapsulates everything very nicely into one class!

Explorer Shell Context Menu

It's as easy as the following code snippet:

// Sample code

ShellContextMenu ctxMnu = new ShellContextMenu();
FileInfo[] arrFI = new FileInfo[1];
arrFI[0] = new FileInfo(this.treeMain.SelectedNode.Tag.ToString());
ctxMnu.ShowContextMenu(arrFI, this.PointToScreen(new Point(e.X, e.Y)));

The only irksome thing is that it takes either an array of FileInfo[] or an array of DirectoryInfo[] although it was VERY easy to modify the source slightly so that it would take an array of FileSystemInfo[]

Beecham answered 20/8, 2009 at 4:7 Comment(2)
That library works fine most of the time, but under some circumstances it blows due to Delegates being released too early, and I can't seem to find where.Ossieossietzky
In case the CodeProject links permanently die: pastebin.com/bfjX7ysX Note that I had to change the ShellHelper.HiWord and ShellHelper.LoWord to ulong but other than that it worked out of the box on .NET 4.6.1Cyclone

© 2022 - 2024 — McMap. All rights reserved.