Adding a drop arrow to a toolbar button | CodeGuru

Adding a drop arrow to a toolbar button

. 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 a drop menu 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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Dec 21, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More


.
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 a drop menu 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.

First off, after your toolbar has been created in CMainFrame::OnCreate(), you will
need to make a call to the following method:

	m_wndToolBar.GetToolBarCtrl().SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);

This will enable your toolbar to handle drop arrows. The next thing you will need to do,
is to actually add the drop arrow to your desired button. This will be done via the SetButtonStyle()
method:

	DWORD dwStyle = m_wndToolBar.GetButtonStyle(m_wndToolBar.CommandToIndex(ID_FILE_OPEN));
	dwStyle |= TBSTYLE_DROPDOWN;
	m_wndToolBar.SetButtonStyle(m_wndToolBar.CommandToIndex(ID_FILE_OPEN), dwStyle);


Now, you will need to add a message handler for the drop arrow, as well a menu to the
application resources. Assuming you already know how to create a menu, ( if not, click
on the resource tab, select the resource name ie: MyApp Resources, then right click. Select
insert, then select Menu, then press the New button )
and assuming that the resource id
for our menu is IDR_MENU1, add the following code to CMainFrame's
message map:


BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	...
	ON_NOTIFY(TBN_DROPDOWN, AFX_IDW_TOOLBAR, OnToolbarDropDown)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


Add the following method to CMainFrame's .cpp file:

void CMainFrame::OnToolbarDropDown(NMTOOLBAR* pnmtb, LRESULT *plr)
{
	CWnd *pWnd;
	UINT nID;

	// Switch on button command id's.
	switch (pnmtb->iItem)
	{
	case ID_FILE_OPEN:
		pWnd = &m_wndToolBar;
		nID  = IDR_MENU1;
		break;
	default:
		return;
	}

	// load and display popup menu
	CMenu menu;
	menu.LoadMenu(nID);
	CMenu* pPopup = menu.GetSubMenu(0);
	ASSERT(pPopup);

	CRect rc;
	pWnd->SendMessage(TB_GETRECT, pnmtb->iItem, (LPARAM)&rc);
	pWnd->ClientToScreen(&rc);

	pPopup->TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL,
		rc.left, rc.bottom, this, &rc);
}


Then add the following to CMainFrame's .h file:


	//{{AFX_MSG(CMainFrame)
	...
	afx_msg void OnToolbarDropDown(NMTOOLBAR* pnmh, LRESULT* plRes);
	//}}AFX_MSG

Download demo project - (28 KB)

Last updated December 21, 1998

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.