Click here to Skip to main content
Click here to Skip to main content

Adding a drop arrow to a toolbar button

By , 15 Dec 1999
 
  • Download demo project - 28 Kb
  • Sample Image - toolbar_droparrow.gif

    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

    DWORD dwExStyle = TBSTYLE_EX_DRAWDDARROWS;
    m_wndToolBar.GetToolBarCtrl().SendMessage(TB_SETEXTENDEDSTYLE, 0, (LPARAM)dwExStyle);
    

    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
    

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Kirk Stowell
    CEO Codejock Software
    United States United States
    Member
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    QuestionDropdown arrow on the bottom of the buttonmemberBruno Scopigno28 Mar '12 - 5:04 
    QuestionImplementing windows explorer 'views' type of menumemberSharanyaMahi5 Dec '11 - 20:20 
    Generaldropdown toolbar instead of menumembermimosa22 Jun '10 - 11:21 
    Hi
     
    this is nice. However, I'd like to have a toolbar to pop instead of a menu. How would I do that? I couldn't find any article on this. Thanks for guiding me.
    Mat Suspicious | :suss:

    QuestionHow do I add menu item function and update function for multiple and dynamic menu itemsmemberakira3226 Oct '08 - 23:17 
    GeneralI used the vs2008 to compile the demo source codememberakira3226 Oct '08 - 16:03 
    AnswerRe: I used the vs2008 to compile the demo source codememberakira3226 Oct '08 - 17:21 
    GeneralBUG: CToolBar, drop-down, XP theme, and Internet ExplorermemberDamir Valiulin3 May '06 - 19:47 
    GeneralMoving Arrow to Left of buttonmemberHamid Reza Mohammadi2 Dec '05 - 5:12 
    Questionboth arrows and not?sussnoaa28 Jul '05 - 14:04 
    Generalreplacing button iconsmemberprrem24 Feb '05 - 2:52 
    GeneralDrop Down Button in Internet Explorer Standard ToolbarmemberAtif Goheer10 Jan '04 - 9:26 
    GeneralRe: Drop Down Button in Internet Explorer Standard ToolbarmemberBill SerGio, The Infomercial King4 Aug '04 - 4:29 
    GeneralRe: Drop Down Button in Internet Explorer Standard ToolbarmemberAlexey Petuschak10 Sep '05 - 4:18 
    GeneralCannot click arrowmemberpinguin75126 Jul '03 - 20:01 
    GeneralRe: Cannot click arrowmemberTux9 Feb '04 - 15:26 
    GeneralRe: Cannot click arrowmemberrlaley18 Jan '05 - 22:34 
    GeneralRe: Cannot click arrowsussRummey13 Mar '05 - 7:04 
    GeneralRe: Cannot click arrowmemberKirk Stowell14 Mar '05 - 4:21 
    GeneralRe: Cannot click arrowmemberRummey14 Mar '05 - 5:35 
    GeneralRe: Cannot click arrowmemberKirk Stowell14 Mar '05 - 10:21 
    GeneralRe: Cannot click arrowmemberRummey14 Mar '05 - 10:55 
    GeneralRe: Cannot click arrowmemberKirk Stowell15 Mar '05 - 4:29 
    GeneralRe: Cannot click arrowmemberRummey15 Mar '05 - 4:53 
    GeneralRe: Cannot click arrowmemberKirk Stowell15 Mar '05 - 5:06 
    GeneralRe: Cannot click arrowmemberCodeHead2 Sep '05 - 8:50 
    AnswerRe: Cannot click arrowmemberKirk Stowell2 Sep '05 - 10:49 
    GeneralRe: Cannot click arrowmemberCodeHead6 Sep '05 - 2:52 
    QuestionHow can I do it in a docking toolbar?memberPaulaPaziani25 Jul '03 - 6:42 
    QuestionHow to do it in C#?memberCarl Mercier23 Feb '03 - 11:18 
    GeneralGood JobmemberOld Timer8 Jan '03 - 2:57 
    GeneralVC++.NET ProblemsussAnonymous12 Aug '02 - 13:28 
    GeneralRe: VC++.NET ProblemmemberKirk Stowell13 Aug '02 - 4:44 
    GeneralRe: VC++.NET Problemmemberhaoshenghan1 May '04 - 0:05 
    GeneralRe: VC++.NET ProblemmemberBausch23 May '06 - 23:17 
    GeneralRe: VC++.NET ProblemmemberAni4 Jul '06 - 18:51 
    GeneralRe: VC++.NET Problemmemberkarthikeyan nithiyanandavelu1 Jan '07 - 19:49 
    GeneralRe: VC++.NET Problemmemberdangero19 Jun '08 - 17:04 
    GeneralAbsolutely brilliantmemberDanPetitt9 Aug '02 - 10:08 
    GeneralRedo/Undo Dropdown WindowmemberAnonymous20 Jun '02 - 5:13 
    GeneralA very little additionmemberMorozov Alexey19 Feb '02 - 13:56 
    GeneralText Menusmemberswinefeaster28 Oct '01 - 12:29 
    GeneralDoing this in a CDialog windowsussJeremy Davis20 Mar '00 - 5:36 
    GeneralTrouble with doing this in a CDialogsussJeremy Davis21 Feb '00 - 3:29 
    GeneralBug in VC6 MFC with dropdown arrows in toolbarssussMartin Speiser21 Jan '00 - 3:55 
    GeneralBug in VC6 MFC with dropdown arrows in toolbarssussMartin Speiser21 Jan '00 - 3:55 
    GeneralBug in VC6 MFC with dropdown arrows in toolbarssussMartin Speiser21 Jan '00 - 3:55 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    Web04 | 2.6.130516.1 | Last Updated 16 Dec 1999
    Article Copyright 1999 by Kirk Stowell
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid