将 WPF 窗口保留在当前工作显示器上(在多显示器设置中)

问题描述 投票:0回答:1

我正在开发一个WPF应用程序。在我的应用程序中,将有一个主窗口 (System.Windows.Window),在其顶部我将在页面 (System.Windows.Controls.Page) 之间导航。每当我从一个页面导航到另一页面时,我希望它位于当前工作监视器的中心(多个监视器将可用)。我可以使用下面的代码实现主窗口的中心定位。 每当我在页面之间导航时(例如:登录页面 -> 主页),我都会使用当前窗口的宽度和高度调用以下代码

public static void CenterWindowOnScreen(double width, double height)
{
    double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
    double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
    double windowWidth = width;
    double windowHeight = height;
    App.Current.MainWindow.Left = (screenWidth / 2) - (windowWidth / 2);
    App.Current.MainWindow.Top = (screenHeight / 2) - (windowHeight / 2);
}

应用程序启动时,主窗口将加载登录页面并位于主监视器的中心。现在,我将主窗口拖放到另一个(辅助)显示器上,当我尝试登录时,主窗口会自动移动到主显示器。现在主窗口已加载主页,但该窗口位于主监视器上。我想将主窗口保留在当前工作监视器的中心(在本例中为辅助监视器)。

c# wpf windows monitor
1个回答
0
投票

我刚刚错过了添加工作区域的左侧和顶部坐标。下面的链接帮助我将 WPF 窗口集中在当前工作的显示器上,并连接了多个显示器(具有不同的 DPI 比例)。 访问 https://stackoverflow.com/a/68283497/6454203

© www.soinside.com 2019 - 2024. All rights reserved.