{"id":2913,"date":"2013-10-16T07:00:00","date_gmt":"2013-10-16T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/10\/16\/why-does-my-window-get-a-wm_activate-message-when-it-isnt-active\/"},"modified":"2013-10-16T07:00:00","modified_gmt":"2013-10-16T07:00:00","slug":"why-does-my-window-get-a-wm_activate-message-when-it-isnt-active","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20131016-00\/?p=2913","title":{"rendered":"Why does my window get a WM_ACTIVATE message when it isn&#039;t active?"},"content":{"rendered":"<p>Say you launch a program, and for whatever reason the program takes a long time to start up, so you start doing something else, say launching Calculator and balancing your checkbook. Eventually, the program you launched a while back gets itself off the ground and creates its main window. And the window sits in the background (since the window manager won&#8217;t let it steal foreground activation), but the caret is blinking in the edit control, and the program seems to think it&#8217;s the active window.\n If you write a test program to do this, say by sticking a <code>Sleep(10000)<\/code> at the start of your <code>WinMain<\/code>, you&#8217;ll see that your window gets a <code>WM_ACTIVATE<\/code> message that says &#8220;Yup, you&#8217;re active.&#8221;\n But you&#8217;re not.\n Or are you?\n What&#8217;s going on here?\n Let&#8217;s rewind to 16-bit Windows. The active window was the top-level window that receives input. You could make a window active by clicking on it or by selecting it via <kbd>Alt<\/kbd>+<kbd>Tab<\/kbd> or any number of other operations. A program could change the active window by calling the <code>SetActiveWindow<\/code> function explicitly, or by performing a number of other operations which imply changing the active window. (For example, depending on the parameters you pass, <code>ShowWindow<\/code> and <code>SetWindowPos<\/code> may make the window active as well as showing\/repositioning it.) There was only one active window at a time; if a program set itself as the active window, then the previous active window lost activation.\n Okay, now let&#8217;s move forward to Win32. Recall that in Win32, most of the state that used to be global became thread-local.&sup1; This was done to permit the asynchronous input model, where each thread gets its own input queue. This means <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2009\/12\/17\/9937972.aspx\"> each thread gets its own mouse cursor show state<\/a>, each thread gets its own caret, each thread gets its own focus window, and so on. And one of the and-so-on bits is that each thread gets its own active window.\n What you&#8217;re seeing is a thread which has an <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/07\/20\/10040074.aspx#10040488\"> active window which is not the foreground window<\/a>. The thread also has a focus window, and when an edit control gets the focus, it draws a blinky caret. Mind you, the window is not the <i>foreground<\/i> window, so your input doesn&#8217;t actually go to it, but the window doesn&#8217;t know that. It&#8217;s sitting around as an active window, wondering why nobody is typing anything.\n So now you know what&#8217;s going on. Mind you, there&#8217;s nothing actually wrong with this situation. In fact, it&#8217;s a sign that the virtualization is doing what it&#8217;s supposed to do: The thread is living in its own world, a world designed to be compatible with the 16-bit world where there was only one active window.\n <b>Footnote<\/b><\/p>\n<p>&sup1;&thinsp;Or, more nitpickily, local to the input thread group. Since most input thread groups consist of a single thread, I&#8217;ll just write thread and leave you to insert &#8220;or input thread group&#8221; as necessary. But you knew that, because it&#8217;s one of the five things every Win32 programmer should know. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Say you launch a program, and for whatever reason the program takes a long time to start up, so you start doing something else, say launching Calculator and balancing your checkbook. Eventually, the program you launched a while back gets itself off the ground and creates its main window. And the window sits in 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-2913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Say you launch a program, and for whatever reason the program takes a long time to start up, so you start doing something else, say launching Calculator and balancing your checkbook. Eventually, the program you launched a while back gets itself off the ground and creates its main window. And the window sits in the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2913","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=2913"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2913\/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=2913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=2913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=2913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}