Solved

How to have a menu item that can be clicked but also has a submenu

Posted on 2003-10-21
35
365 Views
Last Modified: 2013-11-20
Hi,
I need to be able to have items in a menu that can be clicked on but also have a submenu of items. (TrackPopUpMenu...)
The way I understand right now in the windows menus an item is either an item and can be clicked or a submenu and then only its items can be clicked.
What I need is something like in the Internet explorer favorites where if you double click on a folder it opens the folder (the folder of course has subitems) only I would rather it happened when you click and not double click.
Is there any way to detect a click or doubleclick (not OnSelect which happens when it is highlighted) on the menu item that has a submenu ?
I will accept any type of solution that will solve this problem meaning any technology or even custom compontents that give this functionality.
I already have a menu like this in DHTML but I would like to have the functionality in an active-x or something because I want the menu in an internet explorer toolbar that I have created.
Also I would much prefer if the menu behaved as the windows menus meaning I would not have to click on an arrow to open each level of the menu unless there is no other choice.
I am giving a lot of points for this so I expect a serious answer please that will help to solve my problem.
Thanks very much in advance.
0
Comment
Question by:ElishaKlein
  • 17
  • 15
  • 2
  • +1
35 Comments
 
LVL 12

Expert Comment

by:migel
ID: 9596908
Hi!
IMHO You need to write your own control that behave like menu but wit your own customization with submenus.
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9596941
Try this link

http://www.codeproject.com/docking/sizablerebar.asp - A set of classes to create the IE-style GUI

Good Luck
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9596975
Hi,
roshmon can you show me the part of this project that is relevant to what I want to do please ? Some sampe code ?
migel thanks for your response, sounds like a good direction for a start. Can you show more details of how I would do this ? Like sample code please ?
Thanks a lot.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9597000
I must be missing something important in the question.
I have an app with a list box.  I right click the list box to get a context menu.  That has various items one of which when the mouse goes onto it opens a sub menu (no click required).
All built in the resource editor - no coding to get the sub menu displayed.
I personally find this easier to use than a menu where one would have to click an item to get at further choices.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9597010
Is it the permanent display of the sub menu that is important for you?
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9597028
Hi Andy,
Thanks for your reply. I guess I didn't say this but the contents of the menu are built dynamically and needs to be refreshable at runtime while the menu is open. Also it is a hierarchial structure that can have many levels meaning a menu with a sub menu with submenus with items... but each submenu is also an item in itself.
Also if I am already going into the details - beyond each item and subitems each item (or submenu) will also need to be able to open a seperate menu (context menu) when it is right clicked so the user will be able to configure the actual structure for the main menu (like deleting an item for example) but i figured this is a seperate question and I plan to reward points for that seperately.
0
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 500 total points
ID: 9597040
Hi ElishaKlein,
      In default MFC Wizard MDI/SDI implementation, For menu they are using CToolbar. In IE, that uses CToolbarCtrl ("ToolBarWindow32" of common control).  
Using some subclass mechanism (i'm not sure, I have to check that code), they are subclassing the menu as window and accepting all window messages. And CToolbarCtrl  should be the parent of CMenu

     I' searching for the details, and I let you know If I get any relevent data ...

Rosh :)
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9597069
Thanks a lot Rosh.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9597108
I think I understand what you require.

How about
when launching your menu you create a list box (long enought to hold your items), maybe ownerdraw for displaying the menu items with sub menu's.  This list box is a sparate child window.
When the user selects a sub menu item then a further child list box is created and displayed.
ie. Just one class is required for this control.

When the 'menu' experiences a right mouse click then you can customise/resize appropriately.
when an item is selected work you way up the ownership chain (GetParent) until it meets the window that started everything off, send it a message saying what was selected and then delete these 'menu' windows.
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9597159
Andy,
If I understand correctly what you are saying each level of the menu would actually be a seperate list box window ?
How would the submenu open (second list box) when you highlighted an item with subitems ?
This solution sounds very complex because there are a lot of details I would need to work out including making the list box look like a menu and coordinating beteween all the levels of the menu.


0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9597164
Also how would the cursor pass from one listbox to another?
How would all this look ?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9597489
Yes - that allows a permanence in the display.
You can set the foreground/text colours by overriding the OnCtlColor function (that makes it look like a menu).  Using owner draw you can display the items containing a sub menu differently from the other items (favorites in IE).
One list box is a child of the other (and inversely a parent of the other).
You put the itmes into the list therefore you 'know' if the correct response to the mouse click / enter key is to perform an action or to display/hide the sub menu.  Equally well you can respond to the left/right arrow key to pass focus down to the child list box or back up to the parent list box.  The normal up/down arrow keys are handled by the default behaviour.

The items you put into the list would require something for the text to display, the event to perform (if an event driver) or an ID to allow you to generate the child menu and a CMenuListbox pointer for your child menu.  (You did say you create/modify the menus dynamically).

There is certainly some coding required but you would have to understand the code written by someone else should you find something somewhere that purports to do the job (and may well require bits recoding for your specific instance).
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9597620
Can You post some sample code of a watered down version to illustrate how it would work and what it  would look like ?
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9597642
"You put the itmes into the list therefore you 'know' if the correct response to the mouse click / enter key is to perform an action or to display/hide the sub menu."

Just to be clear on the main issue of the question. The sub menu should appear when I highlight the parent item. But something else will happen when I click the parent item.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9597779
I had a further thought.  This is overly complex.
Use a tree control instead.  That only requires the one window and a lot of the functionality is already coded into it.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9597823
I just got called away briefly before I could type more.
A tree control.  You can add/modify/remove items on the fly.  You can populate the branch only when it is to be opened.  Keyboard support for arrow keys are built in.  Only one child window required so should be easier for you to control everything.

logic:
create as child control, populate with items. Display some symbol (or other visual hint) for the items to have a sub menu.
When a branch is to be displayed add the items to that branch. Resize window width.  (Sub Menu behaviour)
Message handler for the click event - if it has children display/hide branch.  If no children then it is an event to be passed onto the parent window for handling.
USe SetCapture/GetCapture/Release capture to destroy the child when the user clicks off the control with the mouse.
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9597892
Andy thanks for your suggestions.
Actually I thought about using a tree control but I stopped because I don't think it is possible to have it come out of an internet explorer toolbar is it ?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:ElishaKlein
ID: 9597904
Also I don't like it so much becuase you have to click once for each level of the menu. I want just one click at the end when you find the item you need. highlighting opens the submenus. Can this be done with a tree control ?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9597926
Your dll/ActiveX control would have to encapsulate the tree and its functionality.
Exactly what options/actions you have/perform and how it communicates with the browser are up to you but that is another matter.  I think the tree is a possible way to go (it should provide an undestandable interface for the user).  After all your extension will have to contain at least one window.
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9597989
I realize I may have mentioned a lot of details in the question, however I tried to keep the basic question focused and well defined. I want to create a menu that has submenus and they can also have submenus, so that each each submenu opens when it's parent is highlighted and each item (or parent item also) is selcted when it is clicked. As I said I already have this menu in DHTML (so the user interface is well defined and easy to understand).
The fact that I could (somehow, maybe) modify the behavior of either a menu, or a list, or a tree to achieve this behaviour is I feel obvious.
The question is how I am going to do that.
I need at least a viable plan, if not some sample code so I can decide what to do.

Thanks.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9597996
Just fill the tree and all it's branches when you launch it.  There is a function in a tree that will open all branches automatically.
You code however have some code that checks if the mouse is hovering over an item (for say 0.4 secs) and then it automatically opens that branch for you (if it is a sub menu type of item that is).
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9598058
go to www.msnbc.com .
Look at the menu on the left side of the page.
Highlight travel for example.
This is what I want my menu to behave like, except in one way.
In my alternative menu if you click say "latest travel news" it will also take me to a web page.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9599246
OK.
I'll look at the msnbc page re menu.
I'll not have chance to post anything else today.  
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9599284
I do have time for a quick comment.  It looks like a more or less standard context menu.  Possibly owner drawn.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9599559
To be clearer.
Main menu - list box based.  Check for mouse hovering over an item, display context menu if it is hovering and if there is a context menu available.
For the hover follow the mouse move messages and see if it is over an item in the list box for more than a predetermined interval.

That way mouse click on listbox - do event
Mouse hover, popup menu appears and that will fire an event is something is selected
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9599681
But that brings us back to all the problems with the list box.
I started looking at hooking the click on the menu. looks simpler and more promising.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9604506
I had a look at the msnbc site and the menu there.  The main menu part looks very different to the rest.  The rest looks like a normal context menu.
Hence the suggestion of the main menu part as a list box.
The top entries don't have any context menu possibilites the lower ones do.

I suggest the following three stages of coding.
So have a list box with 2 items.  Click on the first and your 'app' fires an event to go to www.x.com, click the second and it shows a dialog with some info.
<easy coding>

Now
Modify the click on the second item and instead of the dialog start a context menu (as if a right click had occurred)  This is too test the functionality of the context menu and its code.
<still pretty easy>

Finally.
Reset the click event for the second item.
When the list box is visible start a timer at say 0.1 second intervals.
Set a member var to hold the current item's number in the list (or some other unique ID)
have a handler for the mouse move message.
in the handler do the following.
check if the mouse is captured by the app and if not capture the mouse. (we want all mouse movements)
check is the mouse over the list (maybe it has moved off) and if not release the mouse, set to null the var noting which item the mouse is over.
In the OnTimer do the following
is the item the mouse is over the same as the last one the timer fired at.  Yes - increment a counter, no - set counter to zero.
Is the counter at a preset value (eg. 4 == 0.4 secs) yes - display context menu.

There are other things to handle but that gives you an idea of a possible logic.
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9605014
Andy,
Thanks for your help.
I think we also had another misunderstanding here. I do not need the top level to look diferent like it does on msnbc. It was just an example to say that I want it to look like (the rest of it after the top). My fault for not explaining that.
In  any case I have decided to use a context menu and hook the click events. Thats what I found in the link that Rosh gave me, so I awarded him the points.
I feel like you should also get some points since you spent so much time, even though it didn't help in the end.
Right now I am getting the click events from the menu from the hook, but the only problem now is I can't recognize if the click was inside the menu or not. I tried using MenuItemFromPoint but that didn't work (I think because I don't have the HWND from the menu I have the hwnd from my window that holds the menu and gets the hook notifications) so right now I am assuming that the click is on the last item that was highlighted but this is a probelem when the user actually clicks outside the menu.
Can I open a private question for you and if you help me with this give you another 500 points ?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9605019
OK - misunderstandings occurr and it's your question to award how you see fit.
I'll have a think about the latest problem.  If I can help then we can generate another question specifically for the points as you suggest.
I'll post something later.
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9605032
Ok thanks.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9605063
Please could you send me the code invovlved in launching the context menu and your click event you get (or the complete file).
info@ainscow.ch
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9605209
Got it to work !!
Do you still want to see the code ?
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9605226
In case anyone else wants to see the answer:
I saved the window that was highlighted in OnMenuSelect
POINT pt;
GetCursorPos(&pt);
MenuWnd = WindowFromPoint(pt);

And then in the MouseProc (from the hook) I checked if it was the same window before deciding that an item had been selected.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9605582
I had an idea on the same lines myself but I didn't know enough of what you were doing to suggest it.
So does it all work?
0
 
LVL 1

Author Comment

by:ElishaKlein
ID: 9607034
Yeah it does.
I'll remember your name if I have any trouble in the future.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now