{"id":27573,"date":"2007-03-19T10:00:00","date_gmt":"2007-03-19T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2007\/03\/19\/how-do-the-menu-functions-find-items\/"},"modified":"2007-03-19T10:00:00","modified_gmt":"2007-03-19T10:00:00","slug":"how-do-the-menu-functions-find-items","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20070319-00\/?p=27573","title":{"rendered":"How do the menu functions find items?"},"content":{"rendered":"<p>Most of the menu item functions such as <code>GetMenuItemInfo<\/code> allow you specify the menu item either by position or by command. Some of them use the <code>MF_BYPOSITION<\/code> and <code>MF_BYCOMMAND<\/code> flags. Others separate the search algorithm into a separate <code>fByPosition<\/code> flag.\n Searching for menu items by position is straightforward: The specified position is used as a zero-based index into the menu. In other words, the first item in the menu is item zero.\n Searching for menu items by command is trickier. The menu manager searches the entire menu hierarchy, <strong>including submenus<\/strong>, for an item with the command you specify. If more than one menu item has the identifier you requested, then one of them is chosen arbitrarily. Searching the hierarchy for a command means that you can, for example, remove or disable a menu item by just passing the root menu (which you typically have easy access to) and the item identifier. If the submenus were not searched, then synchronizing menu states would be a much more cumbersome affair.\n But what if your menu has multiple items with the same identifier? Well, the short answer is, &#8220;Then don&#8217;t use <code>MF_BYCOMMAND<\/code>.&#8221; You can still use <code>MF_BYPOSITION<\/code> to access your menu items. But why would you have multiple items with the same identifier in the first place? When the user selects the menu, a <code>WM_COMMAND<\/code> is posted to the window with the menu identifier as one of its parameters, and none of the other parameters gives you the menu handle. If you have multiple menu items with the same identifier, you won&#8217;t be able to tell which of them the user picked!\n There was <a href=\"http:\/\/groups.google.com\/group\/microsoft.public.win32.programmer.gdi\/msg\/168795212a13d297\"> an emotional discussion a while back<\/a> that generated far more heat than light. But I can use my psychic powers to explain what that person was seeing, even though not enough information was provided in the original problem description, and it&#8217;s not a bug in Windows.\n Addressing the original complaint: If you have a menu with more than one item with the same identifier, then <code>MF_BYCOMMAND<\/code> is ambiguous, and all that is promised is that <strong>some<\/strong> item with that identifier will be found. It might not be the one you wanted, but since you gave multiple items the same name, the menu manager did the best it could. This is analogous to other searching functions like <code>FindWindow<\/code> and <code>GetDlgItem<\/code>, which operate on the first item they find. If multiple items match the criteria you specify, it just returns one of them.<\/p>\n<p> As for the specific problem: It so happens that there is no cache, at least not yet. (But who knows, there might be a cache in the future if we discover that lots of applications query for the same item in rapid succession.) But what this person didn&#8217;t realize is that the unnamed custom GUI library they&#8217;re using doesn&#8217;t create submenus until the <code>WM_INITMENUPOPUP<\/code> message is received. The reason why the item isn&#8217;t found before the submenu is opened is that until the submenu is opened, <strong>the submenu doesn&#8217;t exist<\/strong>. And naturally, you can&#8217;t find something that doesn&#8217;t exist. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of the menu item functions such as GetMenuItemInfo allow you specify the menu item either by position or by command. Some of them use the MF_BYPOSITION and MF_BYCOMMAND flags. Others separate the search algorithm into a separate fByPosition flag. Searching for menu items by position is straightforward: The specified position is used as a [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-27573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Most of the menu item functions such as GetMenuItemInfo allow you specify the menu item either by position or by command. Some of them use the MF_BYPOSITION and MF_BYCOMMAND flags. Others separate the search algorithm into a separate fByPosition flag. Searching for menu items by position is straightforward: The specified position is used as a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/27573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=27573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/27573\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=27573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=27573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=27573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}