{"id":102411,"date":"2019-04-11T07:00:00","date_gmt":"2019-04-11T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102411"},"modified":"2019-06-06T17:42:24","modified_gmt":"2019-06-07T00:42:24","slug":"20190411-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190411-00\/?p=102411","title":{"rendered":"How can I determine the reason why my window is closing?"},"content":{"rendered":"<p>A customer wanted to know whether their MFC program can determine why their window is closing, similar to <a HREF=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.windows.forms.closereason\">how WinForms does<\/a>. <\/p>\n<p>The source code for WinForms is online. You can see how they do it, and then translate that to MFC. <\/p>\n<p>Many of the <code>Close&shy;Reason<\/code> values refer to actions that occurred within WinForms itself, so those are naturally generated by WinForms. Three of the reasons are external to WinForms. <\/p>\n<ul>\n<li><code>User&shy;Closing<\/code>:     This is generated     <a HREF=\"https:\/\/referencesource.microsoft.com\/#System.Windows.Forms\/winforms\/Managed\/System\/WinForms\/Form.cs,7406\">    in response to     the <code>WM_<\/code><code>SYS&shy;COMMAND<\/code>     when the code is <code>SC_<\/code><code>CLOSE<\/code><\/a>.     This happens when the user closes the window     by clicking the &times; in the upper right corner,     or by double-clicking the system icon,     or by selecting <i>Close<\/i> from the system menu. <\/li>\n<li><code>Windows&shy;Shut&shy;Down<\/code>:     This is generated     <a HREF=\"https:\/\/referencesource.microsoft.com\/#System.Windows.Forms\/winforms\/Managed\/System\/WinForms\/Form.cs,7544\">    in response to the <code>WM_<\/code><code>QUERY&shy;END&shy;SESSION<\/code>     and     <code>WM_<\/code><code>END&shy;SESSION<\/code>     messages<\/a>. <\/li>\n<li><code>Task&shy;Manager&shy;Closing<\/code>:     This is generated     <a HREF=\"https:\/\/referencesource.microsoft.com\/#System.Windows.Forms\/winforms\/Managed\/System\/WinForms\/Form.cs,7537\">    in response to the <code>WM_<\/code><code>CLOSE<\/code> message<\/a>,     provided it wasn&#8217;t already set by someone else with better information. <\/li>\n<\/ul>\n<p>The &#8220;provided it wasn&#8217;t already set by someone else with better information&#8221; is important, because many of the window closing scenarios lead to <code>WM_<\/code><code>CLOSE<\/code>. For example, the default handling for the <code>SC_<\/code><code>CLOSE<\/code> system menu command is to send the <code>WM_<\/code><code>CLOSE<\/code> message, so you will see the <code>SC_<\/code><code>CLOSE<\/code> first, followed by the <code>WM_<\/code><code>CLOSE<\/code> message. <\/p>\n<p>Note that <code>Task&shy;Manager&shy;Closing<\/code> is inferred by the fact a <code>WM_<\/code><code>CLOSE<\/code> message arrives without any of the known preliminaries. While it&#8217;s true that Task Manager uses the <code>WM_<\/code><code>CLOSE<\/code> message to encourage an app to exit, it&#8217;s not the only program that does it. <\/p>\n<p>A better name might be <code>External<\/code> or <code>Programmatic<\/code>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Listen for signals.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-102411","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Listen for signals.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102411","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=102411"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102411\/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=102411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}