Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Menu Item capture

Menu Item capture

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Al_Pennyworth
    wrote on last edited by
    #1

    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???

    R L 2 Replies Last reply
    0
    • A Al_Pennyworth

      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???

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • R Ravi Bhavnani

        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

        A Offline
        A Offline
        Al_Pennyworth
        wrote on last edited by
        #3

        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?

        R 1 Reply Last reply
        0
        • A Al_Pennyworth

          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?

          R Offline
          R Offline
          Ravi Bhavnani
          wrote on last edited by
          #4

          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 the TPM_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

          1 Reply Last reply
          0
          • A Al_Pennyworth

            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???

            L Offline
            L Offline
            liquid_
            wrote on last edited by
            #5

            Try CCmdUI interface for that menu command. In ClassWizzard you actually have 2 messages for a menu_item: ON_ID_menu_item:COMMAND and ON_ID_menu_item:UPDATE_COMMAND_UI. Use the second one for the purpose you face to.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups