在我的winforms应用程序中,我正在尝试获取主窗口句柄,因此我可以将其设置为我的wpf模式窗口的父级。我对winforms没有太多经验,所以经过一些谷歌搜索我找到了两种方法来获得它。
System.Windows.Forms.Application.OpenForms[0].Handle
System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle
(1)似乎总是返回看似正确的相同值(至少我的模态窗口按预期行为),而(2)有时返回与(1)相同的值,但有时 - 一个完全不同的指针,似乎不起作用(我的模态窗口出现在每个其他窗口的顶部,而不仅仅是父窗口)。
有人可以解释这两种方法的区别吗?有时它们会返回不同的结果,这是正常的吗?
编辑:
万一其他人想知道:一旦你得到了句柄,你可以通过创建来使用它 WindowInteropHelper
类:
public static void SetInteropParent(this Window wpfDialogWindow, IntPtr winformsParentHandle)
{
new WindowInteropHelper(wpdDialogWindow) { Owner = winformsParentHandle };
}