C# WinForms:在其他应用程序的主窗口上显示表单模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14977076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:38:01  来源:igfitidea点击:

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

c#winformsmodal-dialog

提问by Harry13

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

是否可以在另一个进程的主窗口上显示 WinForms 模式窗体?

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

例如,我的 WinForms 应用程序包含一个窗体,该窗体在另一个进程的主窗口上是模态的,PID x。

采纳答案by Adam K Dean

You can show it as a dialog, like so:

您可以将其显示为对话框,如下所示:

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

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

您传递当前IWin32Windowform您想成为所有者,因此如果您通过单击父窗体的按钮调用它,只需传递this.

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

您希望能够获得IWin32Window另一个进程的 ,这是可能的,但我不知道是否将表单显示为模态。

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?

此链接可能会提供有关该主题的更多信息:如何使子进程窗口在我的进程中显示为模态?