11

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.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Ricardo Amores
  • 4,597
  • 1
  • 31
  • 45

3 Answers3

18

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

Google Search

Paul Groke
  • 6,259
  • 2
  • 31
  • 32
Joel Lucsy
  • 8,520
  • 1
  • 29
  • 35
  • 3
    The problem is that I googled with the "change windows owner" word instead of searching for "change windows parent". :) – Ricardo Amores Sep 25 '08 at 14:00
  • 21
    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 '13 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!

Ricardo Amores
  • 4,597
  • 1
  • 31
  • 45
-2

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.

Jason Brower
  • 27
  • 1
  • 4
  • 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 05 '20 at 14:54