{"id":13903,"date":"2010-05-27T07:00:00","date_gmt":"2010-05-27T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/05\/27\/when-will-the-window-manager-destroy-a-menu-automatically-and-when-do-i-need-to-do-it-manually\/"},"modified":"2010-05-27T07:00:00","modified_gmt":"2010-05-27T07:00:00","slug":"when-will-the-window-manager-destroy-a-menu-automatically-and-when-do-i-need-to-do-it-manually","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100527-00\/?p=13903","title":{"rendered":"When will the window manager destroy a menu automatically, and when do I need to do it manually?"},"content":{"rendered":"<p>\nOur old friend Norman Diamond wonders\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/06\/15\/632243.aspx#633541\">\nwhen you are supposed to destroy a menu and when you are\nsupposed to let Windows destroy it<\/a>.\n<\/p>\n<p>\nThe rules for when the window manager implicitly destroys\nmenus are actually not that complicated.\n<\/p>\n<ul>\n<li>If a window is destroyed, the menus attached to the window are also\n    destroyed:<\/p>\n<ul>\n<li>Attached as the menu bar\n        (<code>Get&shy;Menu<\/code>\/<code>Set&shy;Menu<\/code>)\n    <\/li>\n<li>Attached as the system menu (<code>Get&shy;System&shy;Menu<\/code>)\n    <\/li>\n<\/ul>\n<\/li>\n<li>If a menu is destroyed, its submenus are also destroyed.\n<\/li>\n<li>If you replace a <code>MIIM_SUBMENU<\/code> submenu,\n    the old menu is destroyed.\n<\/li>\n<li>If you pass <code>bRevert = TRUE<\/code>\n    to <code>Get&shy;System&shy;Menu<\/code>,\n    then the old system menu is destroyed and a clean system menu\n    is created in its place.\n<\/li>\n<\/ul>\n<p>\nOutside of the above situations,\nyou are on your own.<\/p>\n<p>\nOf course, when I write that &#8220;you are on your own&#8221;\nI do not mean that &#8220;every code which sees a menu is responsible for\ndestroying it.&#8221;\nIf that were the case, you would have a disaster as the slightest\npassing breeze would cause people to call <code>Destroy&shy;Menu<\/code> all\nover the place.\nRather, I mean that in all other cases,\nyou need to &#8220;work it out amongst yourselves&#8221;\nwho is responsible for destroying the menu.\nTypically, the person who creates the menu takes responsibility for\ndestroying it, although that responsibility can be handed off\nbased on mutual agreement between the creator and another component.\n<\/p>\n<p>\nThe original question did include a misunderstanding:\n<\/p>\n<blockquote CLASS=\"q\"><p>\nIf the old object belonged to a window class,\nand we destroy the old object,\nhow do we know that other windows of the same class\naren&#8217;t going to get in trouble?\n<\/p><\/blockquote>\n<p>\nThe mistaken belief here is that each window of a class\nshares the same menu.\nIf that were true,\nthen if a program created two windows of the same class,\nmodifications to one window&#8217;s menu would affect the other.\nYou can see that this is not true by inspection,\nor at least it was easier back in 1995.\nOn Windows&nbsp;95, open two Explorer windows,\nand set them into different views.\nThe two windows now have different menus:\nOne of them has a bullet next to the <i>Large Icons<\/i> menu item,\nwhereas the other has a bullet next to <i>Details<\/i>.\n<\/p>\n<p>\nWhen you register a window class, you pass in the\nmenu you want, but only in the form of a template:\n<\/p>\n<pre>\n    WNDCLASS wc;\n    ...\n    wc.lpszMenuName = MAKEINTRESOURCE(...);\n<\/pre>\n<p>\nThere is no menu yet, just a description of how to create a menu\nwhen the time comes.\nWhen you create a window from this class,\nthe window manager initializes the menu by doing the equivalent of\n<\/p>\n<pre>\n    SetMenu(hwnd, LoadMenu(pWndClass-&gt;hInstance,\n                           pWndClass-&gt;lpszMenuName));\n<\/pre>\n<p>\nEach window gets a fresh menu from the specified menu template.\nOnce that&#8217;s done,\nyou can change it all you want; it won&#8217;t affect any the menus\nassociated with any other windows.\n<\/p>\n<p>\nThe system menu works the same way:\nEvery window starts out with a default system menu,\nand when you call <code>Get&shy;System&shy;Menu<\/code>\nwith <code>bRevert = FALSE<\/code>,\nyou get a handle to that system menu,\nwhich you can modify to your heart&#8217;s content without affecting\nany other menus.\nSystem menus have this additional wrinkle where you can pass\nwith <code>bRevert = TRUE<\/code>\nto ask the window manager to destroy the current system menu\nand replace it with a fresh new default system menu.\n<\/p>\n<p>\n<b>Exercise<\/b>:\nHow would you accomplish the logical equivalent of\n<code>Get&shy;System&shy;Menu(TRUE)<\/code> for the menu bar menu?\n<\/p>\n<p>\n<b>Bonus chatter<\/b>:\nWhile the system menu certainly behaves as I described it above,\nthere&#8217;s actually a little bit of optimization going on under\nthe hood.\nWe&#8217;ll look at that\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/05\/28\/10016691.aspx\">\nnext time<\/a>.\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our old friend Norman Diamond wonders when you are supposed to destroy a menu and when you are supposed to let Windows destroy it. The rules for when the window manager implicitly destroys menus are actually not that complicated. If a window is destroyed, the menus attached to the window are also destroyed: Attached as [&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-13903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Our old friend Norman Diamond wonders when you are supposed to destroy a menu and when you are supposed to let Windows destroy it. The rules for when the window manager implicitly destroys menus are actually not that complicated. If a window is destroyed, the menus attached to the window are also destroyed: Attached as [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/13903","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=13903"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/13903\/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=13903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=13903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=13903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}