Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach form window to another window in C#

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.

  1. Now I need to find out whether this can be written in a stable way, appearing on Win 7 as well.
  2. Nevertheless, I still cannot click any buttons on my form, needs to be solved too.

WinXP WinForm attached to notepad

like image 533
Horst Walter Avatar asked May 27 '12 09:05

Horst Walter


People also ask

How do I import one Windows form into another?

Now go to Solution Explorer and select your project and right-click on it and select a new Windows Forms form and provide the name for it as form2. Now click the submit button and write the code. When you click on the submit button a new form will be opened named form2.


2 Answers

Here is a working example. The hosting app is a simple WinForms application with a blank form (not included here), while the "guest app" has a main form (code behind included below) with some controls, including a test button to display a message after changing the guest form's parent.

The usual caveats linked to in the OP's question apply to this, too.

public partial class GuestForm: Form
{
  [DllImport("user32.dll")]
  public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

  [DllImport("user32.dll")]
  public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

  [DllImport("user32.dll", SetLastError = true)]
  private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

  public static int GWL_STYLE = -16;
  public static int WS_CHILD = 0x40000000; 

  public GuestForm()
  {
    InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {
    MessageBox.Show("done");
  }

  private void button2_Click(object sender, EventArgs e)
  {
    Process hostProcess = Process.GetProcessesByName("HostFormApp").FirstOrDefault();
    if (hostProcess != null)
    {
      Hide();
      FormBorderStyle = FormBorderStyle.None;
      SetBounds(0, 0, 0, 0, BoundsSpecified.Location);

      IntPtr hostHandle = hostProcess.MainWindowHandle;
      IntPtr guestHandle = this.Handle;
      SetWindowLong(guestHandle, GWL_STYLE, GetWindowLong(guestHandle, GWL_STYLE) | WS_CHILD);
      SetParent(guestHandle, hostHandle);

      Show();
    }
  }
}
like image 142
Alan Avatar answered Oct 03 '22 02:10

Alan


@Horst Walter Hey man, I'm not sure if you've fixed the issue, but I just found a solution to this..

For me the issue was the transparency of the main form you want inside the other form.

Just disable transparency and it should work.

like image 40
Red Fane Avatar answered Oct 03 '22 00:10

Red Fane