Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:14:33+00:00 2026-06-04T19:14:33+00:00

I want to attach a form to another window (of another process). I try

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:14:34+00:00Added an answer on June 4, 2026 at 7:14 pm

    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();
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, what I want is basically to attach a javascript handler to a form,
Let's say I open a form and want to attach a command to it
If I want to fetch a form, then grab some button, then attach some
I want to attach a submit() handler to a form to run an ajax
I'm using Inline Form Validation Engine to validate a form. Now I want to
I have a problem with dojox.form.Uploader. I want to use it to attach files
I want to attach .vcf file with my mail and send through the mail.
i have a Button on a HTML page . i want to attach file
want to know why String behaves like value type while using ==. String s1
say i want to attach errors to my Zend_Form i found out the default

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.