I want to attach a form to another window (of another process). I try to do this by using
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
setParentWindow(myWindowHwnd, newParentHwnd);
In doing so my form becomes attached, but is also invisible. Question "Attach window .." solves this issue for a WPF Window, basically by using
HwndSourceParameters parameters = new HwndSourceParameters();
...
HwndSource src = new HwndSource(parameters);
I have tried to transfer this to my form, but I am unable to do so (e.g. how to handle src.RootVisual = (Visual)window.Content;
? -> Complete source).
Another comment says, I need to modify the windows style:
For compatibility reasons, SetParent does not modify the WS_CHILD or WS_POPUP window styles of the window whose parent is being changed. Therefore, if hWndNewParent is NULL, you should also clear the WS_CHILD bit and set the WS_POPUP style after calling SetParent. Conversely, if hWndNewParent is not NULL and the window was previously a child of the desktop, you should clear the WS_POPUP style and set the WS_CHILD style before calling SetParent.
Here I miss the corresponding API for doing so, can I do it directly from C# or have I to use another DllImport
again?
Good or evil - SetParent() win32 API between different processes advises against attaching windows in different processes at all, but at least I want to try.
Question:
What would I need to do to get the form window visible? If the approach with WS_Child
is the correct one, how would I set it? Or is the WPF approach the way to go, but how would I apply it to an windows form?
-- Findings (later added) --
Modify the windows style of another application using winAPI shows how to modify the style from C# / PInvoke
Find all windows styles here, C# syntax at the bottom.
-- Findings due to discussion with Alan --
I did run my program on Win XP to crosscheck (see Alan's answer below and the comments). At least I do now see something. Since I have added the coordinates as of Alan's examples, my window now shines through in notepad when moving over the other window near the left upper corner. You can still see the text typed in notepad as overlay. Under Win 7 (32) I do see nothing at all.
- Now I need to find out whether this can be written in a stable way, appearing on Win 7 as well.
- Nevertheless, I still cannot click any buttons on my form, needs to be solved too.