Click here to Skip to main content
Click here to Skip to main content

Adding a drop-down menu to an IE Toolbar button

, 20 Sep 2006
Rate this:
Please Sign up or sign in to vote.
This article explains how to add a drop-down menu to a toolbar button of Internet Explorer.

TestExtension menu

Introduction

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>
"CLSID"="{1FBA04EE-3024-11D2-8F1F-0000F87ABD16}"
...

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)
    {
        if(!pUnkSite)
        {
            ATLTRACE(_T("SetSite(): pUnkSite is NULL\n"));
        }
        else
        {
            HRESULT hRes = S_OK;
            CComPtr<IServiceProvider> spSP;
            hRes = pUnkSite->QueryInterface(&spSP);
            if(SUCCEEDED(hRes) && spSP)
                hRes = spSP->QueryService(IID_IWebBrowserApp, 
                                          &m_pWebBrowser2);
        }
        
        m_spUnkSite = pUnkSite;
        m_bIsIe7 = FALSE;
    
        if(pUnkSite)
        {
            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, 
                                               (void**)&pHtmlDoc2);
                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 = 
                              pNavigator->get_appVersion(&bstrVersion);
                            if(SUCCEEDED(hRes))
                            {
                                CHAR szVersion[MAX_PATH];
                                memset(szVersion,0,MAX_PATH);
    
                                WideCharToMultiByte(CP_ACP, 0, 
                                          bstrVersion.m_str, 
                                          -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;
    m_pWebBrowser2->get_HWND(&nBrowser);
    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);
    ...
    
    if(hWnd)
    {
        TCHAR szClassName[MAX_PATH];
        while(hWnd)
        {
            memset(szClassName,0,MAX_PATH);
            GetClassName(hWnd, szClassName, MAX_PATH);
            if(_tcscmp(szClassName,_T("TabWindowClass"))==0)
            {
                // the active tab should be visible
                if(IsWindowVisible(hWnd))
                {
                    hWnd = GetWindow(hWnd, GW_CHILD);
                    while(hWnd)
                    {
                        memset(szClassName,0,MAX_PATH);
                        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;
                            break;
                        }
                        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;
    if(hWndToolBar)
    {
        ScreenToClient(hWndToolBar,&pt);
        int nButton = (int)::SendMessage(hWndToolBar, 
                       TB_HITTEST, 0, (LPARAM)&pt);
        if(nButton>0)
        {
            TBBUTTON pTBBtn;
            memset(&pTBBtn,0,sizeof(TBBUTTON));
            if(::SendMessage(hWndToolBar, TB_GETBUTTON, 
                             nButton, (LPARAM)&pTBBtn))
            {
                nIDCommand = pTBBtn.idCommand;
                RECT rcButton;
                if(::SendMessage(hWndToolBar,TB_GETRECT,nIDCommand,
                                (LPARAM)&rcButton))
                {
                    pt.x = rcButton.left;
                    pt.y = rcButton.bottom;
                    ClientToScreen(hWndToolBar,&pt);
    
                    RECT rcWorkArea;
                    SystemParametersInfo(SPI_GETWORKAREA,0,
                                        (LPVOID)&rcWorkArea,0);
                    if(rcWorkArea.right-pt.x<150)
                    {
                        bRightAlign = TRUE;
                        pt.x = rcButton.right;
                        pt.y = rcButton.bottom;
                        ClientToScreen(hWndToolBar,&pt);
                    }
                }
            }
        }
        else
        {
            GetCursorPos(&pt);
            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.

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

Share

About the Author

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

Comments and Discussions

 
GeneralIts not working PinmemberUtkal Ranjan7-Oct-09 1:09 
GeneralRe: Its not working Pinmemberandrej33@yandex.ru24-Dec-09 0:56 
GeneralIE8 support Pinmemberandrej33@yandex.ru7-Sep-09 9:28 
QuestionDoes this work for IE8/Vista ? PinmemberHappyFunBall13-Jul-09 14:50 
GeneralNeed Help Pinmemberjayshml19-Jan-09 0:22 
GeneralGreat work !! Pinmemberbishwajeet17-Sep-08 3:25 
GeneralI need help! Pinmemberpla12318-Aug-08 18:03 
GeneralNewbie PinmemberTahir Raza17-Jul-08 2:20 
QuestionDoes this makes difference for creating drop down Menu Item Pinmembervikrant kpr31-Aug-07 1:26 
QuestionAdding DropMenu to IE Button Pinmembervikrant kpr31-Aug-07 0:13 
Generalsame work but on ms outlook PinmemberParijat Chandra18-Jul-07 8:42 
GeneralGreat artical... but i need in c# [modified] Pinmembercinni18-Jul-07 4:33 
Questionhow to add a tool tip to sub menu Pinmembersumeetpk6-Jun-07 6:06 
GeneralFacing problem in registering the DLL Pinmembersumeetpk29-May-07 0:15 
GeneralRe: Facing problem in registering the DLL Pinmembersumeetpk31-May-07 4:51 
QuestionHow to add drop down arrow to IE7 Button Pinmemberrneela28-May-07 3:01 
QuestionHow to do this for Windows Explorer? Pinmemberyvz17-Jan-07 3:00 
AnswerRe: How to do this for Windows Explorer? PinmemberHarish_kumar31-Aug-09 22:07 
Generalinstalling PinmemberthebigDWK3-Jan-07 20:32 
GeneralNice arrticle but i need it in c# Pinmember101626-Dec-06 2:08 
GeneralGreat article... Pinmemberprogramvinod13-Dec-06 1:37 
GeneralRe: Great article... Pinmemberasdklasjdlaskj14-Dec-06 4:15 
GeneralIE7 PinmemberMurph Dogg7-Dec-06 5:27 
GeneralGood article!!! Pinmemberwwwwww21-mail28-Nov-06 22:06 
GeneralGood article! Pinmemberwwwwww21-mail28-Nov-06 22:04 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 20 Sep 2006
Article Copyright 2006 by Igor Tolmachev
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid