Can I change the owner of a window?

Holland, RD (DI SW ME PRD SDE AS) 36 Reputation points
2021-02-04T21:39:05.343+00:00

I have a WS_POPUP window owned by my main frame window. I need to be able to change the owner of the window. I use CreateEx to create the window and pass in the initial owner when I make that call. I can easily change parents of a window, including one that has the WS_POPUP style instead of WS_CHILD.

But, I have not been able to find a way to change the owner. Is there a way to do this?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,650 questions
{count} votes

Accepted answer
  1. Castorix31 85,711 Reputation points
    2021-02-05T07:31:44.297+00:00

    Change the Tag : this is not UWP, but Win32...

    You can use GWL_HWNDPARENT with SetWindowLong to change the owner but it is not recommended

    I did some tests and it uses SetParent instead between childs or it even crashed my OS (Windows 10) when I set an Edit control as owner of a Popup window...


1 additional answer

Sort by: Most helpful
  1. Drake Wu - MSFT 991 Reputation points
    2021-02-05T08:41:53.803+00:00

    Hi, @Holland, RD (DI SW ME PRD SDE AS) A window can have a parent or an owner but not both:

    A window can be created as a child window (WS_CHILD set) or a top-level window (WS_CHILD not set). A child window has a parent, which you specify when you call CreateWindowEx, and which you can change by calling SetParent. A top-level window, on the other hand, has no parent. Its parent is NULL.

    Ownership is a concept that relates top-level windows. A top-level window can optionally have an owner, which is also specified when you call CreateWindowEx, and which you can change by a complicated mechanism described in my talk.

    Note that changing a window’s parent or owner is not a normal operation. You usually create a window with a specific parent or owner and leave it that way for the lifetime of the window.

    Now, a window can have a parent, or it can have an owner, or it can have neither, but it can never have both.

    Or Could you share the use case you need to set owner?


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.