Ricardo Amores
Ricardo Amores

Reputation: 4687

How to change a Window Owner using its handle

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.

Upvotes: 13

Views: 13552

Answers (3)

Jason Brower
Jason Brower

Reputation: 25

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.

Upvotes: -3

Joel Lucsy
Joel Lucsy

Reputation: 8706

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

Google Search

Upvotes: 20

Ricardo Amores
Ricardo Amores

Reputation: 4687

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!

Upvotes: 2

Related Questions