Click here to Skip to main content
15,896,500 members
Articles / Programming Languages / C++

Extending Context Menus and Changing File Icons

Rate me:
Please Sign up or sign in to vote.
4.47/5 (14 votes)
27 Nov 2001CPOL6 min read 101.2K   39   9
An article on Changing Default File Icons and Adding your commands in Context Menu

Introduction

Last time I posted my article on CodeProject.com for changing the Default Icons of the Folders. Now I am again here with another article which will demonstrate the method to change the unknown file types Icons and to associate any Program with any File Type.

As we know that any File whose extension is not recognized by the Windows, always has the default icon as shown in the picture above. But sometime we need to display an icon when our file is shown in the Windows Explorer. Like Text File has an Icon, Image File has an Icon and so on. So, why don't have our file ? It should have. Let we try to do this.

Actually, each file type (file Extension) is registered with windows. If we register our file type, then it will be very easy to put any icon on that file when that is shown in the windows explorer. To register any file type, we have to manipulate registry.

Let us assume that we have a file type with extension .SOS, and we want see an icon (we already have chosen), on this file when it is shown in the windows explorer. To do this, you have to add some keys in the Windows Registry. Open the registry with regedit.exe and Expand the HKEY_CLASSES_ROOT.

Now create a key under HKEY_CLASSES_ROOT named .sos (Note that SOS is the Extension of our File) and Set it's Default value to any text like "SOSFile". These settings should look like this.

Image 1

OK. Now Create another key under the HKEY_CLASSES_ROOT named "SOSFile" (Note that it is the Default value for the .sos key. You must have to make this key of same name which was mentioned in .sos default value). Now you can set the Default value of this key to any text. This text will be shown as the description of our .sos file, Like this

Image 2

OK. Now you explore your files and select one. You will see that the description "Save Our Souls" is displayed there instead of "SOS File" which was by default "extension File" where extension is the extension of the file. That may look like this.

Image 3

Now we have registered our file with the windows. Next step is to register an icon for this file or tell the windows to place an icon on this file instead of default icon. To do this, make another key under "SOSFile" key. Name that "DefaultIcon" and set it's default value to the path of icon file. You can give the path of any exe file or dll file which contains icons, like iexplore.exe, the Internet Explorer File. But if you are using .exe or .dll file, you must have to mention the icon index in that file. You can do this by placing "," after file path and name and then the number (index) of icon, like 2, 3. When you will done this it will look like this in registry.

Image 4

Now Explore your files, you will see the Icon you specified like I have this view.

Image 5

That's all. You have registered your file extension with Windows. Windows recognizes it. The next step, you may want, is to associate this file with any Program, Say "Notepad.exe". Means, when you double click on it, Notepad should open it. Or when you Right click on it, any option should be available in context menu which allow you to open it with a program. To do this, you have to add some more keys.

First add "Shell" Key under "SOSFile" key. Then Add any other key under "Shell" Key Like "Open My File". Shell key tells the system that this file has association with any program. And that porgram is mentioned under the shell key as We mentioned with the key "Open My File". To mention the program for text "Open My File", we must have to add another key under "Open My File" with the name "Command" and we must have to set it's default value to the path of the program, like we are mentioning Notepad.exe. Your Registry should look like this.

Image 6

Note that I mentioned "%1" after the path of Notepad.exe. This parameter tells the Notepad to open the file. If you do not mention this "%1", then it will only open the notepad, not open the file in the notepad.

You can set the Default value for "Open My File" Key. If you set this, like "Open My File in &Notepad", then this text will be displayed in the context menu. Do this to check. Set the Default value of "Open My File" to "Open My File In &Notepad" and then you will see like this.

Image 7

The "&" sign is placed for the shortcut key. Note that "N" is underlined in the context menu. If you do not want to make any shortcut, you can ommit this "&" in the key value.

You can also make more than one commands for a file type. Just add more keys under the shell like you made "Open My File" Let we add one to print. We name that "Print My File" For that, you have to repeat steps which you followed for "Open My File" Add key named "Print My File" under "Shell" and then Add another key "Command" under "Print My File". Set the default value of "Command" to path of Program which will be responsible of printing. Like we mention here Word Pad. So, your registry will look like this.

Image 8

Note that "/p" is added in the path of Program file. The switch which will tell the program to print the file. Now your program, when receive this switch, should print the file. It doesn't belong to the operating system. It is the Program File's responsibility to print the file.

In the final, There is one thing left. When you are adding more than one commands for a file type, you may want to set a default command. Normally, the command added first or appears first is the default. But you can mentioned it in the "Shell" Key's default value. Like you want to make "Print My File" as the default command, so that when user double click on the file, this command should be invoked. To do this, just Set the default value of "Shell" to "Print My File". That's all. Now when you will see in the context menu, the "Print My File" will be displayed as bold.

Note : Since windows sometimes takes some time to refresh itself, you may have to wait to see the effects of your work. Otherwise, restart windows.

This was another use of registry. My one article on the basics of registry is also available here. You can check that. But this is more advance. I will be back with more tips. For that, let me sleep.

Be theAngrycodeR, with theAngrycodeR

License

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


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

Comments and Discussions

 
QuestionCongratulations and question Pin
CQüeb15-Jun-09 12:34
CQüeb15-Jun-09 12:34 
Generalthanks Pin
Sameerkumar Namdeo24-Nov-08 19:50
Sameerkumar Namdeo24-Nov-08 19:50 
Generalthanks Pin
Sameerkumar Namdeo24-Nov-08 19:49
Sameerkumar Namdeo24-Nov-08 19:49 
GeneralTo place icon along with the shortcuts Pin
neenatm30-Jan-06 19:35
neenatm30-Jan-06 19:35 
GeneralProgram Code Pin
Debs*29-Aug-05 9:42
Debs*29-Aug-05 9:42 
GeneralRe: Program Code Pin
Anonymous2-Oct-05 13:54
Anonymous2-Oct-05 13:54 
Generalrefresh icons Pin
-oli-19-Aug-05 0:12
suss-oli-19-Aug-05 0:12 
GeneralJust what I was looking for Pin
Saint Jude10-Nov-03 8:56
Saint Jude10-Nov-03 8:56 
GeneralThanks Pin
Rogerio Silva19-Dec-01 6:34
Rogerio Silva19-Dec-01 6:34 

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.