C# WPF中的屏幕分辨率问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2236173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:38:09  来源:igfitidea点击:

Screen Resolution Problem In WPF?

c#.netwpfscreenresolution

提问by Mohammad Dayyan

I'm gonna detect the resolution with the following code in WPF :

我将在 WPF 中使用以下代码检测分辨率:

double height = System.Windows.SystemParameters.PrimaryScreenHeight;
double width = System.Windows.SystemParameters.PrimaryScreenWidth;

Current resolution of my screen is 1920*1200, but heightis 960.0 and widthis 1536.0 !!!

我的屏幕当前分辨率是 1920*1200,但是height是 960.0 和width1536.0 !!!

What's wrong with it ?
Thanks in advance.

它出什么问题了 ?
提前致谢。

采纳答案by Hans Passant

Keep in mind that all WPF locations and sizes are floating point with a unit of 1/96 inch. Not pixels. This makes your window designs resolution independent. Doing the math: height = 960 / 96 = 10 inches. With your video adapter set to 120 DPI (120/96 = 125%): 10 * 120 = 1200 pixels. Same for width: 1536 / 96 * 120 = 1920 pixels.

请记住,所有 WPF 位置和大小都是浮点数,单位为 1/96 英寸。不是像素。这使您的窗口设计分辨率独立。计算一下:身高 = 960 / 96 = 10 英寸。将您的视频适配器设置为 120 DPI (120/96 = 125%):10 * 120 = 1200 像素。宽度相同:1536 / 96 * 120 = 1920 像素。

System.Windows.Forms works in units of pixels. You are getting less than 1050 for the height because it subtracts the height of the taskbar. But for WPF you always want to work with 1/96", never pixels.

System.Windows.Forms 以像素为单位工作。你得到的高度小于 1050,因为它减去了任务栏的高度。但是对于 WPF,您总是希望使用 1/96",而不是像素。

回答by Akash Kava

Try SystemParameters.FullPrimaryScreenWidthand FullPrimaryScreenHeight, I believe PrimaryScreenWidth and Height returns size of available client window after removing the taskbar and other deskbands on your screen.

尝试SystemParameters.FullPrimaryScreenWidth和 FullPrimaryScreenHeight,我相信 PrimaryScreenWidth 和 Height 在移除屏幕上的任务栏和其他桌面后返回可用客户端窗口的大小。

回答by deepu

try these..i believe this could correct the error.....

试试这些..我相信这可以纠正错误.....

System.Windows.Form1.Screen.PrimaryScreen.Bounds.Height; System.Windows.Form1.Screen.PrimaryScreen.Bounds.Widtht;

System.Windows.Form1.Screen.PrimaryScreen.Bounds.Height; System.Windows.Form1.Screen.PrimaryScreen.Bounds.Widtht;

回答by JeffFerguson

For an even more robust implementation, you should calculate the DPI factors on your system and work with those factors. A normal DPI value is 96, but some monitors may have different values. Consider that your code may be running on a monitor that has a different DPI value than 96. Consider this code:

为了实现更强大的实现,您应该计算系统上的 DPI 因素并使用这些因素。正常的 DPI 值为 96,但某些显示器可能具有不同的值。考虑到您的代码可能在 DPI 值与 96 不同的监视器上运行。请考虑以下代码:

    private static void CalculateDpiFactors()
    {
        Window MainWindow = Application.Current.MainWindow;
        PresentationSource MainWindowPresentationSource = PresentationSource.FromVisual(MainWindow);
        Matrix m = MainWindowPresentationSource.CompositionTarget.TransformToDevice;
        thisDpiWidthFactor = m.M11;
        thisDpiHeightFactor = m.M22;
    }

You can then use those ratios to get the final values:

然后,您可以使用这些比率来获得最终值:

CalculateDpiFactors();
double ScreenHeight = SystemParameters.PrimaryScreenHeight * thisDpiHeightFactor;
double ScreenWidth = SystemParameters.PrimaryScreenWidth * thisDpiWidthFactor;

The values of ScreenHeight and ScreenWidth should then match what you see in your monitor's Properties window.

ScreenHeight 和 ScreenWidth 的值应该与您在监视器的“属性”窗口中看到的相匹配。

回答by Marc G.

If you check "Use Windows XP style DPI scaling", then the returned value of "Screen.PrimaryScreen.Bounds" is correct. If not, the returned value is proportionally shrunk by the DPI value (which is the default).

如果勾选“Use Windows XP style DPI scaling”,则“Screen.PrimaryScreen.Bounds”的返回值是正确的。如果不是,则返回值按 DPI 值(默认值)按比例缩小。

To find the "Use Windows XP style DPI scaling" checkbox, expand the "To make text and on-screen items clearer in programs that aren't designed for high DPI" link in the following link: http://windows.microsoft.com/en-us/windows-vista/Make-the-text-on-your-screen-larger-or-smaller

要找到“使用 Windows XP 样式 DPI 缩放”复选框,请展开以下链接中的“使文本和屏幕上的项目在不是为高 DPI 设计的程序中更清晰”链接:http: //windows.microsoft。 com/en-us/windows-vista/Make-the-text-on-your-screen-larger-or-smaller

回答by Andreas

Please call this after your windows is loaded.

请在您的窗口加载后调用它。

 public static class Ext
{
    public static Size GetNativePrimaryScreenSize(this Window window)
    {
        PresentationSource mainWindowPresentationSource = PresentationSource.FromVisual(window);
        Matrix m = mainWindowPresentationSource.CompositionTarget.TransformToDevice;
        var dpiWidthFactor = m.M11;
        var dpiHeightFactor = m.M22;
        double screenHeight = SystemParameters.PrimaryScreenHeight * dpiHeightFactor;
        double screenWidth = SystemParameters.PrimaryScreenWidth * dpiWidthFactor;

        return new Size(screenWidth, screenHeight);
    }
}

回答by Marco Concas

You can use that:

你可以使用它:

float dpiX, dpiY;
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
     dpiX = graphics.DpiX;
     dpiY = graphics.DpiY;
}

double screenX = SystemParameters.PrimaryScreenWidth / 96 * dpiX;
double screenY = SystemParameters.PrimaryScreenHeight / 96 * dpiY;