简体   繁体   English

在与 MainWindow 相同的监视器上打开窗口

[英]Open Window on the Same Monitor as 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.我已经看到很多关于类似问题的问题,但我还没有找到一个足够具体到我的问题来找到答案的问题。

I allow the user to move the MainWindow of my program around and, if they have one, onto another monitor.我允许用户移动我的程序的 MainWindow,如果他们有,则移动到另一台显示器上。 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. 如何在与主窗口相同的监视器上打开一个对话框(视图),并看到在这种情况下将所有者设置为 MainWindow 有效。 So I added所以我加了

Owner = Application.Current.MainWindow;

into the Window_Loaded method the new Window.进入Window_Loaded 方法新建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.这确实将 Window 加载到与 MainWindow 相同的屏幕上,但随后崩溃,指出Cannot set Owner property after Dialog is shown.

Naturally I move the setting of the Owner property to before the new Window is shown, so it looks like this;当然,我将 Owner 属性的设置移动到显示新窗口之前,所以它看起来像这样;

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

however this then has the same issue as before, the contractAddWindow starts on the primary screen.但是,这与以前存在相同的问题, contractAddWindow在主屏幕上启动。

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?所以我的问题是,如何强制新窗口与 MainWindow 而不是主屏幕加载在同一监视器上?

You can use WindowStartupLocation :您可以使用WindowStartupLocation

From XAML:从 XAML:

WindowStartupLocation="CenterOwner"

From Code:从代码:

WindowStartupLocation = WindowStartupLocation.CenterOwner;

@EDIT: Consider trying Nawed's answer (see below) before using this one. @编辑:在使用这个答案之前,请考虑尝试 Nawed 的答案(见下文)。 His is much simpler.他的要简单得多。

Try this methods:试试这个方法:

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

And then, call it like this from the constructor of the Window you want to show on the same monitor as the MainWindow :然后,从要在与MainWindow相同的监视器上显示的Window的构造函数中像这样调用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM