WPF 中的窗口所有者没有始终位于顶部的行为

发布于 2024-10-26 14:45:10 字数 442 浏览 6 评论 0原文

是否可以在不获取全部功能的情况下获取 Window.Owner 的部分功能?

有两个窗口,窗口 A 和窗口 B。我希望选择其中一个窗口会将它们置于其他应用程序之上,但其中一个窗口可以覆盖另一个应用程序。 (实际上不止两个,但它们的行为都应该相似。)

如果我将窗口 B 的 Owner 设置为 A,那么切换到任一窗口都会将这两个窗口置于其他应用程序的前面(我希望),但也会迫使 B 始终坐在 A 之上(我不希望这样)。

实际上,我已经有了独立于 Owner/OwnedWindows 跟踪窗口层次结构的代码,因此我可以扩展它来解决激活问题。因此,如果这简化了问题,我正在寻找的另一个答案是:

“当用户激活此窗口时,将一组特定的窗口(应用程序中的所有其他窗口)带到 Z-顺序就在我下面,同时保留它们现有的相对于彼此的 Z 顺序”?

Is it possible to get some of the functionality of Window.Owner without getting all of it?

There are two windows, window A and window B. I want to make it so that selecting either one will bring them on top of other applications, but either one can overlay the other. (In reality there more than just two, but they should all behave similarly.)

If I set window B's Owner to A, then switching to either window will bring both in front of other applications (which I want), but will also force B to always sit on top of A (which I don't want).

I actually already have code which is tracking the window hierarchy independently of Owner/OwnedWindows, so I can probably extend that to sort out the activation problem. So if that simplifies the problem, an alternative answer I'm looking for is:

How do I actually do "when this window is activated by the user, bring a specific set of windows (all the others in the app) to the Z-order just below me, while preserving their existing Z-orders relative to each other"?

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

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

发布评论

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

评论(1

孤独患者 2024-11-02 14:45:10

一种可能的解决方案是拥有一个隐藏窗口来拥有应用程序中的所有窗口。

您可以将其声明为:

<Window
    Opacity="0"
    ShowInTaskbar="False"
    AllowsTransparency="true"
    WindowStyle="None">

确保从 App.xaml 中删除 StartupUri。在您的 App.xaml.cs 中,您将重写 OnStartup 使其看起来像这样:

protected override void OnStartup(StartupEventArgs e)
{
    HiddenMainWindow window = new HiddenMainWindow();
    window.Show();

    Window1 one = new Window1();
    one.Owner = window;
    one.Show();

    Window2 two = new Window2();
    two.Owner = window;
    two.Show();
}

另一个困难是您希望如何处理关闭实际应用程序。如果其中一个窗口被视为 MainWindow,则只需将应用程序 ShutdownMode 更改为 ShutdownMode.OnMainWindowClose,然后将 MainWindow 属性设置为其中一个窗口即可。否则,您将需要确定所有窗口何时关闭并显式调用 Shutdown。

One possible solution would be to have a hidden window that owns all the windows in your app.

You would declare it something like:

<Window
    Opacity="0"
    ShowInTaskbar="False"
    AllowsTransparency="true"
    WindowStyle="None">

Be sure to remove StartupUri from your App.xaml. And in your App.xaml.cs you would override OnStartup to look something like:

protected override void OnStartup(StartupEventArgs e)
{
    HiddenMainWindow window = new HiddenMainWindow();
    window.Show();

    Window1 one = new Window1();
    one.Owner = window;
    one.Show();

    Window2 two = new Window2();
    two.Owner = window;
    two.Show();
}

Another difficulty will be how you want to handle closing the actual application. If one of these windows is considered the MainWindow you can just change the application ShutdownMode to ShutdownMode.OnMainWindowClose and then set the MainWindow property to either of those windows. Otherwise you will need to determine when all windows are closed and call Shutdown explicitly.

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