Using Hot Toolbar Buttons

.
If you are interested in more articles written by Kirk Stowell, you can click here to visit his home page.

If you wanted to add hot toolbar buttons like the ones seen in internet explorer, it is pretty straight forward. This approach for will work for both Visual C++ 5 and 6, however you may want to read up on the enhancements to the toolbar class for VC 6.0.

After your toolbar has been created in CMainFrame::OnCreate(), you will need to add the following code, this is assuming that you have two bitmap resources already created IDB_TOOLBAR_COLD for normal, and IDB_TOOLBAR_HOT for hot buttons:


	// Set up hot bar image lists.
	CImageList	imageList;
	CBitmap		bitmap;

	// Create and set the normal toolbar image list.
	bitmap.LoadBitmap(IDB_TOOLBAR_COLD);
	imageList.Create(21, 20, ILC_COLORDDB|ILC_MASK, 13, 1);
	imageList.Add(&bitmap, RGB(255,0,255));
	m_hotToolBar.SendMessage(TB_SETIMAGELIST, 0, (LPARAM)imageList.m_hImageList);
	imageList.Detach();
	bitmap.Detach();

	// Create and set the hot toolbar image list.
	bitmap.LoadBitmap(IDB_TOOLBAR_HOT);
	imageList.Create(21, 20, ILC_COLORDDB|ILC_MASK, 13, 1);
	imageList.Add(&bitmap, RGB(255,0,255));
	m_hotToolBar.SendMessage(TB_SETHOTIMAGELIST, 0, (LPARAM)imageList.m_hImageList);
	imageList.Detach();
	bitmap.Detach();

The send message portion of the code has been wrapped into a CToolBarCtrl() function call for Visual C++ 6. If you will notice, I have used 24 bit color images with the toolbar shown here, so you are not limited to just 16 colors.

Download demo project - (30 KB)

Last updated December 21, 1998



Comments

  • Good solution but I could not get tooltips

    Posted by Legacy on 11/17/2003 12:00am

    Originally posted by: Sergei

    Good solution!

    But I could not get tooltips over
    buttons. Please help...

    Sincerely
    Sergei

    Reply
  • Bitmaps with different sizes

    Posted by Legacy on 10/24/2002 12:00am

    Originally posted by: Philippe Preget

    And how could we add buttons with bitmaps of different widths?

    Reply
  • perfect!!

    Posted by Legacy on 10/17/2002 12:00am

    Originally posted by: ltqin

    good job ,3xs!!!

    Reply
  • in case some toolbar buttons gets enabled/disabled at run time in your app.

    Posted by Legacy on 06/27/2002 12:00am

    Originally posted by: Davinder Pal Singh

    First of all I wish to thank Mr.Kirk Stowell for his article on hot toolbar buttons.After reading his article i was able to implement the same in my project.Thanx once again. I hope to read more articles of yours in future.

    Just a little addition to his article.

    In case in your application as a result of some operations some of your toolbar buttons gets enabled and disabled,to handle that u can have one more bitmap and using
    TB_SETDISABLEDIMAGELIST in your SendMessage(). See code given below.

    After your toolbar has been created in CMainFrame::OnCreate(), you will need to add the following code, this is assuming that you have three bitmap resources already created IDB_TOOLBAR_COLD for normal,IDB_TOOLBAR_HOT for hot buttons and IDB_TOOLBAR_DISABLE for disabled buttons:


    // Set up hot bar image lists.
    CImageList imageList;
    CBitmap bitmap;

    // Create and set the normal toolbar image list.
    bitmap.LoadBitmap(IDB_TOOLBAR_COLD);
    imageList.Create(21, 20, ILC_COLORDDB|ILC_MASK, 13, 1);
    imageList.Add(&bitmap, RGB(255,0,255));
    m_hotToolBar.SendMessage(TB_SETIMAGELIST, 0, (LPARAM)imageList.m_hImageList);
    imageList.Detach();
    bitmap.Detach();

    // Create and set the hot toolbar image list.
    bitmap.LoadBitmap(IDB_TOOLBAR_HOT);
    imageList.Create(21, 20, ILC_COLORDDB|ILC_MASK, 13, 1);
    imageList.Add(&bitmap, RGB(255,0,255));
    m_hotToolBar.SendMessage(TB_SETHOTIMAGELIST, 0, (LPARAM)imageList.m_hImageList);
    imageList.Detach();
    bitmap.Detach();

    // Create and set the disabled toolbar image list.
    bitmap.LoadBitmap(IDB_TOOLBAR_DISABLE);
    imageList.Create(21, 20, ILC_COLORDDB|ILC_MASK, 13, 1);
    imageList.Add(&bitmap, RGB(255,0,255));
    m_hotToolBar.SendMessage(TB_SETDISABLEDIMAGELIST, 0, (LPARAM)imageList.m_hImageList);
    imageList.Detach();
    bitmap.Detach();

    I have simply added little code.All the credit goes to Kirk
    Stowell


    Reply
  • It works.. but.....How to set the background to highlighted color on SetCheck(TRUE).

    Posted by Legacy on 09/17/2001 12:00am

    Originally posted by: Ramesh

    I'm mapping the ON_UPDATE_COMMAND_UI message in which i'm changing the state to SetCheck(TRUE), but the background color is not getting highlighted. How to do it.?

    Reply
  • Little bug and solution

    Posted by Legacy on 11/24/2000 12:00am

    Originally posted by: Serrand

    Good code but a resource leak exist due to your ImageList.
    
    

    Solution:

    1) make your ImageList member of the CMainFrame derived class.

    2) call DeleteImageList( ) when the application ends.

    Reply
  • Other soultion would be....

    Posted by Legacy on 01/08/2000 12:00am

    Originally posted by: Peter Hahm

    There is an other solution for this Problem using some Long Style constants for the toolbar:

    int CMyToolBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    ��� if (CToolBar::OnCreate(lpCreateStruct) == -1)
    ������� return -1;
    ��� DWORD dwStyle = ::GetWindowLong(
    CToolBar::GetToolBarCtrl().m_hWnd , GWL_STYLE);
    ��� dwStyle |=
    ( TBSTYLE_FLAT /*| TBSTYLE_TRANSPARENT |
    TBSTYLE_HOTTRACK*/);
    ��� // some cool stuff is still missing
    ��� // Note that some Versions of Devstudio do declare
    ��� // TBSTYLE_TRANSPARENT and TBSTYLE_HOTTRACK and
    // others not
    ��� ::SetWindowLong(CToolBar::GetToolBarCtrl().m_hWnd,
    GWL_STYLE , dwStyle);
    ��� return 0;
    }

    This overriding of CToolBar::OnCreate results in the same behaviour.

    Hope this Helps

    Peter Hahm
    PHahm@compuserve.com

    Reply
  • THE ARTICLE AND SOURCE CODE ARE OUTDATED, PLEASE FOLLOW THIS LINK FOR THE LATEST UPDATE

    Posted by Legacy on 09/12/1999 12:00am

    Originally posted by: Kirk Stowell

    Hello,

    Until I am able to update this article, as an alternative, please visit this link http://www.codejock.com/ for the latest updates. My email address has changed also, please respond to kstowell@codejock.com...

    Thanks,
    Kirk Stowell

    Reply
  • Does it require IE4 or new Comctl32.dll?

    Posted by Legacy on 08/17/1999 12:00am

    Originally posted by: nguyen

    I just wonder if this method required IE4 or new Comctl32.dll? I'm using win95 (version 1).Thank!

    Reply
  • how to enable/disable hot-toolbar-buttons?

    Posted by Legacy on 06/22/1999 12:00am

    Originally posted by: panza

    SetState(ID_WHATEVER, TBSTATE_PRESSED); gives it the
    look of a pressed button, but the picture is the cold one
    and I can still press the button...

    I can set it with EnableButton(FALSE); but the whole
    button goes away... Is there a special STATE to disable
    a hot-toolbar-button?

    I want it to be visible, but looking either pressed (with
    the hot picture) or disabled (like a button without a
    valid ID).

    Thanks a lot...

    Reply
  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The latest release of SugarCRM's flagship product gives users new tools to build extraordinary customer relationships. Read an in-depth analysis of SugarCRM's enhanced ability to help companies execute their customer-facing initiatives from Ovum, a leading technology research firm.

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds