5,693,062 members and growing! (18,529 online)
Email Password   helpLost your password?
Desktop Development » Shell and IE programming » General     Intermediate

How to Use Submenus in a Context Menu Shell Extension

By Michael Dunn

How to manage submenus in a context menu extension
VC6, C++Windows, NT4, Win2K, WinXP, ATL, VS6, Visual Studio, Dev

Posted: 14 Feb 2003
Updated: 14 Feb 2003
Views: 97,449
Bookmarked: 46 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
29 votes for this Article.
Popularity: 7.11 Rating: 4.86 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
2 votes, 6.9%
4
27 votes, 93.1%
5

Submenus in a context menu extension

In this article, I'll cover a tricky aspect of context menu extensions - submenus. The approach most people take at first to creating submenus leads to odd behavior in Explorer, but once you know the trick to make Explorer manage the menu correctly, it's easy! This article assumes you have a good grasp of context menu extensions. If you need a refresher, see part 1 and part 2 of my shell extension series.

Adding a submenu

This article's extension is a simple Open With... submenu, which has two items, Notepad and Internet Explorer. It behaves like the enhanced Open With menu in XP, and opens the selected file in the program that you pick. This Open With menu will demonstrate how to properly create a submenu in an extension.

What you might try first

The first thing that comes to mind is to create a new menu with CreatePopupMenu() and insert it into the menu provided by Explorer.

HRESULT COpenWithCtxMenuExt::QueryContextMenu ( 
  HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, UINT uidLastCmd, UINT uFlags )
{
    // If the flags include CMF_DEFAULTONLY then we shouldn't do anything.

    if ( uFlags & CMF_DEFAULTONLY )
        return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );

    // First, create and populate a submenu.

    HMENU hSubmenu = CreatePopupMenu();
    UINT uID = uidFirstCmd;

    InsertMenu ( hSubmenu, 0, MF_BYPOSITION, uID++, _T("&Notepad") );
    InsertMenu ( hSubmenu, 1, MF_BYPOSITION, uID++, _T("&Internet Explorer") );

    // Insert the submenu into the ctx menu provided by Explorer.

    InsertMenu ( hmenu, uMenuIndex, MF_BYPOSITION | MF_POPUP, 
                 (UINT_PTR) hSubmenu, _T("C&P Open With") );

    return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, uID - uidFirstCmd );
}

This actually works fine for the context menu, however the context menu items you add are duplicated on Explorer's File menu. If you invoke the context menu repeatedly, you'll see leftover popups on the File menu:

 [Leftover submenus - 12K]

The cause is related to how Explorer cleans up its menus after extensions are invoked. The return value of QueryContextMenu() tells Explorer how many items we add, and Explorer cleans up by calculating the IDs of our items and deleting them. Explorer knows the ID of the first item (since it passes us the value as uidCmdFirst) and can calculate the IDs of the others since the IDs are assumed to be consecutive. However, the popup menu has no ID, so Explorer doesn't delete it.

The correct way

The secret is to insert the submenu using the InsertMenuItem() API. What's different about using InsertMenuItem() is that you can give the submenu an ID, which isn't possible when you use InsertMenu(). Here's the corrected QueryContextMenu() code.

HRESULT COpenWithCtxMenuExt::QueryContextMenu ( 
  HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, UINT uidLastCmd, UINT uFlags )
{
    // If the flags include CMF_DEFAULTONLY then we shouldn't do anything.

    if ( uFlags & CMF_DEFAULTONLY )
        return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );

    // First, create and populate a submenu.

    HMENU hSubmenu = CreatePopupMenu();
    UINT uID = uidFirstCmd;

    InsertMenu ( hSubmenu, 0, MF_BYPOSITION, uID++, _T("&Notepad") );
    InsertMenu ( hSubmenu, 1, MF_BYPOSITION, uID++, _T("&Internet Explorer") );

    // Insert the submenu into the ctx menu provided by Explorer.

    MENUITEMINFO mii = { sizeof(MENUITEMINFO) };

    mii.fMask = MIIM_SUBMENU | MIIM_STRING | MIIM_ID;
    mii.wID = uID++;
    mii.hSubMenu = hSubmenu;
    mii.dwTypeData = _T("C&P Open With");

    InsertMenuItem ( hmenu, uMenuIndex, TRUE, &mii );

    return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, uID - uidFirstCmd );
}

The return value this time is 3, which tells Explorer that we inserted 3 items (the two Open With items, and the submenu itself). Since all 3 items have IDs, Explorer can delete them all and completely remove our items from its menu.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Michael Dunn


Sitebuilder, Mvp
Michael lives in sunny Sunnyvale, California, and is still trying to break the habit (as a Buffy fan) of typing "Sunnydale." He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1995, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Zabersoft's flagship product is a media grabber and download manager with the whacky name PimpFish. Mike is now a developer (using WTL~!) at VMware.

He also enjoys his hobbies of playing pinball, bike riding, photography, and the occasional PlayStation or MAME game (current favorite: Out Run). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

Mike has been a VC MVP since April, 2005.
Occupation: Software Developer (Senior)
Company: VMware
Location: United States United States

Other popular Shell and IE programming articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 55 (Total in Forum: 55) (Refresh)FirstPrevNext
GeneralConverting this project to Dev C++ - not possible?memberGoran _15:51 11 Jul '08  
GeneralCan we make it dynamic?memberGoran _16:33 5 Jul '08  
QuestionHelp needed - Submenus using c#memberSharath Ambati0:55 2 Apr '08  
GeneralRe: Help needed - Submenus using c#mvpMichael Dunn14:38 3 Apr '08  
Generaldefault context menu loading issue in vista...........memberaditya_mnit1:45 3 Mar '08  
GeneralRe: default context menu loading issue in vista...........mvpMichael Dunn17:16 16 Mar '08  
Generalhow to get the pressed item ?membercarabutnicolae12344:33 13 Dec '07  
QuestionMFT_OWNERDRAW but not reallymemberXygorn21:14 10 Nov '06  
AnswerRe: MFT_OWNERDRAW but not reallysitebuilderMichael Dunn14:47 12 Nov '06  
GeneralWhat about using MF_BYCOMMANDmemberhighdoe20:41 19 Mar '06  
GeneralRe: What about using MF_BYCOMMANDsitebuilderMichael Dunn12:07 22 Mar '06  
GeneralRe: What about using MF_BYCOMMANDmemberhighdoe7:35 23 Mar '06  
QuestionThere's one thing I can't understand ...memberAssarbad0:40 1 Jan '06  
AnswerRe: There's one thing I can't understand ...sitebuilderMichael Dunn9:28 1 Jan '06  
GeneralRe: There's one thing I can't understand ...memberAssarbad10:04 1 Jan '06  
GeneralStill duplicated Menumemberswim071015:03 27 Dec '05  
QuestionRe: Still duplicated Menu [modified]memberNvmkpk7:49 29 Aug '06  
AnswerRe: Still duplicated Menumembercrino9:29 3 Sep '06  
AnswerRe: Still duplicated Menumembertdinneen22:28 10 Apr '07  
GeneralRe: Still duplicated MenumemberMarx Chen16:25 13 Apr '07  
GeneralRe: Still duplicated Menumemberdalsegno1:40 18 Jun '07  
GeneralAdding command to the right click context menu in Windows XPmemberMr_Wilson_Wilson15:09 1 Dec '05  
GeneralRe: Adding command to the right click context menu in Windows XPsitebuilderMichael Dunn11:06 16 Dec '05  
GeneralVS 7.0memberHansBys23:30 11 Jan '05  
GeneralRe: VS 7.0sitebuilderMichael Dunn4:55 12 Jan '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Feb 2003
Editor: Chris Maunder
Copyright 2003 by Michael Dunn
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project