C# 创建窗口——定义父窗口

标签 c# wpf windows createwindow

我想使用 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 窗体的。

在 WPF 中,您可以尝试以下操作:

var helper = new WindowInteropHelper(/* your Window instance */);

helper.Owner = // Set with handle for the parent

我在显示 WPF 窗口后快速尝试了这个,它似乎按预期工作,但 WPF 知识不是那么多。

关于C# 创建窗口——定义父窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6145872/

相关文章:

windows - Azure:如果 guest 操作系统更新破坏了我的应用程序怎么办?

c# - 与硬件接口(interface)的 MCV/MVP 模式和应用程序(DAQ/PLC/等)

c# - wpf渲染同步

c# - metro 应用程序 : gridview with auto switch item content

wpf - 绑定(bind)属性中的“自动”值 'width'

wpf - 在 wpf 控件中处理对象

windows - 为什么 WMDC/ActiveSync 如此不稳定?

c# - 带有 Sqlite 的 AdoJobStore

c# - 解析没有反射对象的复杂 JSON?

c# - 使用语音合成器在 Windows 应用商店应用程序中将文本转为语音