Click here to Skip to main content
15,312,257 members
Articles / Programming Languages / C++
Posted 20 Sep 2006


61 bookmarked

Adding a drop-down menu to an IE Toolbar button

Rate me:
Please Sign up or sign in to vote.
4.74/5 (15 votes)
20 Sep 20063 min read
This article explains how to add a drop-down menu to a toolbar button of Internet Explorer.

TestExtension menu


This article explains how to add a drop-down menu to a toolbar button of Internet Explorer.

The procedure for adding a standard button to the IE Toolbar is well described at MSDN.

Microsoft provides only one CLSID for the button extension: {1FBA04EE-3024-11d2-8F1F-0000F87ABD16}. So, using this CLSID, we can't add a button different from a standard style, and trying to set a BTNS_DROPDOWN or BTNS_WHOLEDROPDOWN style will be ignored:

HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Extensions\<Your GUID>

Some of the Internet Explorer built-in toolbar buttons (such as "Back" or "Forward"), and some buttons of applications from Microsoft placed on the Internet Explorer toolbar (MS Office, for example), have drop-down functionality.

Unfortunately, MSDN does not provide any documentation regarding the addition of drop-down buttons (the author hasn’t found any information for a long time) for the Internet Explorer toolbar. Probably, such a procedure requires deeper integration with Internet Explorer than the usual registry workaround.

This article solution implements a drop-down behavior of the Internet Explorer toolbar button.

General Steps

Since the IE7 tabbed browsing concept has been released, the Internet Explorer internal window structure has been changed considerably. The following illustration from Spy++ shows the structure of internal windows of Internet Explorer 6 and 7.

IE6 internal window structure

IE7 internal window structure

The first step is to determine the version of Internet Explorer.

  1. Detecting the IE version.

    The best place for this is the IObjectWithSite::SetSite() method, which will be called during the creation and termination of the object we are implementing here (meaning our test extension). While creating, the site object passed at SetSite() is non-zero, and allows us to get the pointer to the Web browser instance.

    STDMETHODIMP CMasterObject::SetSite(IUnknown *pUnkSite)
            ATLTRACE(_T("SetSite(): pUnkSite is NULL\n"));
            HRESULT hRes = S_OK;
            CComPtr<IServiceProvider> spSP;
            hRes = pUnkSite->QueryInterface(&spSP);
            if(SUCCEEDED(hRes) && spSP)
                hRes = spSP->QueryService(IID_IWebBrowserApp, 
        m_spUnkSite = pUnkSite;
        m_bIsIe7 = FALSE;
            CComPtr<IDispatch>        pDocDisp;
            CComQIPtr<IHTMLDocument2> pHtmlDoc2;
            CComQIPtr<IHTMLWindow2>   pWindow;
            CComQIPtr<IOmNavigator>   pNavigator;
            HRESULT hRes = m_pWebBrowser2->get_Document(&pDocDisp);
            if(SUCCEEDED(hRes) && pDocDisp)
                hRes = pDocDisp->QueryInterface(IID_IHTMLDocument2, 
                if(SUCCEEDED(hRes) && pHtmlDoc2)
                    hRes = pHtmlDoc2->get_parentWindow(&pWindow);
                    if(SUCCEEDED(hRes) && pWindow)
                        hRes = pWindow->get_navigator(&pNavigator);
                        if(SUCCEEDED(hRes) && pNavigator)
                            CComBSTR bstrVersion;
                            hRes = 
                                CHAR szVersion[MAX_PATH];
                                WideCharToMultiByte(CP_ACP, 0, 
                                          -1, szVersion, 
                                          MAX_PATH, NULL, NULL);
                                if(strstr(szVersion, "MSIE 7.") != 0)
                                    m_bIsIe7 = TRUE;
        return S_OK;
  2. Finding a handle of the Web browser window.

    This handle is required for the TrackPopupMenu function that we should use later. We can obtain it very easily for versions 5 and 6:

    long nBrowser = 0;
    HWND hWndParent = (HWND)nBrowser;

    But since beta1 of version 7, we have to find the active tab first and the corresponding browser window next:

    IE7 internal window structure

    HWND hWnd = GetWindow(hWndParent, GW_CHILD);
        TCHAR szClassName[MAX_PATH];
            GetClassName(hWnd, szClassName, MAX_PATH);
                // the active tab should be visible
                    hWnd = GetWindow(hWnd, GW_CHILD);
                        GetClassName(hWnd, szClassName, MAX_PATH);
                        if(_tcscmp(szClassName,_T("Shell DocObject View"))==0)
                            hWnd = FindWindowEx(hWnd, NULL, 
                                   _T("Internet Explorer_Server"), NULL);
                            if(hWnd) hWndIe7ActiveTab = hWnd;
                        hWnd = GetWindow(hWnd, GW_HWNDNEXT);
            hWnd = GetWindow(hWnd, GW_HWNDNEXT);
    if(hWndIe7ActiveTab) hWndMenuParent = hWndIe7ActiveTab;

    Probably, when IE7 will be fully released, Microsoft will present a new IWebBrowser interface, and we can then determine which tab is active at the moment. But, currently, MSDN does not provide any such function. So, we have to enumerate all internal "TabWindowClass" windows and assume that the active tab should be visible.

  3. Finding a handle of the IE Toolbar window.

    We can obtain it very easily for all IE versions as shown below:

    hWndToolBar = WindowFromPoint(pt);
  4. Finding the button on-screen position.

    In this step, we should determine if the button was clicked by the mouse or selected from the chevron’s menu.

    IE7 chevron menu

    And, if the button was clicked, we should calculate the button’s rectangle for correct menu positioning:

    int nIDCommand = -1;
    BOOL bRightAlign = FALSE;
        int nButton = (int)::SendMessage(hWndToolBar, 
                       TB_HITTEST, 0, (LPARAM)&pt);
            TBBUTTON pTBBtn;
            if(::SendMessage(hWndToolBar, TB_GETBUTTON, 
                             nButton, (LPARAM)&pTBBtn))
                nIDCommand = pTBBtn.idCommand;
                RECT rcButton;
                    pt.x = rcButton.left;
                    pt.y = rcButton.bottom;
                    RECT rcWorkArea;
                        bRightAlign = TRUE;
                        pt.x = rcButton.right;
                        pt.y = rcButton.bottom;
            bIsChevron = TRUE;
  5. The last step. Display the button as pushed, and pop up the menu.
    // draw pressed button
    if(nIDCommand!=-1 && !bIsChevron)
        ::SendMessage(hWndToolBar, TB_PRESSBUTTON, 
                      nIDCommand,  MAKELPARAM(1,0));
    // popup the menu
    int nCommand = TrackPopupMenu(hMenuTrackPopup, nFlags, 
                   pt.x, pt.y, 0, hWndMenuParent, 0);
    // release the button
    if(nIDCommand!=-1 && !bIsChevron)
        ::SendMessage(hWndToolBar, TB_PRESSBUTTON, 
                      nIDCommand,  MAKELPARAM(0,0));
    switch (nCommand)
        case ID_ITEM1:

This example does not create a natural drop-down button (missing a drop-down arrow, or a separate section), but can be used as a simple and quick solution to extend the functionality of a simple IE Toolbar button.

Anyway, I hope others find this code useful. Please feel free to report errors, issues, or requests.


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

Igor Tolmachev
Software Developer (Senior)
Ukraine Ukraine
No Biography provided

Comments and Discussions

GeneralIts not working Pin
Utkal Ranjan7-Oct-09 0:09
MemberUtkal Ranjan7-Oct-09 0:09 
GeneralRe: Its not working Pin
andrej33@yandex.ru23-Dec-09 23:56
Memberandrej33@yandex.ru23-Dec-09 23:56 
GeneralIE8 support Pin
andrej33@yandex.ru7-Sep-09 8:28
Memberandrej33@yandex.ru7-Sep-09 8:28 
QuestionDoes this work for IE8/Vista ? Pin
HappyFunBall13-Jul-09 13:50
MemberHappyFunBall13-Jul-09 13:50 
GeneralNeed Help Pin
jayshml18-Jan-09 23:22
Memberjayshml18-Jan-09 23:22 
GeneralGreat work !! Pin
bishwajeet17-Sep-08 2:25
Memberbishwajeet17-Sep-08 2:25 
GeneralI need help! Pin
PPTV18-Aug-08 17:03
MemberPPTV18-Aug-08 17:03 
GeneralNewbie Pin
Tahir Raza17-Jul-08 1:20
MemberTahir Raza17-Jul-08 1:20 
QuestionDoes this makes difference for creating drop down Menu Item Pin
vikrant kpr31-Aug-07 0:26
Membervikrant kpr31-Aug-07 0:26 
QuestionAdding DropMenu to IE Button Pin
vikrant kpr30-Aug-07 23:13
Membervikrant kpr30-Aug-07 23:13 
Generalsame work but on ms outlook Pin
Parijat Chandra18-Jul-07 7:42
MemberParijat Chandra18-Jul-07 7:42 
GeneralGreat artical... but i need in c# [modified] Pin
Cinni18-Jul-07 3:33
MemberCinni18-Jul-07 3:33 
Questionhow to add a tool tip to sub menu Pin
sumeetpk6-Jun-07 5:06
Membersumeetpk6-Jun-07 5:06 
GeneralFacing problem in registering the DLL Pin
sumeetpk28-May-07 23:15
Membersumeetpk28-May-07 23:15 
GeneralRe: Facing problem in registering the DLL Pin
sumeetpk31-May-07 3:51
Membersumeetpk31-May-07 3:51 
QuestionHow to add drop down arrow to IE7 Button Pin
rneela28-May-07 2:01
Memberrneela28-May-07 2:01 
QuestionHow to do this for Windows Explorer? Pin
yvz17-Jan-07 2:00
Memberyvz17-Jan-07 2:00 
AnswerRe: How to do this for Windows Explorer? Pin
Harish_kumar31-Aug-09 21:07
MemberHarish_kumar31-Aug-09 21:07 
Generalinstalling Pin
thebigDWK3-Jan-07 19:32
MemberthebigDWK3-Jan-07 19:32 
GeneralNice arrticle but i need it in c# Pin
rixwan26-Dec-06 1:08
Memberrixwan26-Dec-06 1:08 
GeneralGreat article... Pin
programvinod13-Dec-06 0:37
Memberprogramvinod13-Dec-06 0:37 
GeneralRe: Great article... Pin
asdklasjdlaskj14-Dec-06 3:15
Memberasdklasjdlaskj14-Dec-06 3:15 
GeneralIE7 Pin
Murph Dogg7-Dec-06 4:27
MemberMurph Dogg7-Dec-06 4:27 
GeneralGood article!!! Pin
wwwwww21-mail28-Nov-06 21:06
Memberwwwwww21-mail28-Nov-06 21:06 
GeneralGood article! Pin
wwwwww21-mail28-Nov-06 21:04
Memberwwwwww21-mail28-Nov-06 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.