What menu item was selected?
-
Due to limitations of a combo box I was unable to get around, I decided to use a button and draw a popup menu when the button is clicked. The problem is that the contents of the menu are dynamic (for example, a list of widgets). I want the same method to be invoked no matter which menu item is selected (which I can do by giving the same ID to each menu item), and have that method ask the CMenu object which item was selected. However, there does not seem to be a method of CMenu to ask which menu item was selected. I suppose it would have been too easy to have TrackPopupMenu() return the item selected. Incidently, the problem with the combo box is that I want the window text to say "Select Widget" at all times, rather than to display the selected widget. Using SetWindowText() on the CComboBox does not do the trick.
-
Due to limitations of a combo box I was unable to get around, I decided to use a button and draw a popup menu when the button is clicked. The problem is that the contents of the menu are dynamic (for example, a list of widgets). I want the same method to be invoked no matter which menu item is selected (which I can do by giving the same ID to each menu item), and have that method ask the CMenu object which item was selected. However, there does not seem to be a method of CMenu to ask which menu item was selected. I suppose it would have been too easy to have TrackPopupMenu() return the item selected. Incidently, the problem with the combo box is that I want the window text to say "Select Widget" at all times, rather than to display the selected widget. Using SetWindowText() on the CComboBox does not do the trick.
gokings wrote: the problem with the combo box is that I want the window text to say "Select Widget" at all times, rather than to display the selected widget This is non-standard GUI behaviour, unless there is a REALLY good reason you should try to suprise the user as little as possible. That said, you could use an edit control with read-only text 'Select Widget' and handle mouse-click messages. When one is received create/display a list box under the edit control and then destroy it once the user makes a selection. gokings wrote: I want the same method to be invoked no matter which menu item is selected (which I can do by giving the same ID to each menu item) Don't use the same id, rather use one method to handle the range of commands you will put in the menu. No offense, but neither the comb-box requirement nor your menu workaround provide a standard interface. I would really rethink going this route. Why don't you want to display the selected item in the combobox ? ...cmk Save the whales - collect the whole set
-
Due to limitations of a combo box I was unable to get around, I decided to use a button and draw a popup menu when the button is clicked. The problem is that the contents of the menu are dynamic (for example, a list of widgets). I want the same method to be invoked no matter which menu item is selected (which I can do by giving the same ID to each menu item), and have that method ask the CMenu object which item was selected. However, there does not seem to be a method of CMenu to ask which menu item was selected. I suppose it would have been too easy to have TrackPopupMenu() return the item selected. Incidently, the problem with the combo box is that I want the window text to say "Select Widget" at all times, rather than to display the selected widget. Using SetWindowText() on the CComboBox does not do the trick.
Hi, I suggest you give different ID's to each menu-item. Each item gets a bnClicked handler (or whatever) Then you can call the function you want to execute, BUT before you do that set a variable to some value. eg.
void YourClass::OnBnClickedMenuItem1(){ variable = 1; SameFunctionForEachItem(); } void YourClass::SameFunctionForEachItem(){ switch(variable){ case 1: //...do something break; //.... and so one } }
hope this helps! "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix -
Due to limitations of a combo box I was unable to get around, I decided to use a button and draw a popup menu when the button is clicked. The problem is that the contents of the menu are dynamic (for example, a list of widgets). I want the same method to be invoked no matter which menu item is selected (which I can do by giving the same ID to each menu item), and have that method ask the CMenu object which item was selected. However, there does not seem to be a method of CMenu to ask which menu item was selected. I suppose it would have been too easy to have TrackPopupMenu() return the item selected. Incidently, the problem with the combo box is that I want the window text to say "Select Widget" at all times, rather than to display the selected widget. Using SetWindowText() on the CComboBox does not do the trick.
Please keep your replies in the thread as oppossed to direct reply so others can follow. For the menu option see the MSDN doc's for ON_COMMAND_RANGE. The example they give is specifically for handling menu selection. For you ID_FILE_MENUITEM1 and ID_FILE_MENUITEM3 would define the extents of all possible menu options you would dynamically add. From MSDN : // The code fragment below shows how to use ON_COMMAND_RANGE macro // to map a contiguous range of command IDs to a single message // handler function (i.e. OnFileMenuItems() is the sample below). BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) // ... ON_COMMAND_RANGE(ID_FILE_MENUITEM1, ID_FILE_MENUITEM3, OnFileMenuItems) END_MESSAGE_MAP() void CMainFrame::OnFileMenuItems(UINT nID) { CMenu* mmenu = GetMenu(); CMenu* submenu = mmenu->GetSubMenu(0); submenu->CheckMenuRadioItem(ID_FILE_MENUITEM1, ID_FILE_MENUITEM3, nID, MF_BYCOMMAND); } ...cmk Save the whales - collect the whole set
-
Please keep your replies in the thread as oppossed to direct reply so others can follow. For the menu option see the MSDN doc's for ON_COMMAND_RANGE. The example they give is specifically for handling menu selection. For you ID_FILE_MENUITEM1 and ID_FILE_MENUITEM3 would define the extents of all possible menu options you would dynamically add. From MSDN : // The code fragment below shows how to use ON_COMMAND_RANGE macro // to map a contiguous range of command IDs to a single message // handler function (i.e. OnFileMenuItems() is the sample below). BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) // ... ON_COMMAND_RANGE(ID_FILE_MENUITEM1, ID_FILE_MENUITEM3, OnFileMenuItems) END_MESSAGE_MAP() void CMainFrame::OnFileMenuItems(UINT nID) { CMenu* mmenu = GetMenu(); CMenu* submenu = mmenu->GetSubMenu(0); submenu->CheckMenuRadioItem(ID_FILE_MENUITEM1, ID_FILE_MENUITEM3, nID, MF_BYCOMMAND); } ...cmk Save the whales - collect the whole set
Thank you very much, that was very useful, and solved the problem nicely. I was about to search the MFC header files for "ON_COMMAND" to see if I could find something nearby that seemed suitable for a range (as you had suggested earlier), when I received your message. Once I saw the ON_COMMAND_RANGE and that the associated method received the command id as a parameter, the solution was clear. Thanks again. P.S. Sorry for taking so long to respond, I've been a bit under the weather.