{"id":13803,"date":"2010-06-04T07:00:00","date_gmt":"2010-06-04T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/06\/04\/how-do-i-enable-and-disable-the-minimize-maximize-and-close-buttons-in-my-caption-bar\/"},"modified":"2010-06-04T07:00:00","modified_gmt":"2010-06-04T07:00:00","slug":"how-do-i-enable-and-disable-the-minimize-maximize-and-close-buttons-in-my-caption-bar","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100604-00\/?p=13803","title":{"rendered":"How do I enable and disable the minimize, maximize, and close buttons in my caption bar?"},"content":{"rendered":"<p>\nA customer was having problems with the small icon that appears\nin the upper left corner of the caption:\n<\/p>\n<blockquote CLASS=\"q\"><p>\nIn my program, I need to enable and disable the Close button\nprogrammatically,\nsince the program sometimes goes into a state where I don&#8217;t want\nthe user to close it.\nI do this by removing the <code>WS_SYS&shy;MENU<\/code> style when\nI want to disable the Close button,\nand adding it back when I want to re-enable it.\nHowever, doing this has as a side effect that the icon for\nmy program doesn&#8217;t appear in the title bar any more.\nIf I never touch the <code>WS_SYS&shy;MENU<\/code> style, then\nit works fine (but then I don&#8217;t get the enable\/disable behavior\nthat I want).\n<\/p><\/blockquote>\n<p>\nOkay, the first problem is that you want to disable the Close button.\nUsability research indicates that users really don&#8217;t like it when\nyou disable the Close button.\nIt makes them feel trapped and uncomfortable.\nThis is the reason why standard Windows wizards don&#8217;t provide a way\nfor you to disable the Close button.\nThe user should always have a way out.\n<\/p>\n<p>\nImagine showing a dialog box saying\n&#8220;<a HREF=\"http:\/\/www.wendyhome.com\/2009\/01\/28\/no-expectation-of-privacy\/\">By clicking OK, you agree to the following terms<\/a>,&#8221;\nand not having any way for the user to say, &#8220;No, thanks.&#8221;\nYou should leave the Close button enabled,\nand if the user clicks it at a bad time,\nyou can say,\n&#8220;Okay, I heard you, but I&#8217;m right in the middle of something,\nbut once that&#8217;s done, I&#8217;ll close.&#8221;\n<\/p>\n<p>\nOkay, but suppose you&#8217;re in one of those cases where, really,\nyou need to disable the Close button.\nYou&#8217;ve read the guidelines, you understand why they&#8217;re there,\nbut you believe that you are an exceptional case.\n<\/p>\n<p>\nIf you never want the Close button enabled, then you can just\nspecify the\n<code>CS_NO&shy;CLOSE<\/code> style when you register the class.\n<\/p>\n<p>\nTo disable the Close button in the caption dynamically,\nyou enable and disable the <code>SC_CLOSE<\/code> menu item\nin your system menu.\n<\/p>\n<pre>\nvoid DisableCloseButton(HWND hwnd)\n{\n EnableMenuItem(GetSystemMenu(hwnd, FALSE), SC_CLOSE,\n                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);\n}\nvoid EnableCloseButton(HWND hwnd)\n{\n EnableMenuItem(GetSystemMenu(hwnd, FALSE), SC_CLOSE,\n                MF_BYCOMMAND | MF_ENABLED);\n}\n<\/pre>\n<p>\nThe other two caption buttons are controlled by window styles:\n<\/p>\n<pre>\nvoid DisableMinimizeButton(HWND hwnd)\n{\n SetWindowLong(hwnd, GWL_STYLE,\n               GetWindowLong(hwnd, GWL_STYLE) &amp; ~WS_MINIMIZEBOX);\n}\nvoid EnableMinimizeButton(HWND hwnd)\n{\n SetWindowLong(hwnd, GWL_STYLE,\n               GetWindowLong(hwnd, GWL_STYLE) | WS_MINIMIZEBOX);\n}\nvoid DisableMaximizeButton(HWND hwnd)\n{\n SetWindowLong(hwnd, GWL_STYLE,\n               GetWindowLong(hwnd, GWL_STYLE) &amp; ~WS_MAXIMIZEBOX);\n}\nvoid EnableMaximizeButton(HWND hwnd)\n{\n SetWindowLong(hwnd, GWL_STYLE,\n               GetWindowLong(hwnd, GWL_STYLE) | WS_MAXIMIZEBOX);\n}\n<\/pre>\n<p>\nWhy is the close button managed differently from the minimize\nand maximize buttons?\n<\/p>\n<p>\nHistory.\n<\/p>\n<p>\nOriginally, the window caption had only two buttons in the upper\nright corner, the minimize and maximize buttons,\nand they were controlled with a window style.\nWindows&nbsp;95 added the Close button,\nbut then there was the question of knowing when to enable and disable it.\nBut wait, we already know when to enable and disable it:\nThe application told us when it enabled and disabled the\n<code>SC_CLOSE<\/code> menu item.\nBingo, just hook up the Close button to the existing menu item\n(which applications were already in the habit of maintaining),\nand magic,\nit just works.\nNo need for applications to write special code to support the Close\nbutton.\nThey already wrote the code; they just didn&#8217;t realize it!\n<\/p>\n<p>\n<b>Exercise<\/b>:\nWhat&#8217;s wrong with these alternative functions for enabling\nand disabling the Close button:\n<\/p>\n<pre>\n<i>\/\/ code in italics is wrong\nvoid DisableCloseButton(HWND hwnd)\n{\n SetClassLong(hwnd, GCL_STYLE,\n              GetClassLong(hwnd, GCL_STYLE) | CS_NOCLOSE);\n}\nvoid EnableCloseButton(HWND hwnd)\n{\n SetClassLong(hwnd, GCL_STYLE,\n              GetClassLong(hwnd, GCL_STYLE) &amp; ~CS_NOCLOSE);\n}<\/i>\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A customer was having problems with the small icon that appears in the upper left corner of the caption: In my program, I need to enable and disable the Close button programmatically, since the program sometimes goes into a state where I don&#8217;t want the user to close it. I do this by removing the [&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-13803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer was having problems with the small icon that appears in the upper left corner of the caption: In my program, I need to enable and disable the Close button programmatically, since the program sometimes goes into a state where I don&#8217;t want the user to close it. I do this by removing the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/13803","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=13803"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/13803\/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=13803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=13803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=13803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}