Click here to Skip to main content
15,878,809 members
Articles / Programming Languages / C++
Article

Customize explorer's devious context menu

Rate me:
Please Sign up or sign in to vote.
3.13/5 (16 votes)
2 Feb 2006CPOL2 min read 53.5K   1.3K   24   10
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. <comment title="----------------------------- That's it! -------------------------" xmlns="http://disruptive-innovations.com/zoo/nvu">

License

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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
David Furshpan6-May-11 8:50
David Furshpan6-May-11 8:50 
QuestionHow to add context-menu for selected text??? Pin
ashish1482@gmail.com14-Sep-10 8:19
ashish1482@gmail.com14-Sep-10 8:19 
QuestionGreat and thanks for documenting the registry entries. What about setting the default icon? Pin
Member 125885110-Dec-09 8:31
Member 125885110-Dec-09 8:31 
Generalcannot creat multiple menus for a specific File Type Pin
ahmasdrathore26-Oct-09 2:40
ahmasdrathore26-Oct-09 2:40 
GeneralCustomize explorer's devious context menu Pin
Member 473197413-Apr-08 21:25
Member 473197413-Apr-08 21:25 
I like your tip very much, but unfortunatelly I do not know much about C++. Do you have the project source code in VB6? I will appreciate your help. Please replay to cmills@fasttrack.net.au
Thanks, Carlos.
GeneralThanks! Pin
TyronM1-Apr-06 10:01
TyronM1-Apr-06 10:01 
Questionwhat an article... Pin
Thomas Weidenmueller2-Feb-06 5:10
Thomas Weidenmueller2-Feb-06 5:10 
AnswerRe: what an article... Pin
NewtonVer22-Feb-06 18:45
NewtonVer22-Feb-06 18:45 
Generalbroken links... Pin
toxcct2-Feb-06 5:02
toxcct2-Feb-06 5:02 
GeneralRe: broken links... Pin
NewtonVer22-Feb-06 18:59
NewtonVer22-Feb-06 18:59 

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

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