Click here to Skip to main content
Click here to Skip to main content
Go to top

Toolband (Toolbar for IE) sample using WTL

, 5 Oct 2002
Rate this:
Please Sign up or sign in to vote.
create your own toolband for Internet Explorer using WTL
<!-- Article image -->

Sample Image - toolband10.gif

<!-- Add the rest of your HTML here -->

Credits and Acknowlegements

This module is based on the ATL DeskBand Wizard article that was created by Erik Thompson. My thanks goes to him for producing a great wizard that saves you the nity grity of creating DeskBands.

Please note that this project does not use MFC, for all those MFC die hards that want to use MFC in their Tool Bands I suggest you down load the KKBar sample from microsofts MSDN site.

The KBBar Sample can be downloaded from here

Creating the ToolBand Module

You will need to install the ATL DeskBand Wizard in order to create ToolBands. Please follow the instruction in this article.

The best way to kick start a Tool band project is to use the ATL COM App Wizard. The COM Object needs to be in-proc therefore choose 'DLL' as the Server Type. The rest of the options can be kept in there default state. (Note this project does not use MFC, as the project is based on ATL and WTL)

Once you have a ATL COM Project, You can use the ATL DeskBand Wizard to create the Initial Tool Band.

Adding Support for WTL

You will require the WTL Libraries, these can be downloaded from the microsoft site

See Installation of WTL

The following header files needs to be added to stdafx.h file

atlapp.h
atlwin.h
atlctrls.h
atlmisc.h

Create the ToolBar

You can create your toolbar in the usual way via the resource editor, Once you have your toolbar you need to create the toolbar dynamically. The CBandToolBarCtrl is inherited from CToolBarCtrl and is used to create the toolbar dynamically.

DWORD dStyle = WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | 
               CCS_NODIVIDER | CCS_NORESIZE | CCS_NOPARENTALIGN | 
               TBSTYLE_TOOLTIPS | TBSTYLE_FLAT;
    
HWND hWnd = m_wndToolBar.CreateSimpleToolBarCtrl(hWndChild, IDR_TOOLBAR_TEST, 
                                                 FALSE, dStyle); 

This is done in the RegisterAndCreateWindow function that is called from SetSite method.

Message Reflection

The best way to handle the messages of the toolbar is to let the control handle its own messages via "Message Reflection". The best way to reflect the messages is to create an invisible control that acts as the parent for the toolbar. The invisible control will reflect the toolbar messages back to itself. See CBandToolBarReflectorCtrl class for the invisible control that will be used.

The following code shows the parent child relationship that is used to achieve Message Reflection.

BOOL CToolBandObj::RegisterAndCreateWindow()
{
    RECT rectClientParent;
    ::GetClientRect(m_hWndParent, &rectClientParent);

    // We need to create an Invisible Child Window using the Parent Window, 
    // this will also be used to reflect Command
    // messages from the rebar
    HWND hWndChild = m_wndInvisibleChildWnd.Create(m_hWndParent, <BR>                                                   rectClientParent, 
                                                   NULL, WS_CHILD);
    
    // Now we can create the Tool Bar, using the Invisible Child
    DWORD dStyle = WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | 
                   CCS_NODIVIDER | CCS_NORESIZE | CCS_NOPARENTALIGN | 
                   TBSTYLE_TOOLTIPS | TBSTYLE_FLAT;
    
    HWND hWnd = m_wndToolBar.CreateSimpleToolBarCtrl(hWndChild, <BR>                                                     IDR_TOOLBAR_TEST, 
                                                     FALSE, dStyle); 
    m_wndToolBar.SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);          
    m_wndToolBar.m_ctlBandEdit.m_pBand = this;
    return ::IsWindow(m_wndToolBar.m_hWnd);
}

The following Macros are used to identify the reflected messages from the ordinary messages. e.g WM_COMMAND reflected comes back as OCM_COMMAND.

#define OCM_COMMAND_CODE_HANDLER(code, func) \
if(uMsg == OCM_COMMAND && code == HIWORD(wParam)) \
{ \
    bHandled = TRUE; \
    lResult = func(HIWORD(wParam), LOWORD(wParam), (HWND)lParam, bHandled); \
    if(bHandled) \
    return TRUE; \
}

#define OCM_COMMAND_ID_HANDLER(id, func) \
if(uMsg == OCM_COMMAND && id == LOWORD(wParam)) \
{ \
    bHandled = TRUE; \
    lResult = func(HIWORD(wParam), LOWORD(wParam), (HWND)lParam, bHandled); \
    if(bHandled) \
    return TRUE; \
}

#define OCM_NOTIFY_CODE_HANDLER(cd, func) \
if(uMsg == OCM_NOTIFY && cd == ((LPNMHDR)lParam)->code) \
{ \
    bHandled = TRUE; \
    lResult = func((int)wParam, (LPNMHDR)lParam, bHandled); \
    if(bHandled) \
    return TRUE; \
}

Browser Navigation

In order to Navigate on the browser you need to instantiate the IWebBrowser2 COM Object. This is usually done on the SetSite Method e.g.

IServiceProviderPtr pServiceProvider = pUnkSite;
if (_Module.m_pWebBrowser)
    _Module.m_pWebBrowser = NULL; 

if(FAILED(pServiceProvider->QueryService(SID_SWebBrowserApp, IID_IWebBrowser, 
                                            (void**)&_Module.m_pWebBrowser)))
return E_FAIL;

Once you have the COM Object Instantiated, you can move to your URL using the navigate method

	
_variant_t varURL = _bstr_t("www.codeproject.com"); 
_variant_t varEmpty;
_Module.m_pWebBrowser->Navigate2(&varURL, &varEmpty, &varEmpty, 
                                    &varEmpty, &varEmpty);

Drag and Drop Edit and ComboBox Control

The CBandEditCtrl class is inherited from a WTL CEdit control that has drag and drop facility. i.e. you can drag text from the browser straight to the CEdit Control.

The CBandComboBoxCtrl class is inherited from a WTL CComboBox control that has drag and drop facility. i.e. you can drag text from the browser straight to the CComboBox Control.

Drag and Drop URL - toolband2.gif

The Edit control in this sample module allows you to drag a URL from Explorer or any other Dragable enabled container. Once you have dropped the URL the toolband will go to that site.

Configurable Toolbar Button Styles

The CBandToolBarCtrl class allows you to have the follwoing button styles on the toolbar

  • Image and Text on the right
  • Image and Text on the bottom
  • Image only

Text on Right - toolband3.gif

No Text - toolband4.gif

Text Under - toolband5.gif

Pop-up Menu Tracking

A pop up menu is used to get to the configuration options

Popup Menu Tracking - toolband6.gif

ToolTips

This has been taken from MSDN to explain why you need to handle the tooltips on the toolbar your self.

Tool tips are automatically displayed for buttons and other controls contained in a parent window derived from CFrameWnd. This is because CFrameWnd has a default handler for the TTN_GETDISPINFO notification, which handles TTN_NEEDTEXT notifications from tool tip controls associated with controls.

However, this default handler is not called when the TTN_NEEDTEXT notification is sent from a tool tip control associated with a control in a window that is not a CFrameWnd, such as a control on a dialog box or a form view. Therefore, it is necessary for you to provide a handler function for the TTN_NEEDTEXT notification message in order to display tool tips for child controls.

This can be easily done in WTL by using the following Message Handler in the overriden ToolBar Control

NOTIFY_CODE_HANDLER(TTN_NEEDTEXT, OnToolbarNeedText)

The following is the code that loads the tool tips from the resources and sets the tool tip text.

LRESULT CBandToolBarCtrl::OnToolbarNeedText(int /*idCtrl*/, LPNMHDR pnmh, BOOL& 
                                            bHandled)	
{
    CString sToolTip;    
    //-- make sure this 1is not a seperator
    if (idCtrl != 0) 
    {
        if (!sToolTip.LoadString(idCtrl))
        {
            bHandled = FALSE;
            return 0
        }
    }

    LPNMTTDISPINFO pttdi = reinterpret_cast<LPNMTTDISPINFO>
	
    (pnmh);pttdi->lpszText = MAKEINTRESOURCE(idCtrl);
    pttdi->hinst = _Module.GetResourceInstance();
    pttdi->uFlags = TTF_DI_SETITEM;

    //-- message processed
    return 0;
}

Update the Status Bar

You can update the status bar using the browser method put_StatusText, this method can be used typically in the WM_MENUSELECT event

The following is the code that loads up the menu text from the resources and displays it on the browser status bar.

LRESULT CBandToolBarCtrl::OnMenuSelect(UINT /*uMsg*/, <BR>                                       WPARAM wParam, LPARAM lParam, 
                                       BOOL& bHandled) 
{
    WORD nID = LOWORD(wParam);
    WORD wFlags = HIWORD(wParam);
    
    //-- make sure this is not a seperator
    CString sStatusBarDesc;

    if ( !(wFlags & MF_POPUP) )
    {
        if (nID != 0) 
        {
            if (!sStatusBarDesc.LoadString(nID))
            {
	        bHandled = FALSE;
                return 0;
            }

            int nPos = sStatusBarDesc.Find(_T("\n"));
            if (nPos != -1)
            {
                sStatusBarDesc = 
                sStatusBarDesc.Left(nPos+1);
                _Module.m_pWebBrowser->
                put_StatusText(_bstr_t(sStatusBarDesc));
                return 0;
            }
        }
    }

    return 0;
}

How to add a Chevron to your toolband

In order to add a chevron to your toolband you need to add the DBIMF_USECHEVRON flags to your GetBandInfo method in the DBIM_MODEFLAGS mask.

 
... if(pdbi->dwMask == DBIM_MODEFLAGS) 
    { 
        //AddChevron
        pdbi->dwModeFlags = DBIMF_NORMAL | DBIMF_VARIABLEHEIGHT |    
                               DBIMF_USECHEVRON | DBIMF_BREAK;
    }
...

This basically add the ability to show the chevron, if you want to see it appear on your toolband, then you must make sure the pdbi->ptMinSize.x value is less then your pdbi->ptActual.x value (Again this values can be set in the GetBandInfo method.

In order to handle the events of the button in the chevron menu, you must subclass the rebar control which is hosting your toolbar. The following steps have been used to sublass the rebar control

1. Find the rebar control - This can be achieved by getting the browsers window handle and searching for all its child windows, until you find the rebar control.

2. Once you have found the rebar control you can simply subclass it using an ATL CContainedWindow.

    BOOL CBandToolBarCtrl::SetBandRebar()
    { 
        HWND hWnd(NULL);
        _Module.m_pWebBrowser->
        get_HWND((long*)&hWnd);
	
	if (hWnd == NULL) 
	    return FALSE;

	m_ctlRebar.m_hWnd = FindRebar(hWnd);
	if (m_ctlRebar.m_hWnd == NULL) 
	    return FALSE;

	m_RebarContainer.SubclassWindow(m_ctlRebar);

	return TRUE;
    }

Once you have subclass the window, the events should reach the toolbar class.

Append to the Browser Context Menu

Sample Image - toolband11.gif

I tried to duplicate the google and codeproject toolband right mouse browser context menu searches without any luck, until I looked at the binary resources, I found that you can extend the internet explorer menu by adding the option in the registry. This can easily be achieved by adding an entry in your .rgs file.

HKCU
{
    NoRemove Software
    {
        NoRemove Microsoft
        {
            NoRemove 'Internet Explorer'
            {
                NoRemove MenuExt
                {
                    ForceRemove '&Sample Toolband Serach' = s <BR>                                            'res://%MODULE%/MENUSEARCH.HTM'
                    {
                        val Contexts = b '10'
                    }
                }
            }
        }
    }
}

Note the value of the registry item (a html script that exist in the resources of the module).

see MENUSERACH.HTM under HTML in the module resource to see what the script is doing

History

  • Version 1 [23 Aug 2001] - Created
  • Version 2 [04 Sep 2001] - Added Tooltip code
  • Version 2.1 [23 Sep 2001] - Parent Child WebBrowser Fix
  • Version 2.2 [13 Jan 2002] - slight rewrite, Fix Internal Compiler Error problems, Unicode compatible, Status Bar sample, Chevron Sample, tooltip changes
  • Version 2.3 [15 Jan 2002] - Bug Fixes, Link Error Fixes, Show how to disable, enable buttons, Right mouse context menu
  • Version 2.4 [29 Jan 2002] - Actual Parent Child Browser Fix, v2.1 fix did not work
  • Version 2.5 [29 Oct 2002] - Bug Fixs, ComboBox Sample added, chevron width calculation

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

Rashid Thadha
Web Developer
United Kingdom United Kingdom
Was made redundant in early 2003 after 10 years in computer programming, since then started my own business (selling computer books on the net)
www.pricecutbook.co.uk
 

Comments and Discussions

 
GeneralBuild error PinmemberMember 41711078-Sep-09 21:23 
GeneralYahoo toolbar collapse and expand feature Pinmemberamol sarmalkar23-Aug-09 6:08 
QuestionHow to Add Events on chevron buttons in IE Toolbar Pinmemberam 200922-Jul-09 0:17 
QuestionNeed to add progress bar... Pinmembersunil.n.cs27-May-09 20:16 
Questioncan you help me ?can you give a more detailed process ? Pinmemberzying2008200819-Apr-09 23:56 
Questionhow do i make the toolbar band non movable ?? PinmemberAnantheswarG23-Mar-09 19:20 
QuestionHot Key for the IE Band Pinmemberlal001227-Nov-08 0:15 
QuestionCrashes! Pinmemberstarschen6-Nov-08 22:41 
QuestionRefined Testcase Pinmembershahshirish13-Aug-08 0:08 
QuestionTestCase Pinmembershahshirish12-Aug-08 23:57 
GeneralCBandEditCtrl can't receive WM_MOVE message. Pinmemberc0ffee198227-Jan-08 14:45 
GeneralToolbar is not coming in IE7 Pinmemberrindam9-Aug-07 1:10 
GeneralNeed help - web browser navigation fails PinmemberRealTreasure18-Jul-07 20:36 
Questionhow can i add the kbbar toolbar into FireFox [modified] PinmemberinvinJerry29-May-07 22:40 
AnswerRe: how can i add the kbbar toolbar into FireFox Pinmemberjamesmhall20-Jul-07 6:13 
Generalcan't receive the TTN_NEEDTEXT msg under win32 release Minisize Pinmemberhlqokey29-May-07 22:02 
Generalshow/hide toolbar Pinmember101614-May-07 6:20 
GeneralDetect web page change Pinmemberseralav17-Jan-07 22:35 
GeneralRe: Detect web page change Pinmemberelie00221-Jun-07 6:21 
GeneralRe: Detect web page change Pinmemberankurtechno31-Aug-07 0:05 
GeneralRe: Detect web page change PinmemberThe loon28-Jul-09 4:58 
QuestionHow to decide which programe called me? Pinmemberguostong9-Jan-07 4:34 
GeneralError Pinmembermessages21-Dec-06 21:15 
GeneralRe: Error Pinmemberdigitally_urs24-Dec-06 19:50 
GeneralRe: Error Pinmembermessages26-Dec-06 20:37 
Questionhow to show icons with menu items? Pinmemberdigitally_urs19-Dec-06 8:27 
QuestionXP stle for IE Toolbar PinmemberHarishDixit8-Nov-06 23:59 
QuestionImages in Chevron Window Pinmembersramnani11-Oct-06 7:48 
AnswerRe: Images in Chevron Window Pinmembersramnani27-Feb-07 4:03 
GeneralRe: Images in Chevron Window PinmemberGSL36-Nov-07 12:38 
GeneralRe: Images in Chevron Window Pinmemberlol024-Aug-09 2:09 
GeneralDllRegisterServer Fail Pinmemberjunhee25-Sep-06 15:43 
QuestionMENUSEARCH.HTM? PinmemberMegaChan20-Jun-06 5:20 
QuestionXP theme, XP style support? PinmemberJason Liu6-May-06 22:07 
AnswerRe: XP theme, XP style support? PinmemberCremains7-May-06 7:43 
QuestionRe: XP theme, XP style support? Pinmembercode_discuss27-Apr-07 4:54 
AnswerRe: XP theme, XP style support? Pinmembercact13-Jul-07 6:59 
GeneralRe: XP theme, XP style support? Pinmembercanido1-Oct-07 21:39 
AnswerRe: XP theme, XP style support? PinmemberHeartman18-Jan-08 21:27 
AnswerRe: XP theme, XP style support? Pinmembercanido2-Oct-07 2:20 
GeneralIE crash on exit fixed Pinmemberjoebarthib3-May-06 23:38 
GeneralRe: IE crash on exit fixed Pinmembersaurabh tuteja28-Sep-06 1:59 
GeneralRe: IE crash on exit fixed Pinmembersinaie5-Dec-06 6:10 
GeneralRe: IE crash on exit fixed Pinmembermateuscb30-Aug-07 7:43 
Generalcan't compile on VS .Net 2003 : atlthunk.lib not found Pinmemberjoebarthib3-May-06 22:34 
GeneralRe: can't compile on VS .Net 2003 : atlthunk.lib not found Pinmemberjoebarthib3-May-06 23:17 
Questionie7 support PinmemberCremains17-Apr-06 15:30 
QuestionRe: ie7 support Pinmemberatesz22-Oct-06 4:44 
AnswerRe: ie7 support PinmemberSlavko7827-Feb-07 20:51 
GeneralRe: ie7 support PinmemberSlavko7827-Feb-07 20:54 

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 | Mobile
Web04 | 2.8.140926.1 | Last Updated 6 Oct 2002
Article Copyright 2001 by Rashid Thadha
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid