12

I want to make a .NET Form as a TopMost Form for another external App (not .NET related, pure Win32) so it stays above that Win32App, but not the rest of the apps running.

I Have the handle of the Win32App (provided by the Win32App itself), and I've tried Win32 SetParent() function, via P/Invoke in C#, but then my .NET Form gets confined into the Win32App and that's not what I want.

3 Answers 3

19

I think you're looking for is to P/Invoke SetWindowLongPtr(win32window, GWLP_HWNDPARENT, formhandle)

Google Search

2
  • 3
    The problem is that I googled with the "change windows owner" word instead of searching for "change windows parent". :) Sep 25, 2008 at 14:00
  • 22
    I passed by this answer several times thinking it was answering the wrong question, until I read the following in the msdn docs: ""You must not call SetWindowLong with the GWL_HWNDPARENT index to change the parent of a child window. Instead, use the SetParent function." This statement is misleading. GWL_HWNDPARENT changes the OWNER, not the parent, of a window. It is safe to use for that purpose. "
    – bj0
    Jun 17, 2013 at 22:57
2

Yes! I've already have a P/Invoke import of SetWindowLongPtr (which is x64 safe). And using Reflector I searched upon the Form.Owner property ( i.e. the get_Owner(Form value) method ) and managed to change the owner with

SetWindowLongPtr(childHdl, -8, OwnerHdl)

I was looking what the -8 (0xFFFFFFFFFFFFFFF8) meant before I could post the solution here, but Joel has already pointed it out.

Thanks!

-3

It has now been 12 years since this question was asked so I thought I would provide an updated answer from here.

Do not call SetWindowLongPtr with the GWLP_HWNDPARENT index to change the parent of a child window. Instead, use the SetParent function.

1
  • 1
    Please read the question again. It is not about changing the parent of a child window. It is about changing the owner of another top level window. These are two completely different operations. As explained in the comment of the currently accepted answer, SetWindowLongPtr(..., GWLP_HWNDPARENT, ...) is the correct way to change the owner handle.
    – whY
    Oct 5, 2020 at 14:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.