Click here to Skip to main content
Licence 
First Posted 27 Apr 2000
Views 132,431
Bookmarked 37 times

WTL Tool Bar Drop-Down Extension

By | 26 Jul 2000 | Article
An article on extending the tool bar control using the Window Template Library
  • Download demo project - 58 Kb
  • Download source files - 11 Kb
  • Sample Image

    Introduction

    I needed a toolbar with drop down arrows so that I could place some pop-up menus on them. Initially I tried handling the notification messages from within the parent window. This became a huge mess and was just plain ugly looking. So I decided instead to create my own custom toolbar control. The process is quite simple so I'll get right to the code:

    How it works

    1. We needed to retrieve the notification messages from the parent window. Now, we don't just want to go and reflect all of the notification messages since we don't want to have to handle all of them. So we create a contained window, with which we can selectively chose which notification messages were going to handle.

    CContainedWindow m_wndParent;
    
    //...
    
    CToolBarCtrlExImpl() : 
    	m_wndParent(this, 1), 
    	m_pDropDownButtons(NULL),
    	m_bImagesVisible(false),
    	m_bAnimate(false),		
    	m_clrMask(RGB(192, 192, 192))
    {
    	//...
    }
    
    //...
    
    LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
    {
    	//...
    		
    	// Subclass parent window
    	CWindow wndParent = GetParent();
    	CWindow wndTopLevelParent = wndParent.GetTopLevelParent();
    	m_wndParent.SubclassWindow(wndTopLevelParent);		
    		
    	//...
    }
    

    2. We've now got a copy of the parent window and are receiving all of its messages. Next we need to define in the message map which messages were going to process.

    BEGIN_MSG_MAP(CToolBarCtrlExImpl)
    	//...
    ALT_MSG_MAP(1) // Parent window messages	
    	NOTIFY_CODE_HANDLER(TBN_DROPDOWN, OnParentDropDown)
    	NOTIFY_CODE_HANDLER(TBN_HOTITEMCHANGE, OnParentHotItemChange)
    END_MSG_MAP()
    

    Notice that were using ALT_MSG_MAP(1). ATL supports multiple message maps, so all messages that belong to the parent window are now been pumped through here. By the way, the parent window still receives all these messages, we're just taking a peek at them and using the ones we need.

    The following code is the current message handlers in the toolbar class:

    LRESULT OnParentDropDown(int /*idCtrl*/, LPNMHDR pnmh, BOOL& bHandled)
    {
    	LPNMTOOLBAR lpnmtb = (LPNMTOOLBAR)pnmh;	
    	
    	// Check if this is from us
    	if (pnmh->hwndFrom != m_hWnd)
    	{
    		bHandled = FALSE;
    		return TBDDRET_NODEFAULT;
    	}	
    	
    	_DropDownButton* pb = FindDropDownButton(lpnmtb->iItem);
    	
    	RECT rc;
    	GetRect(pb->uIdButton, &rc);
    	ClientToScreen(&rc);		
    	
    	if (pb && pb->uIdMenu)
    		if (DoDropDownMenu(pb->uIdMenu, &rc)) <- See History for comments
    			return TBDDRET_DEFAULT;
    	
    	return TBDDRET_NODEFAULT;
    }
    
    // Do not hot track if application in background
    LRESULT OnParentHotItemChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& bHandled)
    {
    	LPNMTBHOTITEM lpnmhi = (LPNMTBHOTITEM)pnmh;
    	
    	// Check if this is from us
    	if (pnmh->hwndFrom != m_hWnd)
    	{
    		bHandled = FALSE;
    		return 0;
    	}		
    	
    	DWORD dwProcessID;
    	::GetWindowThreadProcessId(::GetActiveWindow(), &dwProcessID);		
    	if ((!m_wndParent.IsWindowEnabled() || ::GetCurrentProcessId() != dwProcessID))
    		return 1;
    	else
    	{
    		bHandled = FALSE;
    		return 0;
    	}
    }
    

    Updates

    I just finished adding bitmaps to the menu, so that all menu items that would usually display a bitmap, will.

    How to use

    In MainFrm.h declare a CToolBarCtrlEx and in the OnCreate handler initialize it like the below example.

    class CMainFrame : ...
    {
    	//...
    	
    	CToolBarCtrlEx m_wndToolBar;
    	
    	//...
    	
    	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
    	{
    		//...
    	
    		// HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);
    		HWND hWndToolBar = m_wndToolBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_TOOLBAR_PANE_STYLE);
    		m_wndToolBar.LoadToolBar(IDR_MAINFRAME); // Load the toolbar
    		m_wndToolBar.LoadMenuImages(IDR_MAINFRAME); // Load all the menu images were going to need
    		m_wndToolBar.AddDropDownButton(ID_FILE_NEW, IDR_MAINFRAME); // Add some drop-down buttons
    		m_wndToolBar.AddDropDownButton(ID_FILE_OPEN, IDR_POPUP_OPEN);
    		m_wndToolBar.AddDropDownButton(ID_EDIT_PASTE, IDR_POPUP_PASTE);	
    
    		//...					
    
    	}
    
    	//...
    
    };
    

    Thanks to Paul DiLascia for the code to add drop-down menus to toolbar buttons. (MSJ - 1997)

    That's it!.

    History

    Version 1.1

    • Revised the drop down method and made it a virtual function so that it can be overridden for custom handling.
    • Added bitmaps to the menu items.

    Version 1.0

    • Added drop down menus to the drop-down buttons.

    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

    Ben Burnett

    Student
    University of Lethbridge
    Canada Canada

    Member

    Ben was born in New Zealand and grew up in Mexico; however, he has lived in Canada for most of his adult life. He has traveled to a few places in Canada so far: British Columbia, Saskatchewan, Manitoba, Ontario and Nova Scotia, but has only ever lived in Alberta — all over Alberta. From Lethbridge to Calgary to Fort McMurray: he has seen most of what the province has to offer. He has also left the comfort of his—at the time—home country and gone abroad. In recent history he has been in China, New Zealand and the US (where he now lives); next year (2009) he plans to travel to Central and South America.
     
    He recently (2007) completed a degree at the University of Lethbridge, in Computer Science, and currently works for the University of Wisconsin-Madison in the Computer Sciences Department as a Systems Programmer for the Condor Research Project[^].
     
    Ben has been known to enjoy reading, watching movies, playing console games, and learning everything he can about computers. And, more importantly, with regards to his character: does not usually speaks about himself in the third person.

    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

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    Questionhow can i add one bitmap or icon in the dropdown menu item Pinmembernanda kumar nidhin21:23 25 Aug '05  
    QuestionDid you ever free GDI and User's resources ? PinmemberTutankhamen7716:33 17 Oct '03  
    Questionin win Xp only with 4bpp displays? Pinmemberrobspychala1:55 5 May '03  
    AnswerRe: in win Xp only with 4bpp displays? Pinmemberrobspychala13:42 1 Jun '03  
    GeneralDynamically add ticks to dropped down menu PinmemberAngus Comber0:58 31 Mar '03  
    QuestionFine work + ??? PinsussArmo4:25 25 Aug '02  
    GeneralDoesn't compiles under VS.NET with WTL7 PinmemberMagomed G. Abdurakhmanov22:12 22 May '02  
    GeneralRe: Doesn't compiles under VS.NET with WTL7 Pinmemberrobspychala11:58 27 Apr '03  
    GeneralCompile errors (int __thiscall CToolBarCtrlExImpl...) PinmemberValeria Costa1:39 12 Oct '01  
    GeneralRe: Compile errors (int __thiscall CToolBarCtrlExImpl...) PinmemberBen Burnett5:29 14 Oct '01  
    GeneralRe: Compile errors (int __thiscall CToolBarCtrlExImpl...) PinmemberValeria Costa21:40 14 Oct '01  
    GeneralRe: Compile errors (int __thiscall CToolBarCtrlExImpl...) PinmemberBen Burnett14:24 15 Oct '01  
    QuestionBug ? PinmemberMickeM2:22 19 Jul '01  
    GeneralTBSTYLE_CHECK PinmemberNiemiets0:58 4 Jun '01  
    GeneralToolbar with text... PinmemberAmit Dey8:56 22 May '01  
    GeneralRe: Toolbar with text... PinmemberBen Burnett9:35 22 May '01  
    GeneralRe: Toolbar with text... PinmemberAmit Dey14:05 22 May '01  
    GeneralRe: Toolbar with text... PinmemberBen Burnett16:49 22 May '01  
    Generalmissing resources PinsussMike Junkin10:21 3 May '00  

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

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web01 | 2.5.120528.1 | Last Updated 27 Jul 2000
    Article Copyright 2000 by Ben Burnett
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid