-
March 20th, 2004, 02:35 PM
#1
Adding sub menus to a menu
Heres what I have...
Code:
{
CMenu menu;
menu.CreatePopupMenu();
for (int x = 0; x < ndex; x++)
{
Swap = StrArr.GetAt(x);
if (Swap != "Main")
menu.AppendMenu(MF_STRING, x, Swap);
}
CPoint point;
GetCursorPos(&point);
SetForegroundWindow();
int Sel = TrackPopupMenuEx(menu.GetSafeHmenu(), TPM_RETURNCMD, point.x, point.y, GetSafeHwnd(), NULL);
PostMessage(WM_NULL, 0, 0);
menu.DestroyMenu();
}
I've deleted variable declarations and some code to save space.
With the AppendMenu(), I'm of course adding menu items. What I want to do is add sub menus to each of these menu items.
Instead of clicking on an item and executing a function, I want a sub menu to pop up. Can't figure out how to do it.
Any help?
Thanks!
Daniel
-
March 20th, 2004, 07:44 PM
#2
I don't understand what you are asking. Is the code you show working and you want to add to it or is it not doing what you are asking to do?
I am not familiar with the differences bertween TrackPopupMenuEx and TrackPopupMenu but I think the only difference is the last parameter, which you are using NULL for. Probably it is unnecessary to use TrackPopupMenuEx, but that probably makes no difference. What can make a difference is using CMenu::TrackPopupMenu instead of ::TrackPopupMenuEx.
The simplified sample code helps of course. Something that helps even more is something we can use to duplicate the problem. The following sample works for me well enough to show a menu, but I don't know if it does what you need.
Code:
m_Menu.CreatePopupMenu();
m_Menu.AppendMenu(MF_STRING, 1, "One");
m_Menu.AppendMenu(MF_STRING, 2, "Two");
m_Menu.AppendMenu(MF_STRING, 3, "Three");
m_Menu.TrackPopupMenu(0, 100, 100, m_pMainWnd);
.
.
.
m_Menu.DestroyMenu();
-
March 20th, 2004, 09:21 PM
#3
Thanks for the reply.
The code I pasted works fine. It shows the menu, I click on an item and it does what I want.
Heres the best way to explain it...
I have this now, it works fine...
I want to turn that into this...
I want to do this with code, not the resource editor.
The menu is built at runtime.
Thanks!
-
March 20th, 2004, 09:52 PM
#4
Originally posted by Daniel1324
I want to do this with code, not the resource editor.
The menu is built at runtime.
I understand. Try this simple sample:
Code:
m_Menu.CreatePopupMenu();
m_SubMenu.CreateMenu();
m_SubMenu.AppendMenu(MF_STRING, ID_APP_EXIT, (LPCTSTR)"E&xit");
m_Menu.AppendMenu(MF_POPUP, (UINT) m_SubMenu.m_hMenu, "&File");
m_Menu.TrackPopupMenu(0, 100, 100, m_pMainWnd);
-
March 20th, 2004, 11:18 PM
#5
Ok, that worked, but another problem has popped up.
I have this, which is great...
And this...
Code:
submenu.AppendMenu(MF_STRING, ID_GOTOADDRESS, "Goto Address");
submenu.AppendMenu(MF_STRING, ID_COPYNAME, "Copy Name");
submenu.AppendMenu(MF_STRING, ID_COPYPASSWORD, "Copy Password");
As you can see, All of the menu items have the same popup menu.
And likewise, all of the menu items->sub menus have the same
command ID (ID_GOTOADDRESS, ID_COPYNAME, ID_COPYPASSWORD).
Now, if I can explain this... How can I tell if 'www.hotmail.com'
or 'www.codeguru.com' was selected.
No matter which item on the main menu is selected, all of the submenus go to the same handlers, And I dont know which item on the main menu was highlighted.
I hope I explained this good enough. Your help is great.
Thanks!
EDIT: There seems to be a problem with the server the pictures are on. They may not load. Hopefully they will.
Last edited by Daniel1324; March 20th, 2004 at 11:22 PM.
-
March 21st, 2004, 09:46 PM
#6
Yes, I think the images are showing okay.
Note that the last sample I posted is a modified version of what is in the CMenu::CreateMenu documentation. Menus sometimes takes time to figure out but usually we can figure them out after working at it a while.
I am not sure what you are asking, but I think you are saying you don't know how to determine which menu item is selected when an item is selected. I don't know what would be the best answer might be. I think it depends on how dynamic the menus need to be. I think there are two general possibilities. One would be somewhat more conventional and the other might be a more advanced option.
Typically, each menu item has a command id associated with it. I assume you know that. I think you are asking how to generate an id dynamically and how to handle the command so that the item associated with the command is known. I think there is a way to specify that MFC should execute a function if a command id is in a range is to be handled. I am not sure of the details but you, me or someone can figure that out if you decide to use that solution. The thing I am not sure of is how to reserve a range of command ids so the resource editor does not use them. I know I have seen various solutions for that too.
If that is not flexible enough, then perhaps there is a way to process the events of the menus being selected and such. Perhaps you could process the item selection and do your own mapping of item number to the selection to be processed. If it were me, I would look at what (notification) messages are sent for menus. I might also use Spy++ to determine that.
I don't know if you have solved the problem but if not then if you can choose which of the two possibilities above would work better then hopefully someone can help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|