How to host an IContextMenu, part 4 – Key context

Date:September 24, 2004 / year-entry #348
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20040924-00/?p=37753
Comments:    6
Summary: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...

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


Comments (6)
  1. Dave says:

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

  2. ep says:

    if (GetKeyState(VK_SHIFT) < 0)

    {

    Call QueryContextMenu with CMF_EXTENDEDVERBS

    }

  3. Jim says:

    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}

  4. IContextMenu のホスト方法 – Shell

  5. Because Alt already has other meaning.

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index