Under Windows 8/8.1 x64, I would like to assign a custom icon for the default Windows context menu items such as Copy, Cut, Paste, Delete, Undo, Redo and Send To items, which by default has any icon:
Where I can locate the "reference" to those context menu items in the registry then add a "icon" registry value for them?
Or in other words, how to assign a icon to a shell extension menu like the SendTo shellex?.
Research
As commented by @Sk8erPeter, seems that:
"Adding the
Icon
string value to different context menu handlers
doesn't work like when adding it to a custom item like e.g.
HKEY_CLASSES_ROOT\*\shell\MYCUSTOMKEY
"
Best Answer
Affiliation notice: I am the author of the software mentioned in this answer.
First up, I'll have you know that I learned C++ and Win32 just for this question.
I have developed a 64-bit shell extension that gets registered as a context menu handler. When it's invoked, it rummages through the existing menu items, looking for interesting entries. If it finds one, it sticks an icon on it (which must have been loaded earlier). At the moment, it looks for Copy, Cut, Delete, Paste, Redo, Send to, and Undo. You can add your own by modifying the code; the procedure for this is described below. (Sorry, I'm not good enough at C++ to make it configurable.)
A screenshot of it in action, with the ugliest icons known to man:
You can download these icons if you really want to.
Setting it up
Download it (from my Dropbox). Notice: this file is detected by one VirusTotal scanner as being some form of malware. This is understandable, given the kind of things it has to do to whack the existing entries. I give you my word that it does no intentional harm to your computer. If you're suspicious and/or you want to modify and extend it, see the code on GitHub!
Create a folder in your C drive:
C:\shellicon
. Create BMP files with the following titles:copy
,cut
,delete
,paste
,redo
,sendto
,undo
. (Hopefully it's obvious which one does which thing.) These images should probably be 16 by 16 pixels (or however big your DPI settings make the menu margin), but I've had success with larger ones as well. If you want the icons to look transparent, you'll have to just make their background the same color as the context menu. (This trick is employed by Dropbox as well.) I made my terrible icons with MS Paint; other programs may or may not save in a manner compatible withLoadImageA
. 16 by 16 at 24-bit color depth at 96 pixels per inch seems to be the most reliable set of image properties.Put the DLL somewhere accessible to all users, that folder you just made is a good choice. Open an admin prompt in the folder containing the DLL and do
regsvr32 ContextIcons.dll
. This creates registration information for the shell types*
,Drive
,Directory
, andDirectory\Background
. If you ever want to remove the shell extension, doregsvr32 /u ContextIcons.dll
.Relevant code
Basically, the extension just queries every context menu item's text with
GetMenuItemInfo
and, if appropriate, adjusts the icon withSetMenuItemInfo
.Visual Studio generates a lot of magic mysterious code for ATL projects, but this is the contents of
IconInjector.cpp
, which implements the context menu handler:Note that the
HBITMAP
s are never cleaned up, but this doesn't matter too much given that the DLL's stuff will go away when Explorer shuts down. The icons barely take any memory anyway.If you're compiling for 32-bit, the first parameter to
GetCommandString
is just aUINT
instead of aUINT_PTR
.If you really want transparent icons, you'll have to create a window with the desired icon and then set
mii.hBmpItem
toHBMMENU_SYSTEM
and put the handle to the window inmii.dwItemData
, as described at the bottom of the MSDN article onMENUITEMINFO
. I wasn't able to figure out how to create windows from shell extensions.LR_LOADTRANSPARENT
looks promising as a flag ofLoadImageA
, but it has its own pitfalls - specifically, not working unless you use 256-color bitmaps.If you experience problems with image loading, try removing the
LR_DEFAULTSIZE
flag from theLoadImageA
calls.Somebody sufficiently skilled in C++ could probably grab resources out of other DLLs and convert them to
HBITMAP
s, but that somebody is not me.Modifying it
I wrote this in Visual Studio, which I believe to be the best editor for Windows C++.
Load up the SLN file into Visual Studio 2015 after you install the C++ tools. In
IconInjector.cpp
, you can addHBITMAP
entries at the top andLoadImageA
calls inInitialize
to add new icons. Down in theelse if
section, use awcscmp
call to look for an exact match, or awcsstr
call to look for the presence of a substring. In both cases, the&
represents the position of the underline/accelerator when using Shift+F10. Set your mode to Release and your architecture to x64, and do Build → Build Solution. You'll get an error about failing to register the output, but don't worry; you'd want to do this manually anyway. End Explorer, copy the new DLL (\x64\Release\ContextIcons.dll
in the solution folder) to the place, then do theregsvr32
dance.Attributions
Many thanks to the MSDN writers, and to the creator of "The Complete Idiot's Guide to Writing Shell Extensions", which I referenced heavily.
Eulogy
To the many Explorer instances that were killed in the production of this shell extension: you died for a great cause, that some people on the Internet can have icons next to their words.