Click here to Skip to main content
Licence CPOL
First Posted 2 Feb 2006
Views 36,314
Bookmarked 24 times

Customize explorer's devious context menu

By | 2 Feb 2006 | Article
Do you want to add a menu item in windows explorer's context menu? then this article will show you easy way.

Introduction

Say, you have developed web based File Management System (FMS), where users can upload files. Your FMS will catalog all files and allow users to search & group uploaded files. If I am a user of such FMS, what I would like that I can right click on any file in windows explorer where it shows me menu item for sending file to FMS.

Image showing customized context menu

Say, you have built encryption application which can encrypt any file or file folder. Again, I would like if you do some magic to allow me to specify file/folder to be encrypted in context menu of windows explorer.

Say, you have created a command line tool, which does something with objects appearing in windows explorepr (files, folders, drives, network shares etc). If you can provide context menu item for your command line tool, your users will be happy - as they will not need to traverse folders at command line. Check this.

Background 

There are two ways of doing things. Easy way and toguh way. (sorry, if you do not like such openings :))

Let us see.

Tough way: Windows explorer knows COM. If you implement some interfaces like IContextMenu in a COM dll and then you register your dll with Windows registry in a way explorer understands, then you can make context menu do anything you want. The problem is you do not know COM! Bigger problem is explorer does not know .NET CLR yet! That translates that you do not want to do not want to be burderned with complex things ;-) Go here to know more.

Easy way: Windows explorer knows registry. you know C#. And .NET knows Registry. Whenever you rightclick in explorer, it looks into registry at a location to find custom context menu items (thanks to MS engineers, who forethought). If it finds one, then it displays it. If you choose that item, filepath is passed as command line parameter to specified exe.

I have gone easy way, and have created a simple class for you which will allow you to customize context menu programatically. As a proof of concept, I have created a small winforms project which will allow your to cusotmize context menu manually.

Using the code

Class is called ShellContextMenu.

Constructor accets Unique name which will be used to name registry key.

private ShellContextMenu m_scm = new ShellContextMenu("Newtonica.Utility.ContextMenu");

You simply then call public methods of instance to add, read or remove context menu items.

m_scm.SetContextMenuItem(ShellContextMenu.ContextMenuFor.ALLFILES,"Send to FMS...", "MyFMS.exe %1%", "");
m_scm.RemoveContextMenuItem(ShellContextMenu.ContextMenuFor.ALLFILES, ""); 

To understand it clearly, go through source code.

If you compliel exe, here is how it will look. (and work hopefully, too.). As I do not have FMS.exe, I have taken notepad.exe as place holder.

Screen of application

Points of Interest

1. To have complete flexibility, you need to implement COM interfaces tough way, and it is worth it.
2. If simple way is enough for the purpose, do not overkill. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

NewtonVer2

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberDavid Furshpan8:50 6 May '11  
QuestionHow to add context-menu for selected text??? Pinmemberashish1482@gmail.com8:19 14 Sep '10  
QuestionGreat and thanks for documenting the registry entries. What about setting the default icon? PinmemberMember 12588518:31 10 Dec '09  
Generalcannot creat multiple menus for a specific File Type Pinmemberahmasdrathore2:40 26 Oct '09  
GeneralCustomize explorer's devious context menu PinmemberMember 473197421:25 13 Apr '08  
GeneralThanks! PinmemberTyronM10:01 1 Apr '06  
omg. You're the greatest. I was just about to go trough all that overly complex COM Crap and then I found your article calling just simple functions to add and remove context menu items.
After reading all the Articles that uses COM to enhance the explorers context menu I never thought that there would be such an easy way to do it.
Thanks man! You saved me from the COM-Hell Big Grin | :-D
 
By the way, why is your Article in the Category MFC/C++? And you also have some typos in your text, just wanted to mention it Smile | :)
Questionwhat an article... PinmemberThomas Weidenmueller5:10 2 Feb '06  
AnswerRe: what an article... PinmemberNewtonVer218:45 2 Feb '06  
Generalbroken links... Pinmembertoxcct5:02 2 Feb '06  
GeneralRe: broken links... PinmemberNewtonVer218:59 2 Feb '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 2 Feb 2006
Article Copyright 2006 by NewtonVer2
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid