none
Winform has no parent? RRS feed

  • Question

  • I have a mixture of native and .NET in an application. We create a native window and give it to a client. In some cases, the client is writing code using .NET. Sometimes we need to get parent windows using GetParent. I have found that I can get the parent of a .NET edit control and it is a WinForm. When I call GetParent using the WinForm handle, GetParent returns 0.

    Using Spyxx.exe to examine the windows for the WinForm, Spyxx indicates the WinForm has no parent nor an owner window.

    But, I know the native window handle. If I call GetWindow(xxx, GW_CHILD), I get back the WinForm handle. This has complicated the code as now I can't just walk the parentage.

    Fortunately for me, Spyxx walks top-down, not bottom up so I could see my native window has the Winform as a child (even though Spyxx shows the WinForm has noparent).

    I have the .NET code and after obtaining our parent window, the code uses PInvoke to call the Windows SetParent API. That's what allows Spyxx (and now me) to see the Winform as a child, but not the parent window as the parent of the WinForm.

    Why the disconnect? And how do I know a.) I have a Winform (probably wpf too or whatever MS is telling .NET users to use now) and 2.) How do I find its parent?


    R.D. Holland

    Tuesday, December 3, 2019 7:12 PM

Answers

  • When you call SetParent on the WinForm handle do you also set the WS_CHILD style and clear any other styles that are inapplicable for a child window?
    • Marked as answer by RD Holland Wednesday, December 4, 2019 4:08 PM
    Tuesday, December 3, 2019 10:24 PM

All replies

  • Did you also investigate GetWindow(formHandle, GW_OWNER), GetParent(formHandle) and GetAncestor(formHandle, GA_PARENT)?

    Tuesday, December 3, 2019 9:10 PM
  • When you call SetParent on the WinForm handle do you also set the WS_CHILD style and clear any other styles that are inapplicable for a child window?
    • Marked as answer by RD Holland Wednesday, December 4, 2019 4:08 PM
    Tuesday, December 3, 2019 10:24 PM
  • Hi RD Holland,
    The GetParent function retrieves a handle to the specified window's parent or owner.
    If you want to obtain the parent window and not the owner,you can use GetAncestor with the GA_PARENT flag.
    Beside, there are some reasons that return value is null when using the GetParent function.
    1.The window is a top-level window that is unowned or does not have the WS_POPUP style.
    2.The owner window has WS_POPUP style.
    More details you can refer to this document.
    [GetParent function]
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 4, 2019 5:25 AM
  • Hi Viorel,

    No, I did not make all those calls since Spyxx showed the window had no owner and I assumed GetParent and GetAncester with GA_PARENT were essentially the same call.

    Just in case, I added those calls to see if I get something Spyxx did not. The result was null in each case. No parent, no owner, no ancestor parent.


    R.D. Holland

    Wednesday, December 4, 2019 3:05 PM
  • Hi RLWA32,

    Since I have the sample c# project, I looked at the code. The code just creates a new Form and calls SetParent using my native window handle as the parent. Spyxx does show there is no WS_CHILD style set. I opened the Form and examined all the properties. Unlike native windows created in Visual, I found no style setting that would let me mark the form as a child window. I also used the Browser to examine the Form and its parent and only found a StyleChanged event. The styles I can set programmatically are the FormBorderStyle and the enum doesn't include a child style. There is a GripStyle but it is not relevant either. Other than those two, it is all MDI related and I don't think I want to mark this form as an MDI child (we are an MDI app but those windows host our documents).

    I'll see if I can PInvoke a style change.


    R.D. Holland

    Wednesday, December 4, 2019 3:19 PM
  • Hi (again) RLWA32,

    Well it seems awful complicated to do such a simple thing but I managed to PInvoke GetWindowLongPtr and SetWindowLongPtr. I set the parent via PInvoke and then I get the style, add WS_CHILD to it and set it.

    That fixes the issue so my original code works as is. I'll let the vendor know they can do this if they don't want to wait for my fix to handle this situation (I walk top down on all my client windows when GetParent returns zero getting the child window of each client I have and comparing to the handle of the winform).

    Thanks. .NET interop is always so much fun.


    R.D. Holland

    Wednesday, December 4, 2019 4:08 PM
    • Edited by RLWA32 Wednesday, December 4, 2019 4:26 PM
    Wednesday, December 4, 2019 4:25 PM