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.
To display a form modally use the ShowDialog method.
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.
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.
In WinForms, you do that by setting the FormBorderStyle property to one of the following: FormBorderStyle. FixedSingle , FormBorderStyle. Fixed3D , or FormBorderStyle.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With