Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SetWindowLong and GWL_HWNDPARENT

1,751 views
Skip to first unread message

Bob

unread,
Nov 9, 1999, 3:00:00 AM11/9/99
to
I found the following information on the MSDN website:

"You must not call SetWindowLong with the GWL_HWNDPARENT index to change the
parent of a child window. Instead, use the SetParent function. Instead, use
the SetParent function."

Unfortunately, there is no further explanation. Can anyone provide more
details on this?

I'm calling SetWindowLong with the GWL_HWNDPARENT index in one of my
applications because the child window can be placed outside the parent form.
When using SetParent, the child form stays inside the parent form.

Thanks, Bob

Tom Esh

unread,
Nov 9, 1999, 3:00:00 AM11/9/99
to
>"You must not call SetWindowLong with the GWL_HWNDPARENT index to change the
>parent of a child window. Instead, use the SetParent function. Instead, use
>the SetParent function."
(In)famous misleading statement. Almost as misleading as the choice of
GWL_HWNDPARENT as the name. It has nothing to do with a window's
parent. It really changes the Owner, which (in VB5 or later) is
exactly the same thing as including the Owner argument in the Show
statement.
A more accurate version might be..
"SetWindowLong with the GWL_HWNDPARENT will not change the parent of a
child window. Instead, use the SetParent function. GWL_HWNDPARENT
should have been called GWL_HWNDOWNER, but nobody noticed it until
after a bazillion copies of the SDK had gone out. This is what happens
when the the dev team lives on M&Ms and CocaCola for to long. Too bad.
Live with it."
<g>

-Tom
(remove gibberish for e-mail repl)

chaos....@gmail.com

unread,
Sep 8, 2012, 4:30:54 AM9/8/12
to
1999年11月9日火曜日 17時00分00秒 UTC+9 Bob:
> I found the following information on the MSDN website:
>
> "You must not call SetWindowLong with the GWL_HWNDPARENT index to change the
> parent of a child window. Instead, use the SetParent function. Instead, use
> the SetParent function."
>
> Unfortunately, there is no further explanation. Can anyone provide more
> details on this?
>
> I'm calling SetWindowLong with the GWL_HWNDPARENT index in one of my
> applications because the child window can be placed outside the parent form.
> When using SetParent, the child form stays inside the parent form.
>
> Thanks, Bob

According to MS, there's no API to change owner after window has appeared.
You know it is possible to change by using setWindowLong with GWL_HWNDPARENT.
But, it is not supported feature, and you should not use setWindowLong for the purpose of changing window's owner.
Only one way to set window's owner is use CreateWindowEx with set Instance of owner at 8th of parameter.
You can set owner at creating window.
That's all.

Larry

unread,
Aug 5, 2022, 9:11:47 AM8/5/22
to
(Responding many years later but for the benefit of anyone coming across this):

Agreed, changing the owner via "SetWindowLong()" (superceded by "SetWindowLongPtr()") using GWL_HWNDPARENT is a hack since it's not officially documented by MSFT. Only the "Get" versions of these functions are. However, Raymond Chen (well known MSFT guru) actually does this on slide 32 of his "Five Things Every Win32 Developer Should Know" presentation. Copied his code verbatim below but here's one link to the page at this writing (or just search for presentation if link has gone dead):

https://image.slidesharecdn.com/fun412chen-120811232150-phpapp01/95/five-things-every-win32-developer-should-know-32-728.jpg?cb=1344727818

HWND SetWindowOwner(HWND hwnd, HWND hwndOwner)
{
assert(!(GetWindowStyle(hwnd) & WS_CHILD));
assert(!(GetWindowStyle(hwndOwner) & WS_CHILD));
return (HWND)SetWindowLongPtr(hwnd, GWLP_HWNDPARENT, (LONG_PTR)hwndOwner);
}

Since Chen uses it (and posted it for the world to see), then in all likelihood it's probably safe (reliable). Still, use at your own risk though (I just started to in order to deal with a very specific situation I'm dealing with and so far so good - no other documented solution I could find unfortunately)
0 new messages