Menu Item capture
-
I have a unique project I am trying to accomplish. I have a right-click menu that has several items. This is an existing application so AppendMenu is used for most of the elements. I would like to add one additional elements, called "Select File...", which is actually a popup menu to a submenu. This submenu has to be dynamic to where it reads a directory and lists all the files in that directory. The purpose is to allow a user to select the file he/she chooses and launch an editor. In researching my project, I have the code to read and list the files. My problem is as follows: how do I capture the menu item that the user selected? In the existing structure, the AppendMenu consists of ID values so in the WM_COMMAND for the window, the ID can be captured. I cannot accomplish my task in the same manner since the # of files can vary. I was planning on using by position but how do I capture the menu item after the user has clicked on it? Maybe I am making this more complicated than it should be???
-
I have a unique project I am trying to accomplish. I have a right-click menu that has several items. This is an existing application so AppendMenu is used for most of the elements. I would like to add one additional elements, called "Select File...", which is actually a popup menu to a submenu. This submenu has to be dynamic to where it reads a directory and lists all the files in that directory. The purpose is to allow a user to select the file he/she chooses and launch an editor. In researching my project, I have the code to read and list the files. My problem is as follows: how do I capture the menu item that the user selected? In the existing structure, the AppendMenu consists of ID values so in the WM_COMMAND for the window, the ID can be captured. I cannot accomplish my task in the same manner since the # of files can vary. I was planning on using by position but how do I capture the menu item after the user has clicked on it? Maybe I am making this more complicated than it should be???
You could reserve an id range starting with a known base (eg:
IDC_EDIT_FILE_0
) and use the offset into your file list to compute the popup menu's command id. The handler for menu commands in the range would then compute the index into the file collection and cause the appropriate file to be processed. I'm guessing this is how MFC's MRU support is implemented. One downside is you can't process file collections outside the range, so you'd need to select a reasonably high upper limit. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com -
You could reserve an id range starting with a known base (eg:
IDC_EDIT_FILE_0
) and use the offset into your file list to compute the popup menu's command id. The handler for menu commands in the range would then compute the index into the file collection and cause the appropriate file to be processed. I'm guessing this is how MFC's MRU support is implemented. One downside is you can't process file collections outside the range, so you'd need to select a reasonably high upper limit. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.comThat is kind of how I am presently implemented it. I am reading through the directory, for each value, I am assigning a MenuItemInfo, then doing a SetMenuItemInfo, followed by the InsertMenuItem. I am doing this since I need to be able to retrieve the appropriate filename. I am using BASE_ID_VALUE+nIndex, where nIndex starts at 0 and I keep adding 1 to it. I have attempted to put code in the WM_COMMAND but I can never seem to capture the command to the menu. If I change the code to use AppendMenu, it seems I can capture the message in the WM_COMMAND but I am not sure how to get the filename associated with the menu item clicked. Any ideas on how I can capture the WM_COMMAND message after the user has clicked on one of the filenames?
-
That is kind of how I am presently implemented it. I am reading through the directory, for each value, I am assigning a MenuItemInfo, then doing a SetMenuItemInfo, followed by the InsertMenuItem. I am doing this since I need to be able to retrieve the appropriate filename. I am using BASE_ID_VALUE+nIndex, where nIndex starts at 0 and I keep adding 1 to it. I have attempted to put code in the WM_COMMAND but I can never seem to capture the command to the menu. If I change the code to use AppendMenu, it seems I can capture the message in the WM_COMMAND but I am not sure how to get the filename associated with the menu item clicked. Any ideas on how I can capture the WM_COMMAND message after the user has clicked on one of the filenames?
Use the
ON_COMMAND_RANGE
macro to this. You'll need to reserve 2 command ids, one for each end point of the range.ON_COMMAND_RANGE (CMD_OPEN_FILE_0, CMD_OPEN_FILE_1024, OnOpenFile)
You'll also need to keep the file list around during the execution of the command. Alternatively, if you're calling
TrackPopupMenu()
, use theTPM_RETURNCMD
flag to retrieve the selected command id. Then, compute the selected filename and just call an api to do your bidding. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com -
I have a unique project I am trying to accomplish. I have a right-click menu that has several items. This is an existing application so AppendMenu is used for most of the elements. I would like to add one additional elements, called "Select File...", which is actually a popup menu to a submenu. This submenu has to be dynamic to where it reads a directory and lists all the files in that directory. The purpose is to allow a user to select the file he/she chooses and launch an editor. In researching my project, I have the code to read and list the files. My problem is as follows: how do I capture the menu item that the user selected? In the existing structure, the AppendMenu consists of ID values so in the WM_COMMAND for the window, the ID can be captured. I cannot accomplish my task in the same manner since the # of files can vary. I was planning on using by position but how do I capture the menu item after the user has clicked on it? Maybe I am making this more complicated than it should be???