September 24th, 2004

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.]

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.