CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2006
    Posts
    75

    HMENU: How to get parent menu? (build in function)

    Hi everyone,
    Faced with a following problem:

    I have a drop down menu with items. Each item has a sub menu.
    The structure is as follows:
    subid1
    id0----subid1
    subid1

    subid1
    id1----subid1
    subid1

    all sub items has the same id so when WM_MENUCOMMAND arrives it is necessary to determine the parent menu.
    Found nothing in MSDN.
    Do i need to interate through all id-x menus comparing the handle from WM_MEMUCOMMAND to current menu handle? Does win has internal tree of menu's handles?
    Thanks in advance.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: HMENU: How to get parent menu? (build in function)

    all sub items has the same id so when WM_MENUCOMMAND arrives it is necessary to determine the parent menu.
    No, only thing you need is just to give them unique ids.
    Best regards,
    Igor

  3. #3
    Join Date
    Jul 2006
    Posts
    75

    Re: HMENU: How to get parent menu? (build in function)

    Quote Originally Posted by Igor Vartanov View Post
    No, only thing you need is just to give them unique ids.
    The problem is that my menu is not static (number of idx may vary on program running). So when i add a new idx menu item i must generate unique menu id's for it. So there must be a dynamic storage for them. This requires additional code in message loop to run through all id's and find necessary.
    Well this is not difficult to implement. I was just wondering is there any already made solution for that in WinAPI?

    PS: The way with menu handle searching seems better because i can specify the same id's for submenu items. Am i right?

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: HMENU: How to get parent menu? (build in function)

    number of idx may vary on program running
    It depends on many details. Number of menu items may vary while each item may have a static/predefined command id.

    Menu is intended to send commands. Commands must be unique. It's up to application how it manages to handle that. Period.

    While being generated menu ids may be grouped in ranges. This might help to deduce the parent menu alright.
    Best regards,
    Igor

  5. #5
    Join Date
    Aug 2008
    Posts
    112

    Re: HMENU: How to get parent menu? (build in function)

    You didn't tell us how each subitem was added, because it needs to know whose parent it is, shouldn't that be the parent handle it needs to store for later use ?
    hi,,,

  6. #6
    Join Date
    Jul 2006
    Posts
    75

    Re: HMENU: How to get parent menu? (build in function)

    Quote Originally Posted by Igor Vartanov View Post
    It depends on many details. Number of menu items may vary while each item may have a static/predefined command id.
    Yes. My case is as you described above. Number of menu items is variable. (when user adds a new graph to plot a new menu item is added). Each menu item has fixed number of sub-items. I think that these sub menu items must have the same id. But when message comes containing id we must find to what parent menu does this id belongs.

    Quote Originally Posted by Igor Vartanov View Post
    Menu is intended to send commands. Commands must be unique. It's up to application how it manages to handle that. Period.
    Yes. As i described above the unique command will help to distinguish sub-menu id from other sub-menu id. no information about parent menu. sad that windows does builds the tree

    Quote Originally Posted by Igor Vartanov View Post
    While being generated menu ids may be grouped in ranges. This might help to deduce the parent menu alright.
    You are talking about sub menu id?

    Quote Originally Posted by Khiem View Post
    You didn't tell us how each subitem was added, because it needs to know whose parent it is, shouldn't that be the parent handle it needs to store for later use ?
    If my sub-items have different id's than on adding sub-item i can save the parent menu handle than to identify the menu item. But as i said earlier my sub-items are not unique. (unique only in the menu item but not across the other menu items)

    It's much easier for me to plot the situation

    .......................id0
    menu1 (no id).....id1
    ........................id2

    ........................id0
    menu2 (no id).....id1
    ........................id2

    ........................id0
    menu3 (no id).....id1
    ........................id2

    The other idea came into my mind. I can generate ABSOLUTELY unique id's but storing them in array adding a parent menu handle.

    .............................ARRAY WILL HAVE:
    ........................id0 (menu1 handle)
    menu1 (no id).....id1 (menu1 handle)
    ........................id2 (menu1 handle)

    ........................id3 (menu2 handle)
    menu2 (no id).....id4 (menu2 handle)
    ........................id5 (menu2 handle)

    ........................id6 (menu3 handle)
    menu3 (no id).....id7 (menu3 handle)
    ........................id8 (menu3 handle)

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: HMENU: How to get parent menu? (build in function)

    You are talking about sub menu id?
    I'm talking about the concept in whole. Somehow you got fond of idea of back tracing a menu's item parent, a way you wasn't able to find in WinAPI, and I'm not aware of one either. My suggestion still remains: deal with unique menu ids. And it's up to you whether you follow it.
    Best regards,
    Igor

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: HMENU: How to get parent menu? (build in function)

    Quote Originally Posted by Sh@dow View Post
    But when message comes containing id we must find to what parent menu does this id belongs.
    As Igor mentioned, you don't need to try to approach it this way.

    As you have found out, the WinApi doesn't provide a way to get parent menu id's. So the parent menu idea isn't going to work.

    What you have to do is give each menu item a unique id. You are close with the answer of storing a mapping in an array, but you'll need to forget storing the menu handle.

    All you need to do is when you generate the hierarchy (and generate the unique menu id), store additional info with regard to it's hierarchy).

    Create a class that stores the hierarchy info and then use a map to map it to the id.

    Something like (pseudo code)
    Code:
    class MenuItem
    {
      // ctor omitted
      string hierarchy;
      int id;
    }
    
    // dynamically generate id (and hierarchy)
    MenuItem item(_T("topmenu\submenu1\submenu2"), id);
    
    m_MenuMap.Add( currentId, item );
    Now to use it..
    Code:
    void OnMenuClicked(  ..., int Id )
    {
      // use the map to retrieve the additional info for the menu id
      MenuItem item = m_MenuMap[ id ];
    }
    Keep in mind that the code above is pseudo code and I don't suggest you use a string to track the menu hierarchy. It's merely done that way to show you that you can create unique menu id's and map them to any additional data (hierarchy) that you need.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured