Log in

No account? Create an account

Previous Web | Next Web

Note: The information in this entry describes a change to the Windows' Registy. If you would like to apply this change, but you do not understand what the Registry is, what it does, what manually editing the Registry could mean for your computer or what the modification described is doing, YOU SHOULD NOT MAKE THE CHANGE(S) DESCRIBED. If you proceed and Something Bad™ happens to you or your computer, sorry, but that's your problem!

While I was working on the Administrative Editor context-menu entry (described in this post), I stumbled upon a little bit of Microsoft documentation on adding cascading context-menu entries to Explorer using only the Registry, so I thought I'd have a quick play! And it turns out that this is actually incredibly easy.

There are three steps:

  1. Add a new context-menu entry, configured as a cascading menu
  2. Decide what entries you want and create unique "verb" entries for them
  3. Add the "verbs" to the cascading menu

As an experiment, I decided I would add an "Administrative Tasks" submenu to the context-menu for "all items" (that is, the "*" key of HKCR), and that I'd have two entries (based on the Administrative Editor modification that triggered this little investigation) in my submenu; one for GVim and one for Notepad.

Note: I'm not going providing .reg files for cut'n'pasting for this. While the process is easy, one of the keys you are adding to already has content present, and I have no way of knowing if you have entries there that I don't... and accidentally if a .reg file I provided were to overwrite something important in there, it would be extremely difficult to track down and fix.

Note: Explorer only gained this capability in Windows 7. If you're using Vista or earlier, I'm afraid this will not work for you.

The Cascading Context-Menu Entry

This is created just like any other context-menu entry; add a subkey to the shell key under the particular HKCR subkey you want it for.... in other words, for "all items", you add a new subkey under HKCU\*\shell, for a 'txtfile', you add a new subkey under HKCU\txtfile\shell. For my example, I now have a key at HKCU\*\shell\admintasks.

Next up isto tell Windows that this is a cascading submenu. To do this, I just have to add a String Value (a REG_SZ) called MUIVerb, with the value being the text you want displayed in the context-menu, ie "Administrative Tools" in my example. Easy!

You can also add an "Extended" REG_SZ (just as you can for a normal menu entry) if you only want the submenu to show up when the context-menu is triggered by shift-right-click.

At this point, if we open a context-menu for a file, all you'll see is a normal menu entry, ie:

[Context-menu showing Administrative Tools entry]

Adding Verbs

As we've already seen, in order to add submenu support, Microsoft have reused the same basic mechanism for defining a context-menu entry as has (I presume) always been available in the Explorer shell. They have had to extend it a little to add in this extra capability, but I haven't introduced that part yet.

But when it came to defining the entries for the submenu, guess what?

Yep! They used the same mechanism....

But (you might think) that means that there has to be a way to connect the context-menu entry for the submenu to the different entries that will make up the submenu. And if you did think that, then you'd be bang on, there does, and there is... and it comes in two parts, the first of which are "verbs".

Now, a "verb" is what (in the documentation) Microsoft call the combination of the key-structure in the Registry that defines a submenu entry and the label that is attached to it. The key-structure is exactly the same as a context-menu entry, its just stored in a different Registry key; HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell.

So now we know the what and the wherefore, we can add some new verbs. Remember, I want to have two entries in my submenu, one for an Administrative "Edit with Vim" and one for an Administrative "Edit with Notepad"? If I were just adding these to the normal context-menu, I'd add the keys runas_gvim and runas_notepad, so I'm going to use those same key names as my verbs, which means that I modify the Registry like this:

Registry Key: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell

    (default) = Edit with &ViM
    HasLUAShield = ""
      (default) = "c:\program files\x64\nircmd.exe" elevate "c:\program files 
(x86)\vim\vim74\gvim.exe" "%1"
    (default) = Edit with &Notepad
    HasLUAShield = ""
      (default) = "c:\nirsoft\x64\nircmd.exe" elevate notepad.exe "%1"

The first of which, if you read the previous post, should look familiar.

Note: The HasLUAShield value displays the UAC shield icon in the menu and can be omitted if the command doesn't trigger a UAC prompt.

Joining The Dots

So, we have a context-menu entry and we have some verbs, the final piece of the puzzle is to connect the two together, and to do this, all we do is add a list of verbs under the context-menu entry in a value called 'SubCommands', with the verbs separated by semicolons, ie:

Registry Key: HKCU\*\shell\admintasks

  SubCommands = runas_gvim; runas_notepad

And if we right-click a file, we can see that we now have a submenu, complete with entries.

[Context-menu showing Administrative Tools submenu]