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

Creating Flyout Toolbars

By , 31 Oct 2001
 

Sample Image - FOToolBar.jpg

Introduction

This article shows how to create a Flyout Toolbar. Flyout toolbars are used, for example, in 3D Studio & AutoCAD.

The idea is to let the user choose a sub-option of a more 'general' action that can be performed. For example, if we're writing a 2D graphics editor and we have a 'Draw Rectangle' option, we would probably have a button on the toolbar showing a rectangle. But the question is what kind of rectangle? A filled rectangle? Just the border? Or maybe both?

One solution to the problem is to create another 'flyout' toolbar showing all the 'Draw Rectangle' sub-options, which will pop-up next to the 'Draw Rectangle' button on the toolbar if the user clicks that button a little bit longer. If the user has chosen a different sub-option from the flyout toolbar, the button on the main toolbar is automatically updated.

The class CFOToolBar does just that. It's derived from CToolBar, and has an additional member function called addFlyout(), which takes the zero-based index of the button on the toolbar, and the resource ID of the flyout toolbar to be attached to that button.

Usage

In order to use CFOToolBar, just perform the following steps:

1. Create another toolbar with the resource editor that will contain all the sub options ( in this case, the toolbar that contains the different kinds of rectangles Flyout Toolbar. Let's say that it's resource ID is IDR_RECTANGLE_TOOLBAR )

2. Derive your main toolbar from CFOToolBar (or just use it directly instead of deriving from it)

In MainFrm.h (in class CMainFrame)

...
protected:  // control bar embedded members
    CFOToolBar    m_wndToolBar;

3. Call the addFlyout() member function of your main toolbar object to attach the flyout toolbar to the desired button.

In MainFrm.cpp (in OnCreate())

...
// add the flyout to the first button on the toolbar
m_wndToolBar.addFlyout(0, IDR_RECTANGLE_TOOLBAR);	

m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
...

4. Add FOToolBar.cpp & FOToolBar.h to your project.

That's it.

Now if you click the first button on the toolbar a little bit longer, you'll see the 'rectangle' flyout oolbar pop up.

How it works

By overriding the ON_WM_LBUTTONDOWN() message of the main toolbar, we can do two things:

1. Check if a the user clicked on a button to which we previously attached a flyout toolbar.

2. If so, initiate the timer for X milliseconds. In the timer function, we create the flyout toolbar and display it in the right place. The flyout toolbar is also CToolBar-derived. All we had to do is simply remove the caption and float it.

There are a few problems though when displaying the flyout toolbar. It doesn't get any mouse messages because the main toolbar gets them. That's the reason we also had to override ON_WM_MOUSEMOVE() to detect if a flyout toolbar is displayed, and if so, delegate that message to it.

We also override ON_WM_LBUTTONUP() in which we kill the timer and update the new button from the flyout if neccessary.

A strange side effect happens if the user changes the application (by clicking Ctrl+Alt) while a flyout is on, and unclicks the mouse in the other application. When getting back, neither the main toolbar nor the flyout toolbar receive the ON_WM_LBUTTONUP() message, so you'll see that flyout is still displayed until you actually click on one of its buttons.

These are the main guildelines of the implementation. For more specific details please looks at the code, and feel free to e-mail me with questions or suggestions.

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

Y. Hirsh
Israel Israel
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

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionToolTipsmembersc2018411 Apr '07 - 1:27 
I am also struggling with is "Tooltips" on each of the items on the Flyout toolbar. Can you help me to deal with this problem? Thank you!
GeneralThank you! [modified]memberhenky_arektc9725 May '06 - 16:52 
Thank you so much for your contribution!
Anybody who faces difficulties when implementing this article on MFC 4.2, please contact meBig Grin | :-D Because there are several tricks to be implemented in order to get this work.
 
Hky
GeneralRe: Thank you!memberYogesh P. Dhakad23 Aug '06 - 21:52 
Hi,
 
I have implemented the flyout toolbar almost similar to that appearing in the AutoCAD application. Thanks to the Author of this nice contribution.
 
My application design suits well with Flyouts coming horizontal as I place my main toolbar on the Left side of the app so that it appears vertical.
 
The only thing I am struggling with is "Tooltips" on each of the items on the Flyout toolbar. Did you manage to get it?

Generalruntime errormemberzizou0427 Jun '04 - 15:13 
hello;
thank you for this source, it s very suitable.
i tried to add it but i still have this following message;
runtime error
this application has requested the runtime to terminate it in an unusual way.
please contact the application's support team for more information;
 
have anybody any idea?
thanks for help
 
i did it again
Generalfloating toolbarmemberted_yu7 Jun '04 - 14:48 
Hi,
I tried to make my toolbar dockable on any side of frame window.
But the following doesn't achieve what I want:
if (!m_wndToolBar.CreateEx(this) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
Any idea ?
Thanks
GeneralResource RequiredmemberRukhiya30 May '04 - 21:00 
Hi OMG | :OMG:
 
Your code cannot be compiled because you have not supplied the resource files with your project.
Please update the project with the complete files required at the earliest.
 

 
bye n thanx
Rukhiya

GeneralResources RequiredmemberSharjith26 Jun '03 - 5:37 
Hi,
Your code cannot be compiled because you have not supplied the resource files with your projectConfused | :confused: . Please update the project with the complete files required.
Thanks!
 
N. Sharjith
QuestionVC5 ??memberagnes9114 Jan '03 - 5:19 
Anyone did the same for Visual C5 ??
GeneralHeadache research studentmemberMond26 Nov '02 - 22:35 
Hi, your flyout toolbar is exactly what my supervisor needs. But unlucky me I was just thrown this project to do by myself, I really hope u can still help this poor miserable student. I downloaded your source code but I realise that when i compile it, it doesnt seem to work. My problem is that I still cant do out the flyout toolbar. As I am doing a continual project from someother students, a interface is already done up. I followed your instructions and copied your FOTOOLBAR.ccp and .h files into my project. But the diffeernce is that in my project, I have a main toolbar and another palette bar. I need it on my palette bar instead. But in the end there seem to be a clash with your flyout bar coding. I really dont understand. Can u offer some help here? Frown | :( Dead | X| Confused | :confused: Cry | :((
GeneralRe: Headache research studentsussY.H7 Dec '02 - 3:23 
Hi,
Unfortunately, I haven't supported this code for a very long time since I started university (and it takes up a lot of my time, naturally). As far as I remember, I may have forgotten to add the .rc file to the project, so the example may not properly work (or even compile) because it doesn't have the resources. Try doing it with your own resources. I don't think there should be any problems adding the flyout toolbar to any other toolbar in your application, but maybe I didn't think everything over.
Another problem is, that I moved out recently, so I currently don't have an internet connection (I'm in an internet cafe now.Blush | :O ). Still, I'm planning on improving this code, and maybe at some point I'll write an upgrade article and put it here. Let me know if everything worked out okay, or you still have problems. I'll check this site once in a while.
Sorry for not being able to be too much help.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 1 Nov 2001
Article Copyright 2001 by Y. Hirsh
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid