Dave's Brain

Browse - Programming Tips - How do I make a toolbar with icons that are 256 colors and 32x32 (in size)

Date: 2008sep6
Framework: MFC
Language: C/C++

Q.  How do I make a toolbar with icons that are 256 colors and 32x32 (in size)
using MFC?

A.  By default toolbar icons in Windows are smaller and fewer colors.
So this is not totally striaght forward.  You can not use the Visual C++ 6
Toolbar Editor because it converts your icons to 16 colors.  You need
to call SetSizes() or your icons will be too small.

There are 3 ways you can do it:
- Use CToolBar			<- my preference
- Use CToolBarCtrl
- Use the toolbar with win32

This is what works for me:

- Make a 256 color bitmap that has all your icons lined (touching) up in a row
using your favourite paint program.  I suggest calling the file TOOLBAR.BMP.

- Import the bitmap as a resource.  Call it IDB_TOOLBAR.

- Add this to your dialog class's header:

CBitmap m_bitmapToolbar; CToolBar m_Toolbar; void CreateToolbar();
- Add a the following function to your dialog class:
static int iToolbarIconWidth = 32; static int iToolbarIconHeight = 32; void CMyDialog::CreateToolbar() { const UINT ids[] = { IDC_YOUR_ACTION1, IDC_YOUR_ACTION2, IDC_YOUR_ACTION3 }; m_Toolbar.CreateEx(this, TBSTYLE_FLAT | TBSTYLE_TOOLTIPS); m_bitmapToolbar.LoadBitmap(IDB_TOOLBAR); m_Toolbar.SetBitmap((HBITMAP)m_bitmapToolbar); m_Toolbar.SetButtons((const UINT *)&ids, sizeof(ids) / sizeof(ids[0])); m_Toolbar.ShowWindow(SW_SHOW); RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0); m_Toolbar.SetSizes(CSize(iToolbarIconWidth+7, iToolbarIconHeight+6), CSize(iToolbarIconWidth, iToolbarIconHeight)); m_Toolbar.SetHeight(0); }
- Call it from your dialog class's InitDialog()

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.