{"id":23813,"date":"2008-01-15T10:00:00","date_gmt":"2008-01-15T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/01\/15\/use-wm_windowposchanged-to-react-to-window-state-changes\/"},"modified":"2008-01-15T10:00:00","modified_gmt":"2008-01-15T10:00:00","slug":"use-wm_windowposchanged-to-react-to-window-state-changes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080115-00\/?p=23813","title":{"rendered":"Use WM_WINDOWPOSCHANGED to react to window state changes"},"content":{"rendered":"<p>\nThe documentation for the <code>WM_SHOWWINDOW<\/code> message\npoints out that the message is not sent under certain circumstances.\nBut what if you want to know when the window is shown,\nincluding in the cases where you don&#8217;t get <code>WM_SHOWWINDOW<\/code>?\n<\/p>\n<p>\nThe <code>WM_WINDOWPOSCHANGED<\/code> message is sent at the end of the\nwindow state change process.\nIt sort of combines the other state change notifications,\n<code>WM_MOVE<\/code>,\n<code>WM_SIZE<\/code>, and\n<code>WM_SHOWWINDOW<\/code>.\nBut it doesn&#8217;t suffer from the same limitations\nas <code>WM_SHOWWINDOW<\/code>,\nso you can reliably use it to react to the window being shown or hidden.\nThe handler would go something like this:\n<\/p>\n<pre>\nvoid OnWindowPosChanged(HWND hwnd, const WINDOWPOS *pwp)\n{\n    if (pwp-&gt;flags &amp; SWP_SHOWWINDOW) {\n       window_was_shown();\n    }\n    if (pwp-&gt;flags &amp; SWP_HIDEWINDOW) {\n       window_was_hidden();\n    }\n    if (!(pwp-&gt;flags &amp; SWP_NOMOVE)) {\n       window_moved_to(pwp-&gt;x, pwp-&gt;y);\n    }\n    if (!(pwp-&gt;flags &amp; SWP_NOSIZE)) {\n       window_resized_to(pwp-&gt;cx, pwp-&gt;cy);\n    }\n}\nHANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, OnWindowPosChanged);\n<\/pre>\n<p>\nNote also that if you don&#8217;t pass the <code>WM_WINDOWPOSCHANGED<\/code>\nmessage to <code>DefWindowProc<\/code>, then you won&#8217;t get\n<code>WM_MOVE<\/code> or <code>WM_SIZE<\/code> messages, since it is\n<code>DefWindowProc<\/code> that converts <code>WM_WINDOWPOSCHANGED<\/code>\ninto the <code>WM_MOVE<\/code> and <code>WM_SIZE<\/code> messages.\n<\/p>\n<p>\n&#8220;If <code>WM_WINDOWPOSCHANGED<\/code> is redundant with\n<code>WM_MOVE<\/code>,\n<code>WM_SIZE<\/code>, and\n<code>WM_SHOWWINDOW<\/code>,\nthen why do we have those other messages anyway?&#8221;\n<\/p>\n<p>\nThe <code>WM_WINDOWPOSCHANGED<\/code> message wasn&#8217;t invented until\nWindows&nbsp;3.1.\nPrior to that, you had no choice but to react to those other messages.\nYou can think of those other three messages as legacy messages now.\nThere&#8217;s nothing wrong with them, but they&#8217;re kind of old-fashioned now.\n<\/p>\n<p>\nNext time, we&#8217;ll look at the companion message\n<code>WM_WINDOWPOSCHANGING<\/code>.\n<\/p>\n<p>\nPostscript:\nThis entry was inspired by an actual customer question\nregarding the cases where <code>WM_SHOWWINDOW<\/code> message is not sent\nif the program is run with the <code>SW_SHOWMAXIMIZED<\/code> state.\nUnfortunately, one detail I missed in the customer&#8217;s question was the\nremark that they need to know when the window is shown because it is\n&#8220;critical for the application to initialize its state.&#8221;\nI didn&#8217;t follow up on that little remark, but I should have,\nbecause it&#8217;s very strange to do initialization work when a window\nis shown.\nWhat if the window is never shown?\nDoes this mean that the program will never initialize itself?\n(For example, somebody might have run your program with the\n<code>SW_HIDE<\/code> state.)\nThe <code>WM_NCCREATE<\/code> and <code>WM_CREATE<\/code> are the\nmore traditional places to do window initialization.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The documentation for the WM_SHOWWINDOW message points out that the message is not sent under certain circumstances. But what if you want to know when the window is shown, including in the cases where you don&#8217;t get WM_SHOWWINDOW? The WM_WINDOWPOSCHANGED message is sent at the end of the window state change process. It sort of [&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-23813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The documentation for the WM_SHOWWINDOW message points out that the message is not sent under certain circumstances. But what if you want to know when the window is shown, including in the cases where you don&#8217;t get WM_SHOWWINDOW? The WM_WINDOWPOSCHANGED message is sent at the end of the window state change process. It sort of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/23813","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=23813"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/23813\/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=23813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=23813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=23813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}