我想用C#
创建一个窗口,并将其设置为我的定义句柄的父窗口,该句柄是其他进程的窗口句柄。
有人知道如何做吗?
问候,
我想用C#
创建一个窗口,并将其设置为我的定义句柄的父窗口,该句柄是其他进程的窗口句柄。
有人知道如何做吗?
问候,
class Win32Window : IWin32Window
{
IntPtr handle;
public Win32Window(IntPtr handle) { this.handle = handle; }
public IntPtr Handle
{
get { return this.handle; }
}
}
static void Main()
{
IntPtr targetParent = // Get handle to the parent window
new MainForm().ShowDialog(new Win32Window(targetParent));
}
这将把MainForm
变成指定窗口的子窗口,使其始终显示在其上方。在示例中,我使用了ShowDialog
,但是对于Show
也应该适用。这仅适用于Windows Forms。
在WPF中,您可以尝试以下操作:
var helper = new WindowInteropHelper(/* your Window instance */);
helper.Owner = // Set with handle for the parent
在展示WPF窗口后,我快速尝试了这个方法,它似乎按预期工作,但我对WPF的了解并不是很深。