Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms: Show form modal over other application's main window

Is it possible to show a WinForms modal form over another process's main window?

For example my WinForms application consists of one form which is modal over another process's main window with PID x.

like image 821
Harry13 Avatar asked Feb 20 '13 10:02

Harry13


People also ask

How do you show a system Windows Forms form Myform as a modal dialog?

To display a form modally use the ShowDialog method.

How do I open another Windows form?

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.

How do you display a form in C#?

Press F5 to build and run the application. Click on the button in the main form to display the sub form. Now, when you press the button in the sub form, the form will be hidden.

How do I turn off WinForms full screen?

In WinForms, you do that by setting the FormBorderStyle property to one of the following: FormBorderStyle. FixedSingle , FormBorderStyle. Fixed3D , or FormBorderStyle.


1 Answers

You can show it as a dialog, like so:

Form1 frm = new Form1();
frm.ShowDialog(this);
frm.Dispose();

You pass the current IWin32Window or form you want to be the owner, so if you're calling it from say a button click on the parent form, just pass through this.

You want to be able to get the IWin32Window for another process, which is possible, but I don't know if showing a form as a modal over that is.

var proc = Process.GetProcesses().Where(x => x.ProcessName == "notepad").First();
IWin32Window w = Control.FromHandle(proc.MainWindowHandle);

using (Form1 frm = new Form1())
{
    frm.ShowDialog(w);
}

This is how it would work, if it was possible, however, it doesn't seem to work for me.

This link may shed a bit more information on the subject: How can I make a child process window to appear modal in my process?

like image 130
Adam K Dean Avatar answered Oct 06 '22 17:10

Adam K Dean