{"id":11083,"date":"2011-03-31T07:00:00","date_gmt":"2011-03-31T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/03\/31\/having-an-owner-window-from-another-process-is-tricky-but-its-sometimes-the-right-thing-to-do\/"},"modified":"2011-03-31T07:00:00","modified_gmt":"2011-03-31T07:00:00","slug":"having-an-owner-window-from-another-process-is-tricky-but-its-sometimes-the-right-thing-to-do","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110331-00\/?p=11083","title":{"rendered":"Having an owner window from another process is tricky, but it&#039;s sometimes the right thing to do"},"content":{"rendered":"<p>A customer had a main program (let&#8217;s call it A) and a helper program (let&#8217;s call it B), and the customer wanted and wanted B to act like a modal dialog relative to A.<\/p>\n<blockquote class=\"q\"><p>  When B is launched, we disable A&#8217;s&nbsp;window and then call <code>SetForegroundWindow(hwndB)<\/code> to simulate a modal dialog. How do we make sure that focus goes to B&#8217;s&nbsp;window and not A&#8217;s? We&#8217;ve found that if the user clicks on the (now-disabled) window from the process&nbsp;A, then window&nbsp;B loses focus. This is not the behavior from regular modal windows however: For normal modal windows, clicking on the disabled owner activates the modal popup. <\/p>\n<p> One idea is to watch for <code>WM_ACTIVATE(FALSE)<\/code> notifications on <code>hwndSecondProcess<\/code>, and if the window that took focus from us is the one from the first process, then take it back with SetForegroundWindow(hwndSecondProcess). <\/p>\n<p> But then we wondered, since we disabled window&nbsp;A, will it even get the normal activation message? <\/p>\n<\/blockquote>\n<p> Since the window is disabled, it will not receive activation messages because disabled windows cannot be activated. So no, this solution won&#8217;t work.\n The subject line of the question, however, gave the answer without even realizing it. The subject was <i>Out-of-proc pseudo-parent\/child window relationship<\/i>. (Well, okay, the subject line <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/03\/15\/9978691.aspx\"> confused parent\/child with owner\/owned<\/a>, but that&#8217;s a common source of sloppiness when talking about the relationship among windows.)\n Instead of having a pseudo-owner\/owned window relationship, just have a real one. Why fake it when you can get the real thing?<\/p>\n<p> When you call <code>DialogBox<\/code> in process&nbsp;B, pass <code>hwndA<\/code> as the owner window. Now the two windows have a genuine owner\/owned relationship, along with the standard behaviors that come with it. It&#8217;s legal to have an owner\/owned relationship that crosses process boundaries. Note that when you do this, it <a href=\"https:\/\/channel9.msdn.com\/posts\/scobleizer\/Raymond-Chen-PDC-05-Talk-Five-Things-Every-Win32-Programmer-Needs-to-Know\/\"> attaches the two threads&#8217; input queues<\/a> so you have to be careful if both windows process input at the same time. Fortunately, in the modal dialog case, only one of the windows accepts input at a time, so the scariest part of attached input queues doesn&#8217;t apply. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer had a main program (let&#8217;s call it A) and a helper program (let&#8217;s call it B), and the customer wanted and wanted B to act like a modal dialog relative to A. When B is launched, we disable A&#8217;s&nbsp;window and then call SetForegroundWindow(hwndB) to simulate a modal dialog. How do we make sure [&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-11083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer had a main program (let&#8217;s call it A) and a helper program (let&#8217;s call it B), and the customer wanted and wanted B to act like a modal dialog relative to A. When B is launched, we disable A&#8217;s&nbsp;window and then call SetForegroundWindow(hwndB) to simulate a modal dialog. How do we make sure [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/11083","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=11083"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/11083\/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=11083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=11083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=11083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}