Registering Shortcut Menus in Windows
I was working on something whose UI involves right-click menu. Do not have the time to chew through Microsoft docs so I assembled the informations I gathered online.
How-To
- You edit the Windows Registry.
- You don't need to call Win32 API to edit the registry (if you can accept your program asking users for permission), but a Win32 API is provided.
- You can generate a
.reg
file and executeregedit.exe /S [file.reg]
..reg
file is very similar to an.ini
file so it should be easy.- You start with
Windows Registry Editor Version 5.00
and a blank line. If you need to support OSes older than Windows 2000, then it'sREGEDIT4
. - Each section should be separated by a blank line or else it wouldn't register.
- To delete a key, add
-
before the key path, so it's[-PATH]
instead of[PATH]
(which is adding/editing the key).
- You start with
- The "root path" for shortcut menu is:
# for any file, global. "*" matches any extension name; you can also add to the corresponding extension name. HKEY_CLASSES_ROOT\*\Shell # for directory, global. HKEY_CLASSES_ROOT\Directory\Shell # for right-clicking in directory background, global. HKEY_CLASSES_ROOT\Directory\Background\Shell # for any file, local. HKEY_CURRENT_USER\SOFTWARE\Classes\*\Shell # for directory, local. HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Shell # for right-clicking in directory background, local. HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\Shell # for submenu item. (explained later) HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell
- To change the label of the menu-item, edit the default value of the corresponding key. To add a command for executing when selecting the menu-item, add a
Command
subkey and edit the default value. e.g. to add a menu item for directory background locally with nameMyCommand
, label "Execute notepad" and command "notepad.exe", you do this:[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\Shell\MyCommand] @="Execute notepad" [HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\Shell\MyCommand\Command] @="notepad.exe"
An icon can be added as well:
[HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\Shell\MyCommand] @="Execute notepad" "Icon"="C:\\MyIconPath.ico"
The icon path can refer to an
.ico
(of course), an.exe
or a.dll
; in Windows.exe
and.dll
can contain icons and Windows will retrieve the icon from there.You can also control the position of the menu item to be "Top" or "Bottom" by adding a REG_SZ value with name "Position".
- To add a two-level menu, you don't add the
Command
subkey, but instead add value namedMUIVerb
(this would be the label) andSubCommands
(this would be referring to the submenu items).Icon
andPosition
can also be added. - To add submenu items to a two-level menu, you need to first add those item at the path for submenu items mentioned above, and refer their names (not labels) in the
SubCommands
field, separated by semicolon;
. To add a separator item, use|
instead of command names. - Maybe more than 2 levels of submenu is supported. I haven't tested myself so I'm not sure.
Examples
For example, to add a shortcut menu like this:
+ Send this file to Heaven + execute command `send-to.exe heaven north-europe` + icon `C:\MyFiles\n-europe.ico` + Send this file to Hell (Brazil) + execute command `send-to.exe hell brazil` + icon `C:\MyFiles\brazil.ico` + Send this file to Extra Hell + icon `C:\MyFiles\china.ico` + To Shanghai + execute command `send-to.exe extra-hell shanghai` + icon `C:\MyFiles\shanghai.ico` + ---- + To Dongxing + execute command `send-to.exe extra-hell dongxing` + To Shenzhen + execute command `send-to.exe extra-hell shenzhen` + ---- + To Yili + execute command `send-to.exe extra-hell yili`
The .reg
file needed would be:
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\Shell\Send.Heaven] @="Send this file to Heaven" "Icon"="C:\\MyFiles\\n-europe.ico" [HKEY_CLASSES_ROOT\*\Shell\Send.Heaven\Command] @="send-to.exe heaven north-europe" [HKEY_CLASSES_ROOT\*\Shell\Send.Hell] @="Send this file to Hell" "Icon"="C:\\MyFiles\\brazil.ico" [HKEY_CLASSES_ROOT\*\Shell\Send.Hell\Command] @="send-to.exe hell brazil" [HKEY_CLASSES_ROOT\*\Shell\Send.ExtraHell] @="" "MUIVerb"="Send this file to Extra Hell" "SubCommands"="Send.ExtraHell.Shanghai;|;Send.ExtraHell.Dongxing;Send.ExtraHell.Shenzhen;|;Send.ExtraHell.Yili" "Icon"="C:\\MyFiles\\china.ico" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Shanghai] @="To Shanghai" "Icon"="C:\\MyFiles\\shanghai.ico" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Shanghai\Command] @="send-to.exe extra-hell shanghai" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Dongxing] @="To Dongxing" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Dongxing\Command] @="send-to.exe extra-hell dongxing" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Shenzhen] @="To Shenzhen" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Shenzhen\Command] @="send-to.exe extra-hell shenzhen" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Yili] @="To Yili" [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Yili\Command] @="send-to.exe extra-hell yili"
And the .reg
file used to delete all this would be:
Windows Registry Editor Version 5.00 [-HKEY_CLASSES_ROOT\*\Shell\Send.Heaven] [-HKEY_CLASSES_ROOT\*\Shell\Send.Heaven\Command] [-HKEY_CLASSES_ROOT\*\Shell\Send.Hell] [-HKEY_CLASSES_ROOT\*\Shell\Send.Hell\Command] [-HKEY_CLASSES_ROOT\*\Shell\Send.ExtraHell] [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Shanghai] [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Shanghai\Command] [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Dongxing] [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Dongxing\Command] [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Shenzhen] [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Shenzhen\Command] [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Yili] [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\Shell\Send.ExtraHell.Yili\Command]