Click here to Skip to main content
15,886,777 members
Articles / Programming Languages / C++

IE Drop-Down Button

Rate me:
Please Sign up or sign in to vote.
1.85/5 (8 votes)
8 Nov 20073 min read 36.8K   304   16  
similar skype IE toolbar button, with drop-down arraw, support IE6 & IE7(multi-tab)
#include "StdAfx.h"
#include "IEToolbarWnd.h"

CIEToolbarWnd::CIEToolbarWnd(void)
{
    m_isIE7 = FALSE;
}

CIEToolbarWnd::~CIEToolbarWnd(void)
{
}

LRESULT CIEToolbarWnd::OnCheckButton(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
     if (m_isIE7) 
    {
        return DefWindowProc(uMsg, wParam, lParam);
    }

    if (m_ParentWnd.GetButtonID() == (int)wParam)
    {
        BrowserWndInfo *pInfo = m_ParentWnd.GetBrowserWndInfo();

        if (pInfo != NULL) 
        {
            if (pInfo->nButtonState == TBSTATE_CHECKED) 
            {
                lParam = MAKELONG(TRUE, 0);
            }
            else 
            {
                lParam = MAKELONG(FALSE, 0);
            }
        } 
    } 
}

LRESULT CIEToolbarWnd::OnAddString(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
    LRESULT lResult = DefWindowProc(uMsg, wParam, lParam);
    
    if (m_isIE7)
    {
        return lResult;
    }

    if ((int)wParam == 0) 
    {
        LPWSTR strButtonText = (LPWSTR)lParam;
        if (_tcscmp(BUTTON_NAME, strButtonText) == 0) 
        {
            m_buttonStrIndex = lResult;
        }
    }
    return lResult;
}


LRESULT CIEToolbarWnd::OnAddButton(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
     LRESULT lResult = DefWindowProc(uMsg, wParam, lParam);

    if (m_isIE7)  
    {
        return lResult;
    }

    int nButtons = (int)wParam;
    LPTBBUTTON pButtons = (LPTBBUTTON)lParam;
    for (int i = 0; i < nButtons; i++) 
    {
        if (pButtons[i].iString == m_buttonStrIndex)
        {
            m_ParentWnd.SetButtonID(pButtons[i].idCommand);
            
            m_ParentWnd.NotifySetButtonInfo(pButtons[i].idCommand);
        }
    }
    return lResult;
}

LRESULT CIEToolbarWnd::OnSetButtonInfo(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
    int iID = (int) wParam;
    LPTBBUTTONINFO lptbbi = (LPTBBUTTONINFO)lParam;

    int ButtonID = m_ParentWnd.GetButtonID();
    if (ButtonID == -1) 
    {
	    if (lptbbi->pszText != NULL && wcscmp(lptbbi->pszText, L"DropButton") == 0) 
        {
            ButtonID = iID;
            m_ParentWnd.SetButtonID(ButtonID);
        } 
    } 
    if (ButtonID == iID) 
    {
        lptbbi->dwMask = lptbbi->dwMask | TBIF_STYLE |  TBSTYLE_CHECK| TBIF_STATE;
        lptbbi->fsStyle = lptbbi->fsStyle|BTNS_DROPDOWN |BTNS_CHECK;
        BrowserWndInfo* pInfo = m_ParentWnd.GetBrowserWndInfo();
        if (pInfo != NULL && pInfo->nButtonState == TBSTATE_CHECKED) 
        {
            lptbbi->dwMask = lptbbi->dwMask | TBIF_STYLE |  TBSTYLE_CHECK| TBIF_STATE;
            lptbbi->fsStyle = lptbbi->fsStyle|BTNS_DROPDOWN |BTNS_CHECK;
            lptbbi->fsState = TBSTATE_ENABLED|TBSTATE_CHECKED;
        }
        else 
        {
            lptbbi->dwMask = lptbbi->dwMask | TBIF_STYLE |  TBSTYLE_CHECK| TBIF_STATE;
            lptbbi->fsStyle = lptbbi->fsStyle|BTNS_DROPDOWN |BTNS_CHECK;
            lptbbi->fsState = TBSTATE_ENABLED;
        }
    }
    return DefWindowProc(uMsg, wParam, lParam);
}

void CIEToolbarWnd::SubclassToolbar(IWebBrowser2 *pWB)
{
    if (pWB == NULL)
    {
        return;
    }

    long hWnd = NULL;
    pWB->get_HWND(&hWnd);

    int nViewCount = m_arrayBrowserWndInfo.GetSize(); 

    BrowserWndInfo *pBrowserWndInfo = new BrowserWndInfo;

    if (pBrowserWndInfo == NULL)
    {
        return;
    }
    pBrowserWndInfo->pWebBrowser = pWB;
    pBrowserWndInfo->nButtonState = 0;
    pBrowserWndInfo->hBrowserWnd = (HWND)hWnd;
    m_arrayBrowserWndInfo.Add(pBrowserWndInfo);

    if (m_ParentWnd.GetBrowserWndInfo() == NULL) 
    {
        m_ParentWnd.SetBrowserWndInfo(pBrowserWndInfo);
    }

    if (nViewCount > 0) 
    {
        return; 
    }

    HWND hToolbarWnd = NULL;

    if (m_isIE7) 
    {
         ie7SubclassToolbar((HWND)hWnd); 
    } 
    else 
    {
        normalSubclassToolbar((HWND)hWnd);
    }
}

void CIEToolbarWnd::UnsubclassToolbar(IWebBrowser2 *pWebBrowser)
{
    BrowserWndInfo* pBrowseWndInfo = findBrowserWndInfo(pWebBrowser);

    if (pBrowseWndInfo != NULL) 
    {
        m_arrayBrowserWndInfo.Remove(pBrowseWndInfo);
        m_ParentWnd.SetBrowserWndInfo(NULL);
    }

    if (m_arrayBrowserWndInfo.GetSize() == 0) 
    {
        if (m_hWnd) 
        {
            UnsubclassWindow();
        }
        if (m_ParentWnd.m_hWnd)
        {
            m_ParentWnd.UnsubclassWindow();
        }
    }

    if (pBrowseWndInfo != NULL)
    {
        delete pBrowseWndInfo;
    }
}

void CIEToolbarWnd::SetCurrentBrowserWnd(IWebBrowser2 *pWebBrowser)
{
    if (pWebBrowser == NULL) 
    {
        return;
    }
    BrowserWndInfo* pBrowseWndInfo = findBrowserWndInfo(pWebBrowser);
    m_ParentWnd.SetBrowserWndInfo(pBrowseWndInfo);
}


void CIEToolbarWnd::IsIE7(BOOL isIE7)
{
    m_isIE7 = isIE7;
}

BOOL CIEToolbarWnd::ie7SubclassToolbar(HWND hBrowserWnd)
{
    HWND hCmdbarWnd = FindWindowEx(hBrowserWnd, NULL, L"CommandBarClass", NULL);
    if (hCmdbarWnd) 
    {
        HWND hRebarWnd = FindWindowEx(hCmdbarWnd, NULL, L"ReBarWindow32", NULL);

        HWND hToolbarWnd = ::GetDlgItem(hRebarWnd, 0x0000A000);

        if (hToolbarWnd != NULL) 
        {
            m_ParentWnd.SetToolbarWnd(hToolbarWnd);

            SubclassWindow(hToolbarWnd); 

            m_ParentWnd.SubclassWindow(hCmdbarWnd); 

            return TRUE;
        }
    }
    return FALSE;
}

BOOL CIEToolbarWnd::normalSubclassToolbar(HWND hBrowserWnd)
{
    HWND hCmdbarWnd = ::GetDlgItem(hBrowserWnd, 0x0000A005);

    if (hCmdbarWnd) 
    {
        HWND hRebarWnd = FindWindowEx(hCmdbarWnd, NULL, L"ReBarWindow32", NULL);

        if (hRebarWnd) 
        {
            HWND hToolbarWnd = ::GetDlgItem(hRebarWnd, 0x0000A000);
            if (hToolbarWnd != NULL) 
            {
                m_ParentWnd.SetToolbarWnd(hToolbarWnd);
                SubclassWindow(hToolbarWnd); 
                m_ParentWnd.SubclassWindow(hCmdbarWnd); 
                return TRUE;
            }
        }
    }
    return FALSE;
}

BrowserWndInfo* CIEToolbarWnd::findBrowserWndInfo(IWebBrowser2 *pWebBrowser)
{
    BrowserWndInfo* pBrowseWndInfo = NULL;
    int size = m_arrayBrowserWndInfo.GetSize();

    for (int i = 0; i < size; i++) 
    {
        pBrowseWndInfo = m_arrayBrowserWndInfo[i];

        if (pBrowseWndInfo != NULL && pBrowseWndInfo->pWebBrowser == pWebBrowser) 
        {
            return pBrowseWndInfo;
        }
    }
    return NULL;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions