如何强制WPF启动窗口到特定屏幕

如何强制WPF启动窗口到特定屏幕

本文介绍了如何强制WPF启动窗口到特定屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,它将通过专用窗口在投影仪上显示信息.我想配置用于投影仪显示的屏幕和用于主应用程序窗口的屏幕.

I have a WPF application that will show information on a projector through a dedicated window.I would like to configure what screen to be used for projector display and what to be used for main application window.

此代码将在指定的屏幕上生成投影仪输出:

This code will generate projector output on specified screen:

var screen = GetProjectorScreen();
_projectorWindow = new ProjectorWindow();
_projectorWindow.Left = screen.WorkingArea.Left;
_projectorWindow.Top = screen.WorkingArea.Top;
_projectorWindow.Owner = _parentWindow;
_projectorWindow.Show();


public static Screen GetProjectorScreen()
{
    var screens = Screen.AllScreens;
    if (screens.Length > 1 && Settings.Default.DisplayScreen < screens.Length)
    {
        return screens[Settings.Default.DisplayScreen];
    }
    return screens[0];
}

我已经尝试过用启动表单做同样的技巧,但是到目前为止还没有成功.我试图在MainWindow构造函数中设置Top和Left属性,但这没有用.

I have tried to do the same trick with startup form, but so far without success.I tried to set Top and Left properties in MainWindow constructor but that did not work.

通过设置StartupUri从App.xaml.cs启动启动窗口:

The startup window is launched from App.xaml.cs by setting StartupUri:

StartupUri = new Uri("Windows/MainWindow.xaml", UriKind.Relative);

还有其他启动启动表单的方法吗?我试图只调用构造函数,但这会导致崩溃,因为不再加载某些资源.

Is there any other way to launch startup form?I tried to just call the constructor but that causes a crash because some resources are no longer loaded.

推荐答案

我知道了.在设置窗口位置之前,必须将WindowState设置为Normal.并且直到创建窗口(即在构造函数调用之后)该设置才完全起作用.因此,我在Windows_Loaded事件中调用显式设置.如果需要移动窗口,可能会导致闪烁,但这对我来说是可以接受的.

I got it working. It is necessary to set WindowState to Normal before setting window location. And the setting will not work at all until the window is created, i.e. after constructor call. I therefore call the explicit setting in Windows_Loaded event. That might cause a flickering if window need to be moved, but that is acceptable to me.

在用户手动更改了屏幕设置之后,还应该调用SetScreen方法.

The SetScreen method should also be called after screen settings have changed manually by user.

private void SetScreen()
{
    var mainScreen = ScreenHandler.GetMainScreen();
    var currentScreen = ScreenHandler.GetCurrentScreen(this);
    if (mainScreen.DeviceName != currentScreen.DeviceName)
    {
        this.WindowState = WindowState.Normal;
        this.Left = mainScreen.WorkingArea.Left;
        this.Top = mainScreen.WorkingArea.Top;
        this.Width = mainScreen.WorkingArea.Width;
        this.Height = mainScreen.WorkingArea.Height;
        this.WindowState = WindowState.Maximized;
    }
}

备用ScreenHandler实用程序非常简单:

The backup ScreenHandler utility is very simple:

public static class ScreenHandler
{
    public static Screen GetMainScreen()
    {
        return GetScreen(Settings.Default.MainScreenId);
    }

    public static Screen GetProjectorScreen()
    {
        return GetScreen(Settings.Default.ProjectorScreenId);
    }

    public static Screen GetCurrentScreen(Window window)
    {
        var parentArea = new Rectangle((int)window.Left, (int)window.Top, (int)window.Width, (int)window.Height);
        return Screen.FromRectangle(parentArea);
    }

    private static Screen GetScreen(int requestedScreen)
    {
        var screens = Screen.AllScreens;
        var mainScreen = 0;
        if (screens.Length > 1 && mainScreen < screens.Length)
        {
            return screens[requestedScreen];
        }
        return screens[0];
    }
}

这篇关于如何强制WPF启动窗口到特定屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:07