Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I work around the GetParent/EnumChildWindows asymmetry?
    text
    copied!<p>I recently inspect a GUI with Microsoft's Spy++ and noticed a strange structure; it looked like this (warning, ASCII art ahead):</p> <pre> | + 002004D6 "MyRootWindow1" FooClassName | | | + 001F052C "MyChildWindow" ClassOfChildWindow | \ 001D0A8C "MyRootWindow2" SomeOtherClassName </pre> <p>There are two root windows, <code>002004D6</code> and <code>001D0A8c</code>, the former one of which has one child window, <code>001F052C</code>.</p> <p>Now, this would be all good and find if it wasn't for one thing: calling <a href="http://msdn.microsoft.com/en-us/library/ms633510%28VS.85%29.aspx" rel="nofollow noreferrer">GetParent</a> (or watching the 'Parent Window' or 'Owner Window' fields in Spy++) on the child window (<code>001F052C</code>) yields <code>001D0A8C</code>.</p> <p>Read: "MyChildWindow" is a child of "MyRootWindow1", but "MyRootWindow1" is not the parent of "MyChildWindow". Instead, the parent of "MyChildWindow" is "MyRootWindow2" - but, to make this complete, enumerating the children of "MyRootWindow2" does not yield "MyChildWindow".</p> <p>This is a perfectly static GUI applications, so there are no race conditions here or anything.</p> <p>Does anybody know how this can happen? Does anybody know how I can work around this? Until now, I used <a href="http://msdn.microsoft.com/en-us/library/ms633510%28VS.85%29.aspx" rel="nofollow noreferrer">GetParent</a> and <a href="http://msdn.microsoft.com/en-us/library/ms633494%28VS.85%29.aspx" rel="nofollow noreferrer">EnumChildWindows</a> to get the parent (or children) for a given HWND, and I assumed that this relationship is symmetrical. Is there maybe something else I should be using?</p> <p><b>EDIT:</b> Here's the code for a small C++ program which demonstrates the problem:</p> <pre><code>const HINSTANCE thisModule = ::GetModuleHandle( NULL ); HWND oldParent = ::CreateWindow( TEXT("STATIC"), TEXT("Old parent"), WS_VISIBLE | WS_BORDER, 0, 0, 850, 500, NULL, NULL, thisModule, NULL ); HWND child = ::CreateWindow( TEXT("STATIC"), TEXT("This is a sample dialog"), WS_OVERLAPPED | WS_POPUP | WS_VISIBLE | WS_BORDER, 100, 100, 300, 300, oldParent, NULL, thisModule, NULL ); HWND newParent = ::CreateWindow( TEXT("STATIC"), TEXT("Fake main window"), WS_VISIBLE | WS_BORDER, 0, 0, 850, 500, NULL, NULL, thisModule, NULL ); ::SetParent( child, newParent ); </code></pre> <p>Note how the 'child' object has <code>WS_POPUP</code> <em>and</em> <code>WS_OVERLAPPED</code> set, but not <code>WS_CHILD</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload