{"id":29143,"date":"2006-11-02T10:00:02","date_gmt":"2006-11-02T10:00:02","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/11\/02\/make-sure-you-disable-the-correct-window-for-modal-ui\/"},"modified":"2006-11-02T10:00:02","modified_gmt":"2006-11-02T10:00:02","slug":"make-sure-you-disable-the-correct-window-for-modal-ui","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20061102-02\/?p=29143","title":{"rendered":"Make sure you disable the correct window for modal UI"},"content":{"rendered":"<p>\nSome time ago,\nI was asked to look at two independent problems with people trying\nto do modal UI manually.\nWell, actually, when the issues were presented to me,\nthey weren&#8217;t described in quite that way.\nThey were more along the lines of,\n&#8220;Something strange is happening in our UI. Can you help?&#8221;\nOnly in the discussion of the scenarios did it become\napparent that it was improper management of modal UI that\nwas the cause.\n<\/p>\n<p>\nWe already saw\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/02\/27\/81155.aspx\">\none subtlety of managing modal UI manually<\/a>,\nnamely that you have to enable and disable the windows in the correct\norder.\nThat wasn&#8217;t the root of the problems I was looking at,\nbut enabling and disabling windows did play a major role.\n<\/p>\n<p>\nWhen we\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/04\/01\/404531.aspx\">\ntook a look at the dialog loop<\/a>,\nthe first steps involved manipulating the <code>hwndParent<\/code>\nparameter to ensure that we enable and disable the correct window\nat the correct time.\n<\/p>\n<pre>\n if (hwndParent == GetDesktopWindow())\n  hwndParent = NULL;\n if (hwndParent)\n  hwndParent = GetAncestor(hwndParent, GA_ROOT);\n HWND hdlg = CreateDialogIndirectParam(hinst,\n               lpTemplate, hwndParent, lpDlgProc,\n               lParam);\n BOOL fWasEnabled = EnableWindow(hwndParent, FALSE);\n<\/pre>\n<p>\nIn both cases, the first two &#8220;if&#8221; statements were missing.\nWe already saw\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/02\/24\/79212.aspx\">\nthe danger of disabling the desktop window<\/a>,\nwhich is what the first &#8220;if&#8221; statement protects against.\nBut the specific problem with modal UI was being caused by\nthe missing second &#8220;if&#8221; statement.\n<\/p>\n<p>\nBoth of the problems boiled down to somebody passing a child\nwindow as the <code>hwndParent<\/code> and the code doing\nmanual modal UI failing to convert this window to a top-level window.\nAs a result, when they did the <code>EnableWindow(hwndParent, FALSE)<\/code>,\nthey disabled a child window,\nleaving the top-level window enabled.\n<\/p>\n<p>\nThe two problems had the same root cause but manifested themselves\ndifferently.\nThe first problem led to strange behavior because\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/02\/23\/378866.aspx\">\nthe user could still interact\nwith the top-level window since it was still enabled<\/a>.\nSure, a portion of the window was disabled (the portion\ncontrolled by the child window passed as <code>hwndParent<\/code>),\nbut the caption buttons still worked, as did many of the other\ncontrols on the window.\n<\/p>\n<p>\nIn the second case,\ndisabling the wrong window created a different problem:\nWhen the modal UI was complete,\nthe window manager activated the top-level window that was the\nowner of the modal window since that window was never disabled.\nThis caused the top-level window to receive a <code>WM_ACTIVATE<\/code>\nmessage,\nwhich it handled by putting focus on the control that had focus\nwhen the top-level window was deactivated.\nUnfortunately, that window was the window that was passed\nas the <code>hwndParent<\/code>,\nwhich was <i>disabled by mistake<\/i>.\nThe attempt to restore focus failed,\nand when the manual modal UI finally finished up and enabled\nthe child window, it was too late.\nYou wound up with focus nowhere and a dead keyboard.\nThis second problem was\nreported as simply &#8220;<code>SetFocus<\/code> is not working.&#8221;\nOnly after peeling back a few layers (and application of some\npsychic powers) did the root cause emerge.\n<\/p>\n<p>\nNow, even though this was a subtle problem,\nyou already knew all the pieces that went into it since I had\ncovered them earlier.\nAnd as for those psychic powers that I used?\nIt&#8217;s really not that magic.\nIn this case of psychic debugging, I worked backwards.\nIn response to the report that\n<code>SetFocus<\/code> was not working,\nthe next set of questions was to determine why.\nIs it a valid window handle?\nDoes the window belong to your thread?\nIs it enabled?\n<\/p>\n<p>\nAha, the window isn&#8217;t enabled.\nThat&#8217;s when the customer also mentioned that they were doing\nthis inside a <code>WM_ACTIVATE<\/code> handler.\nIf you&#8217;re gaining activation, who were you gaining it from?\nOh, a modal dialog, you say?\nOne that you&#8217;re managing manually?\nOnce I discovered that they were trying to manage modal UI manually,\nI suspected that they were disabling the wrong window,\nsince that fit all the symptoms and it&#8217;s something that people tend\nto get wrong.\n<\/p>\n<p>\nMost of what looks like psychic debugging is really just\nknowing what people tend to get wrong.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago, I was asked to look at two independent problems with people trying to do modal UI manually. Well, actually, when the issues were presented to me, they weren&#8217;t described in quite that way. They were more along the lines of, &#8220;Something strange is happening in our UI. Can you help?&#8221; Only in [&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-29143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Some time ago, I was asked to look at two independent problems with people trying to do modal UI manually. Well, actually, when the issues were presented to me, they weren&#8217;t described in quite that way. They were more along the lines of, &#8220;Something strange is happening in our UI. Can you help?&#8221; Only in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/29143","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=29143"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/29143\/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=29143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=29143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=29143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}