Dave's Brain

Browse - programming tips - win32 enable and disable a toolbar button

Date: 2009apr9
Language: C/C++
Platform: win32

Q.  How do I enable/disable a button in a toolbar?

A.  Here are 2 function that do that:

BOOL EnableToolbarButtonByIndex(CToolBar *pToolbar, const int iIndex, const BOOL bEnabled)
{
	UINT		nNewStyle;

	if (pToolbar == NULL) return FALSE;

	nNewStyle = pToolbar->GetButtonStyle(iIndex) & ~TBBS_DISABLED;
	if (!bEnabled)
	{
		nNewStyle |= TBBS_DISABLED;
		// If a button is currently pressed and then is disabled
		// COMCTL32.DLL does not unpress the button, even after the mouse
		// button goes up! We work around this bug by forcing TBBS_PRESSED
		// off when a button is disabled.
		// This code from http://www.codeguru.com/forum/archive/index.php/t-442010.html
		nNewStyle &= ~TBBS_PRESSED;
	}

	if (nNewStyle & TBBS_SEPARATOR) return FALSE;
	pToolbar->SetButtonStyle(iIndex, nNewStyle);
	return TRUE;
}

BOOL EnableToolbarButtonByCommand(CToolBar *pToolbar, const int iCommand, const BOOL bEnabled)
{
	int		iIndex;

	if (pToolbar == NULL) return FALSE;
	if ((iIndex = pToolbar->CommandToIndex(iCommand)) < 0) return FALSE;
	return EnableToolbarButtonByIndex(pToolbar, iIndex, bEnabled);
}

void ExampleUse()
{
	EnableToolbarButtonByCommand(&m_Toolbar, IDC_SAVE, m_bFileModified);
}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.
Advertisements: