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

发布于 2024-11-10 03:32:43 字数 91 浏览 3 评论 0原文

我想使用 C# 窗口创建,并将父级设置为我定义的句柄, 这是其他进程的窗口句柄。

有人知道该怎么做吗?

问候,

I want create using C# window with setted parent to my defined handle,
this is a other process window handle.

Anyone know how to do this?

Greetings,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半夏半凉 2024-11-17 03:32:43

如果我正确理解你的问题,你应该能够通过使用类似这样的东西来实现你想要的:

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 知识不是那么好。

If I understood your question correctly you should be able to achieve what you want by using something like this:

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));
}

This will turn MainForm a child window of the specified window making it always appear above it. I use ShowDialog in the example, but this should also work for Show. This is specific for Windows Forms.

In WPF you can try the following:

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

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

I quickly tried this after showing the WPF window and it seemed to work as expected, but WPF knowledge is not that great.

红衣飘飘貌似仙 2024-11-17 03:32:43

我相信句柄将是只读的;因此,.Parent 属性是只读的。但是,.Owner 属性有一个 getter 和一个 setter (参考 MSDN) ...但是,您必须有对父窗口的引用。

如果没有更多信息,我将无法提供更多信息。

如果您的父候选人是一个非托管窗口,请检查此链接

I believe the Handle will be read-only; therefore, the .Parent property is read-only. However, the .Owner property has a getter and a setter (ref. MSDN) ... however, you must have a reference to the Parent window.

Without more information, I will not be able to provide much more than that.

If your parent candidate is an unmanaged Window, check this link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文