Introduction
It’s been a while, today we have a pretty awesome content to explore that is how we can create the right-click context menu in Windows.
I found that there’s not a lot of comprehensive and up-to-date tutorial on this subject for tech artists, but this would be a really powerful tool to speed up any types of workflow that involves going into Windows Explorer. (for example, right-click on a file and have a shortcut to get its perforce depot info)
And the best thing is: it’s super easy to follow, certainly much easier than learning how to create a context menu in Qt.
Registry Setup
Everything regards to the Windows context menu setup is done within the Windows Registry Editor.
Hierarchy
First I’m gonna do an example of manual implementation.
-
we’ll want to navigate to this path for now:
HKEY_CURRENT_USER\Software\Classes\directory\Background\shell
(we’ll explain this later here)- right-click on the ‘Background’ key/folder and create a new ‘key’ named ‘shell’ if there isn’t one already
-
create a key under ‘shell’ (I named mine ‘test_menu’) and then create a key under it called ‘command’
Field
Next we’ll want to modify the field of those keys, in the menu item key (in my case the ‘test_menu’) there are some optional fields we can add customize the appearance of our menu item.
The following are some optional String Field:
(To add field, right-click on the right panel, ‘New’ - ‘String Value’ )
- MUIVerb: The display name of the menu item, if this is not set, the name will default to the key name (i.e. “test_menu” in our case)
- Icon: For displaying icon, the data should be set as the icon path, common format for the icons are .ico and .dll
- Position: changes the location of the menu item, e.g. “Top”, “Bottom”
Setting up Command
Finally, we’ll want to modify the command key’s field, so it can actually perform custom operations.
Go to the {Default} and edit the Data to whatever operation we would like to perform when clicking this menu item. The syntax would be an execution command similar to ones we would put in command prompt or a .bat file.
Running Python Script
Here’s an example to run python script:
In the Data section we would put: "path to python.exe" "path to python script.py"
Passing Argument
In order to pass in arguments, such as
the directory or the file where the menu was initiated.
We add %v
for directory and %1
for file path at the end of the command Data:
- e.g.
"D:/python39/python.exe" "D:/p4clipboard.py" "%1"
- be aware of the slashes
- if you would like to debug your python script and not make it close immediately after execution simply
add
-i
flag after the"python.exe"
And within the p4clipboard.py
file, we can parse the argument passed in as such:
1 | # converts the selected file to its perforce depot path and add to clipboard |
Python Registry Automation
The above examples are very cool, but requires a lot of manual set up, we can leverage Python to set up the registry entries for us.
1 | # example from: https://github.com/seddie95/folder_organizer/blob/master/make_key.py |
Reg File
Another way to transfer out the registry setup is by exporting
out from registry editor directly to a .reg
file.
This can be done by simply selecting the top key and right-click
and select ‘Export’; the .reg
file is a text based file.
Cascade Menu
How should we go about implementing a multi-layered menu? I found a lot of examples online that are either complicated/messy or not working at all.
Eventually I’ve come across this blog explaining what I think is the ideal way to implement Cascading menu. It keeps everything in one place, has this clean and easy to understand hierarchy, and very easy.
Essentially, we only need to add one additional field on the
top level menu item called: subcommands
, and we then keep
all the children menu item nested within it like what we have previously done.
(An example of the full hierarchy breakdown)
Target Type
Different context menu triggering condition requires editing different registry keys, This goes the same for different user group (registration visible to the current user vs. affecting globally to the machine).
Background: when right-click on the backdrop of the explorer or desktop
if you are administrator
HKEY_CLASSES_ROOT\Directory\Background\shell
if you are a normal user
HKEY_CURRENT_USER\Software\Classes\directory\Background\shell
Folder: when right-click on a folder in the explorer or desktop
if you are administrator
HKEY_CLASSES_ROOT\Directory\shell
if you are a normal user
HKEY_CURRENT_USER\Software\Classes\directory\shell
File: when right-click on a file in the explorer or desktop
if you are administrator
HKEY_CLASSES_ROOT\*\shell
if you are a normal user
HKEY_CURRENT_USER\Software\Classes\*\shell
File Type Association: when right-click on a specific file type:
if you are administrator:
HKEY_LOCAL_MACHINE\Software\Classes\SystemFileAssociations\{.format}\shell
if you are a normal user:
HKEY_CURRENT_USER\Software\Classes\SystemFileAssociations\{.format}\shell
Noteworthy Mentions
To further customize the context menu, we might be interested with adding separators between menu items, also having specific order or menu items. These can all be achieved quite easily described in Sverrir’s Blog which I’ve linked below.
Furthermore, there are also menu item with toggle box or check box; these are not explored as of now.
References
Stack Overflow - How add context menu item to Windows Explorer for folders
Notion - Creating a context menu with sub-menu for Windows for a specific file extension
Stack Overflow - Add menu item to Windows context menu only for specific filetype
Sverrir’s Blog - Creating Cascading Menu Items in Windows Explorer 7, 8, 8.1 & 10
Microsoft - Enhancing the “Open Command Prompt here” Context Menu experience