Have you seen a menu with no menu items in it, or just a single item popping
up? For example the standard Help menu created in VC has only the "About" item.
Some applications start with very basic menu panels. Why not show the dialogs
behind the single menu item instead of the menu item itself. There I come with
this new idea!
How it works
Making animated dialog menus is truly simple on Windows 98, NT5 and later,
the solution to the problem on older systems is far more complex. This is the
first version of my code, it surely has lots of "bugs". I hope it is useful to
The key is the
AnimateWindow function deeply hidden in the
Windows API. A good idea when using "preliminary" windows API functions is to
add these two lines before any headers get included in the application code:
#define WINVER 1000000
Heh, not really a good idea, but the only choice for Visual C++ 6 with
some service packs and Windows XP Pro SP1 (and yeah, it works!). You should
first check if the call to
AnimateWindow works without it.
WINVER is what VC defines in order to identify the version of windows you
are building for. It is predefined and the API declarations refer to it when
some compatibility info is required. While compiling you will be informed what
to do if you want a stable build.
I created a dialog based application which itself contains a child dialog
that serves as a menu bar (namely
CDummy). The menu bar dialog
handles user requests by popping up the dialogs. It also does draw the effects
and most of the cleanup/repaint operations. Simple buttons control the popup
operations. A little trick I used is that the IDs of the buttons are consecutive
1026, 1027, 1028, so I can address them by an offset value if necessary. It is
not implemented in the demo project.
The code that draws the menus is very straightforward (and slow), the best
explanation is just to read along the lines:
void CDummy::ShowDialogMenu(int number, int initial_control)
for(int e=rc.top+S_SHADE;e< rc.bottom;e++)
The shadow is completely owner-drawn, it paints directly on the desktop
window. This causes some problems with the cleanup paint job but it is fine for
shadows. I will soon update to popup windows and then it will make some sense.
However, if you are concerned about efficiency or paint safety you can simply do
CClientDC paint. The hide menu routine is simply a repaint.
Something important to say is that you can change the animation type by
modifying the flags in the
AnimateWindow. For example change
AW_ACTIVATE | AW_SLIDE | AW_VER_POSITIVE | AW_HOR_POSITIVE to
AW_ACTIVATE | AW_SLIDE | AW_HOR_POSITIVE or
AW_SLIDE | AW_VER_POSITIVE or something else as defined in MSDN under
Using the code in your apps
I didn't try to make a class to implement the functionality because I wanted
to keep the way user controls the dialogs custom. I am open to suggestions here.
Anyways I managed to separate the following steps, that should make using the
code in your applications easier:
- Create all dialogs you want to appear as menus. It is recommended to use
CDialog inherited dialogs. In properties select child, check
control, visible and set foreground. If you don’t specify any of these styles
the menu may not paint correctly. They all have to do with the z-order and the
parent management, not sure how exactly. You know where to dig for details, do
you not. Hey, don't forget to include the proper headers in your
source file, so you can create instances.
- Create a menu bar dialog, like
CDummy. This one is inherited
CDialog, but it could be any
CWnd based thing.
For the SDK fans - you can use direct handles with slight modifications to
eliminate the MFC device contexts. In your main window class declare an instance
CDummy and initialize it (in
example) as follows:
- Declare arrays to store the pointers to your dialogs and the current status
(in the demo project: listMenu and statMenu). Create instances of
your menu dialogs and put initialization code in the
handler, like that:
return TRUE; }
- The events causing the menus to show up or hide must occur in
CDummy. Put some buttons or whatever and process the events. In the
demo project I used simple buttons. This is how I handle one of my buttons’
ShowDialogMenu(int arg1,int arg2) shows the dialog on the
arg1 tells which dialog should be shown, for example
0 means the dialog referenced by
listMenu will be show.
arg2 is the ID of the control (static, button, edit box, check box,
whatever) next to which the dialog will be drawn. The function gets the client
rectangle of the control and draws the dialog just below the
WM_KILLFOCUS in the main window, one of the reasons all
menu dialogs must be child:
void CDmenuDlg::OnKillFocus(CWnd* pNewWnd)
WM_ACTIVATE in the main window, this kind of fixes the
"repaint after lost focus", but not always (see below):
void CDmenuDlg::OnActivate(UINT nState, CWnd* pWndOther,
CDialog::OnActivate(nState, pWndOther, bMinimized);
There are few bugs in Visual C++ or in my code.
AnimateWindow doesn’t do anything and returns
ERROR_SUCCESS. The demo project application will never do that, but
your applications may not be so lucky. I didn’t manage to find out what causes
the problem, but it applies to all preliminary functions.
- Clicking on a control in the main window doesn’t make the menu to disappear
and paints the control over it. To fix this you may use message reflection or
manual message forwarding to the main window, but you have to do that for all
controls on the main window. I am working on another solution.
- If a menu is active and the application loses focus, incorrect repaint will
occur. The last one is a mystery to me!
- On Windows 98 and ME the parallel tasks do not work together very well, I
mean they go slow because of the weak OS threading. Do not overload the app with
many active threads at a time.
I was hired by the pentagon to do this project. They needed some software to
attach to the control interface. I have not included the drivers you see, but I
may have cut/left some other code by an accident. Keep you eyes
Heh, kidding, but keep your eyes open, I may have forgotten to
mention about some little trick you need to make it work. This is a good place
to add “If this code works it’s written by Vladimir Ralev else I don’t know who
wrote it!”, like someone writing for the MSDN.
Thanks for all comments
Written by Vladimir Ralev <email@example.com>, 20 October 2003
Special thanks to PJ Naughter for his great work on
CSortedArray v1.06. I used (a
CTreeFileCtrl to illustrate how easily one can select a
file without having to popup a modal dialog.