Welcome to MSDN Blogs Sign in | Join | Help

How to host an IContextMenu, part 4 - Key context

Another of the bugs you may have noticed in our first attempt at displaying the context menu to the user is that the Delete command doesn't alter its behavior depending on whether you hold the shift key. Recall that holding the shift key changes the behavior of the Delete command, causing it to delete a file immediately instead of moving it to the Recycle Bin. But in our sample program, it always offers to move the file to the Recycle Bin, even if you have the shift key down.

(You can see the difference in the wording of the dialog and in the icon. If the operation is to move the item into the Recycle Bin, you get a Recycle Bin icon and the text asks you to confirm sending the item to the Recycle Bin. If the operation will delete the item permanently, then you get an icon that shows a file and a folder fading away and the text asks you to confirm deleting the item.)

To convey this information to the context menu, you need to pass the key states in the CMINVOKECOMMANDINFOEX structure.

          CMINVOKECOMMANDINFOEX info = { 0 };
          info.cbSize = sizeof(info);
          info.fMask = CMIC_MASK_UNICODE | CMIC_MASK_PTINVOKE;
          if (GetKeyState(VK_CONTROL) < 0) {
            info.fMask |= CMIC_MASK_CONTROL_DOWN;
          }
          if (GetKeyState(VK_SHIFT) < 0) {
            info.fMask |= CMIC_MASK_SHIFT_DOWN;
          }

Make this change and observe that the dialogs you get from the Delete option now respect your shift key state.

Warning: Before playing with this, make sure that you have enabled delete confirmation warnings or you will end up deleting your clock.avi file for real! If you want to play around with the Delete option, you may want to tweak the program so it operates on a file you don't mind losing.

Exercise: There's another place where key context influences the context menu, namely the convention that holding the shift key while right-clicking enables "extended verbs". These are verbs that are lesser-used and therefore do not appear on the conventional context menu to avoid creating clutter. For homework, incorporate the extended verb convention into the sample program.

[Sorry today's entries are late. Had problems connecting to the blog server.]

Published Friday, September 24, 2004 6:09 PM by oldnewthing
Filed under:

Comments

# re: How to host an IContextMenu, part 4 - Key context

Saturday, September 25, 2004 9:12 AM by Dave
>> Warning: Before playing with this, make sure that you have enabled delete confirmation warnings or you will end up deleting your clock.avi file for real! If you want to play around with the Delete option, you may want to tweak the program so it operates on a file you don't mind losing. <<

Of course, if we are good little programmers, we shouldn't be running as administrator.

# re: How to host an IContextMenu, part 4 - Key context

Sunday, September 26, 2004 5:19 AM by ep
if (GetKeyState(VK_SHIFT) < 0)
{
Call QueryContextMenu with CMF_EXTENDEDVERBS
}

# re: How to host an IContextMenu, part 4 - Key context

Sunday, September 26, 2004 2:24 PM by Jim
Nice set of articles that are long over due! I have a question that a bit more along the lines of the first article that has to do with the verb to invoke.

I have found that depending on the IShellFolder executing the "verb" string can lead to results that are different than if you execute the same through explorer. I can't recall the exact example but I found that if I take the requested "verb" to execute and compare it to what I get back from GetCommandString and if equal (i.e. it is a common verb) I then instead of setting lpVerb to the "verb" I use MakeIntResource to set lpVerb I get consistent results with Explorer. Can you elaborate on that more?

The only note in my code I put was this:

{The result of using the 'verb' string and the MakeIntResource is different expecially on system folders. This forces it to use MakeIntResource if it can}

# Shell Extensibility in Longhorn

Wednesday, October 13, 2004 9:29 AM by notgartner.com: Mitch Denny's Blog

# IContextMenu のホスト方法 - Shell

Wednesday, March 22, 2006 1:33 PM by 社本@ワック Blog
IContextMenu のホスト方法 - Shell

# Why doesn't the Shutdown dialog use Alt to get alternate behavior?

Tuesday, September 19, 2006 10:00 AM by The Old New Thing
Because Alt already has other meaning.
New Comments to this post are disabled
 
Page view tracker