Get the menuitem which was pressed
-
Hello all, I'm having difficulties to get the string of a menu item pressed. What I'm doing is writing a IE toolbar and in one of the pull down menu's I dynamically add popup menu's which are also dynamically filled. Just for a theoretical example let's say I have a button 'drives' on the toolbar. Pressing the pulldown menu will add some menu entries (the same amount of disk on my system, C; D; E; ...). Every menu item is a popup itself, and a dynamically created Popupmenu is attached - containing the root level directories for the selected drive (temp, program files, winnt, ...). Following is an adapted piece of code, but it works how it suppose to
MENUITEMINFO mmi; mmi.cbSize=sizeof(MENUITEMINFO); mmi.fMask=MIIM_STRING | MIIM_DATA | MIIM_SUBMENU; char fn[50]; // 3 drives in the example for (int i=0; i<3; i++) { HMENU hMenuToInsert; hMenuToInsert = CreatePopupMenu(); { char drv[255]; while (getthedrives) { sprintf( drv, "%s", a_directory); AppendMenu(hMenuToInsert, MF_ENABLED|MF_STRING, ID_DUMMY, drv); } } sprintf( fn, "Drive %s", drvletter); mmi.hSubMenu = hMenuToInsert; mmi.dwTypeData = fn; mmi.fType=MFT_STRING; //insert the drives InsertMenuItem(m_hDriveMenu, 2+i, TRUE, &mmi); }
The problem I'm having is, is that I'm using a dummy ID (ID_DUMMY). When my ON_COMMAND fires, I have no way to get the correct menu item using GetMenuItemInfo because a) I'm using the same id for every popup menuitem inserted b) I'm reusing the HMENU (hMenuToInsert) for my popmenu's Does anyone know of a simple way to get the text of the menu (directorys on a drive in my case) pressed? Thanks Wim -
Hello all, I'm having difficulties to get the string of a menu item pressed. What I'm doing is writing a IE toolbar and in one of the pull down menu's I dynamically add popup menu's which are also dynamically filled. Just for a theoretical example let's say I have a button 'drives' on the toolbar. Pressing the pulldown menu will add some menu entries (the same amount of disk on my system, C; D; E; ...). Every menu item is a popup itself, and a dynamically created Popupmenu is attached - containing the root level directories for the selected drive (temp, program files, winnt, ...). Following is an adapted piece of code, but it works how it suppose to
MENUITEMINFO mmi; mmi.cbSize=sizeof(MENUITEMINFO); mmi.fMask=MIIM_STRING | MIIM_DATA | MIIM_SUBMENU; char fn[50]; // 3 drives in the example for (int i=0; i<3; i++) { HMENU hMenuToInsert; hMenuToInsert = CreatePopupMenu(); { char drv[255]; while (getthedrives) { sprintf( drv, "%s", a_directory); AppendMenu(hMenuToInsert, MF_ENABLED|MF_STRING, ID_DUMMY, drv); } } sprintf( fn, "Drive %s", drvletter); mmi.hSubMenu = hMenuToInsert; mmi.dwTypeData = fn; mmi.fType=MFT_STRING; //insert the drives InsertMenuItem(m_hDriveMenu, 2+i, TRUE, &mmi); }
The problem I'm having is, is that I'm using a dummy ID (ID_DUMMY). When my ON_COMMAND fires, I have no way to get the correct menu item using GetMenuItemInfo because a) I'm using the same id for every popup menuitem inserted b) I'm reusing the HMENU (hMenuToInsert) for my popmenu's Does anyone know of a simple way to get the text of the menu (directorys on a drive in my case) pressed? Thanks WimWhy are you using the same ID for every menu item? You're defeating the purpose of IDs by doing that. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0.1! | RightClick-Encrypt You cannot truly appreciate Dilbert unless you've read it in the original Klingon.
-
Why are you using the same ID for every menu item? You're defeating the purpose of IDs by doing that. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0.1! | RightClick-Encrypt You cannot truly appreciate Dilbert unless you've read it in the original Klingon.
Hi Michael, I'm reusing them because I really have no clue how many directories I will have. Let's say I have 10.000 directories, that means I have to define 10.000 unique id's. You know of a way to create that many on the fly, or maybe a better way to do it? The other problem I have then is to what popupmenu the directory I clicked belongs (C:, D:, E:), but I supppose I can have a list of HMENU id's and I check every list/MENU_ID for success. Thanks Wim
-
Hi Michael, I'm reusing them because I really have no clue how many directories I will have. Let's say I have 10.000 directories, that means I have to define 10.000 unique id's. You know of a way to create that many on the fly, or maybe a better way to do it? The other problem I have then is to what popupmenu the directory I clicked belongs (C:, D:, E:), but I supppose I can have a list of HMENU id's and I check every list/MENU_ID for success. Thanks Wim
Well, command IDs can be in the range 1-65535. Just start a counter a 1 and increment it for every menu item you add. Each menu under the first level can use 1-65535, you don't need a unique ID for every single item if you think you'd ever have more than 65535. You can tell which top-level popup has been selected by handling
WM_MENUSELECT
, and from there you can work out which directory was selected. You should also give the top-level popups IDs for easier tracking (and those IDs shouldn't be duplicated in the submenus). You can do this withInsertMenuItem()
. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0.1! | RightClick-Encrypt There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true. -
Well, command IDs can be in the range 1-65535. Just start a counter a 1 and increment it for every menu item you add. Each menu under the first level can use 1-65535, you don't need a unique ID for every single item if you think you'd ever have more than 65535. You can tell which top-level popup has been selected by handling
WM_MENUSELECT
, and from there you can work out which directory was selected. You should also give the top-level popups IDs for easier tracking (and those IDs shouldn't be duplicated in the submenus). You can do this withInsertMenuItem()
. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0.1! | RightClick-Encrypt There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.Ok, I see it now. One thing what I discovered when playing with the WM_MENUSELECT was that I couldn't get the (a random) text from my popup menu (just for a test, take the first entry of the popupmenu, I will move it to the WM_COMMAND later). What I did was this:
LRESULT CMyToolBar::OnMenuSelect(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if (lParam&&(HIWORD(wParam)&MF_POPUP)) { HMENU hSubMenu = GetSubMenu ( (HMENU)lParam, LOWORD(wParam)); MENUITEMINFO mii; mii.fMask = MIIM_STRING; char c[2000]; mii.dwTypeData=c; mii.cch=2000; GetMenuItemInfo(hSubMenu, 0, TRUE, &mii); MessageBox(mii.dwTypeData); } return 0; }
According to the documentation, lParam contains the Handle to the menu I pressed (the main menu), and if HIWORD(wParam) contains flag MF_POPUP, the LOWORD(wParam) contains the the position pressed in the menu. If I use GetSubMenu((HMENU)lParam, HIWORD(wParam)), I should get the handle to my popupmenu containing the directories, right? I outputted the parameters, and the handle and index looked OK. But when I used GetMenuItemInfo using the handle obtained with GetSubMenu and index 0 (the first Item in the popup menu), it returns an empty string. Any idea what I'm doing wrong? Thanks Wim -
Well, command IDs can be in the range 1-65535. Just start a counter a 1 and increment it for every menu item you add. Each menu under the first level can use 1-65535, you don't need a unique ID for every single item if you think you'd ever have more than 65535. You can tell which top-level popup has been selected by handling
WM_MENUSELECT
, and from there you can work out which directory was selected. You should also give the top-level popups IDs for easier tracking (and those IDs shouldn't be duplicated in the submenus). You can do this withInsertMenuItem()
. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0.1! | RightClick-Encrypt There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.