简体   繁体   English

自定义DPI的辅助屏幕的实际分辨率

[英]Actual resolution of secondary screen for custom DPI

I'm working on adaptation of application to custom DPI values (Windows scaling). 我正在努力使应用程序适应自定义DPI值(Windows缩放)。

If I've understood correctly, scaling makes applications think, that resolution is smaller, than it actually is. 如果我理解正确,缩放使应用程序思考,分辨率比实际更小。 For example, having 125% scale, if you have 1920x1080 resolution, actual size of maximized window will be 1536x864. 例如,如果您具有1920x1080分辨率,则具有125%的比例,最大化窗口的实际大小将为1536x864。 And for sure, it may cause some problems, if you control some sizes and positions from code. 当然,如果你从代码中控制一些大小和位置,它可能会导致一些问题。

For now, I'm using next values for positioning etc: 现在,我正在使用下一个定位值等:

  • SystemParameters.FullPrimaryScreenWidth and SystemParameters.FullPrimaryScreenHeight as "virtual" (scaled) resolution; SystemParameters.FullPrimaryScreenWidthSystemParameters.FullPrimaryScreenHeight为“虚拟”(缩放)分辨率;
  • Screen.PrimaryScreen.WorkingArea.Width and Screen.PrimaryScreen.WorkingArea.Height for original resolution; Screen.PrimaryScreen.WorkingArea.WidthScreen.PrimaryScreen.WorkingArea.Height用于原始分辨率;

From this values, it is easy to get scaling value, which can be used in any positioning and sizing. 从这个值,很容易获得缩放值,可用于任何定位和大小调整。

But all of this values are actually for primary screen. 但所有这些值实际上都是针对主屏幕的。 What if I will need to do positioning and sizing for secondary screen, which might have different actual (and, therefore, different "virtual") resolution? 如果我需要对辅助屏幕进行定位和调整,如果可能具有不同的实际(因此,不同的“虚拟”)分辨率,该怎么办?

For now I'm getting screen like this 现在我得到这样的屏幕

var screen = Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(Application.Current.MainWindow).Handle);

Which allows me to get actual resolution, using WorkingArea rectangle. 这允许我使用WorkingArea矩形获得实际分辨率。 But what about "virtual" resolution? 但是“虚拟”解决方案呢? Is there analog of SystemParameters.FullPrimaryScreenWidth etc of any screen aside of primary? 是否有主要的任何屏幕的SystemParameters.FullPrimaryScreenWidth等模拟?

PS. PS。 I know, that scale is shared, so by getting its value for primary screen, I can use it for any other screen, but I'd like to know answer, if there is direct variable to get this values, anyway. 我知道,这个比例是共享的,所以通过获取主屏幕的值,我可以将它用于任何其他屏幕,但我想知道答案,如果有直接变量来获取这个值,无论如何。

You may use the following ratio 您可以使用以下比率

        // calculates text size in that main window (i.e. 100%, 125%,...)
        var ratio = Math.Max(Screen.PrimaryScreen.WorkingArea.Width / SystemParameters.PrimaryScreenWidth,
                        Screen.PrimaryScreen.WorkingArea.Height / SystemParameters.PrimaryScreenHeight);

For example, if you'd like to show maximized windows on all screen, you need to use the following rectangles (code is from App.xaml.cs): 例如,如果您想在所有屏幕上显示最大化窗口,则需要使用以下矩形(代码来自App.xaml.cs):

        foreach (var screen in Screen.AllScreens)
        {
            var mainViewModel = container.Resolve<IMainWindowModel>();

            var window = new MainWindow(mainViewModel);
            if (screen.Primary)
                Current.MainWindow = window;

            window.Left = screen.WorkingArea.Left / ratio;
            window.Top = screen.WorkingArea.Top / ratio;
            window.Width = screen.WorkingArea.Width / ratio;
            window.Height = screen.WorkingArea.Height / ratio;
            window.Show();
            window.WindowState = WindowState.Maximized;
        }

In addition, you may take a look at my post https://ireznykov.com/2017/01/13/wpf-windows-on-two-screens/ , that refers to GitHub sample application that outputs maximized windows on all screens. 此外,您可以查看我的帖子https://ireznykov.com/2017/01/13/wpf-windows-on-two-screens/ ,它指的是在所有屏幕上输出最大化窗口的GitHub示例应用程序。

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

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