在与 MainWindow 相同的监视器上打开窗口 [英] Open Window on the Same Monitor as MainWindow

查看:31
本文介绍了在与 MainWindow 相同的监视器上打开窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多关于类似问题的问题,但我还没有找到一个足够具体到我的问题来找到答案的问题.

I've seen many questions regarding similar issues, but I haven't found one quite specific enough to my question to find an answer yet.

我允许用户移动我的程序的 MainWindow,如果有的话,可以移动到另一台显示器上.当他们在 MainWindow 上单击以添加内容时,会创建一个新窗口,以便他们可以填写表单.

I allow the user to move the MainWindow of my program around and, if they have one, onto another monitor. When they click to add something on the MainWindow, a new Window is created so they can fill in a form.

问题是这个新窗口将在操作系统的主屏幕上启动,而不是在 MainWindow 当前所在的屏幕上启动.

The problem is that this new Window will start on the primary screen of the OS instead of starting on the screen that the MainWindow is currently located on.

我看过这个问题;如何在与主窗口相同的监视器上打开一个对话框(视图) 并看到在这种情况下将所有者设置为 MainWindow 有效.所以我加了

I had a look at this question; How to make a dialog(view) open up on the same monitor as the main window and saw that setting the owner to the MainWindow worked in this case. So I added

Owner = Application.Current.MainWindow;

进入Window_Loaded方法新建Window.这确实将 Window 加载到与 MainWindow 相同的屏幕上,但随后崩溃,说明 在显示 Dialog 后无法设置 Owner 属性.

into the Window_Loaded method the new Window. This does load the Window on the same screen as the MainWindow but then crashes, stating Cannot set Owner property after Dialog is shown.

我自然将 Owner 属性的设置移动到显示新窗口之前,所以它看起来像这样;

Naturally I move the setting of the Owner property to before the new Window is shown, so it looks like this;

contractAddwindow.Owner = Application.Current.MainWindow;contractAddwindow.ShowDialog();

然而,这与之前有同样的问题,contractAddWindow 在主屏幕上启动.

however this then has the same issue as before, the contractAddWindow starts on the primary screen.

所以我的问题是,如何强制新窗口加载到与 MainWindow 相同的监视器上而不是主屏幕上?

So my question is, how do I force the new Window to load on the same monitor as the MainWindow instead of the primary screen?

推荐答案

@在使用此答案之前,请考虑尝试 Nawed 的答案(见下文).他的要简单得多.

@ Consider trying Nawed's answer (see below) before using this one. His is much simpler.

试试这个方法:

using System.Windows;
using System.Windows.Forms; // Reference System.Drawing

[...]

public void SetWindowScreen(Window window, Screen screen)
{
    if (screen != null)
    {
        if (!window.IsLoaded)
        {
            window.WindowStartupLocation = WindowStartupLocation.Manual;
        }

        var workingArea = screen.WorkingArea;
        window.Left = workingArea.Left;
        window.Top = workingArea.Top;
    }
}

public Screen GetWindowScreen(Window window)
{
    return Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(window).Handle);
}

然后,从您希望与 MainWindow 显示在同一监视器上的 Window 的构造函数中像这样调用它:

And then, call it like this from the constructor of the Window you want to show on the same monitor as the MainWindow:

SetWindowScreen(this, GetWindowScreen(App.Current.MainWindow));

这篇关于在与 MainWindow 相同的监视器上打开窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆