Nikolay
Nikolay

Reputation: 587

Set owner to form from another process form

I have a Form (parent). Also I have handle to the Form (child) from another process. I need to always show the child form on top of the parent and was possible to work with the main form.

It works when we call:

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    private void MainForm_Shown(object sender, EventArgs e)
    {
        var childForm = new Form();
        childForm.Show(this);
    }
}

Also, if the main form closes, the child form from another process will continue to work.

I try used pinvoke SetParent() function, but it set child form as MDI.

Upvotes: 1

Views: 2100

Answers (1)

David Heffernan
David Heffernan

Reputation: 613311

Call SetWindowLongPtr passing GWL_HWNDPARENT as the index.

SetWindowLongPtr(OwnedWnd, GWL_HWNDPARENT, OwnerWnd);

You may need to manually bring the owned window in front of the owner window in the z-order, because simply calling the function above will not make that happen.

Upvotes: 4

Related Questions