Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Iam confused about how to set an icon in a button placed in a tool bar. What i meant was, suppose i have tool bar with name of IDR_TOOLBAR1. i have "save" icon. i want set this save icon to this tool bar. how it is possible?
Posted
Updated 24-Feb-13 5:34am
v2

1 solution

You can use the bitmap for the WHOLE Toolbar, that is possible
(if you want to change the bitmap of one single button you need to change the whole bitmap as well):
- 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:
C++
CBitmap     m_bitmapToolbar;
CToolBar    m_Toolbar;
void CreateToolbar();

- Add a the following function to your dialog class:
C++
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()
 
Share this answer
 
v6
Comments
H.Brydon 24-Feb-13 21:25pm    
Great answer. +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900