WPF 中的屏幕分辨率问题?

发布于 2024-08-20 21:41:58 字数 343 浏览 5 评论 0原文

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

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

我的屏幕当前分辨率为 1920*1200,但 height 为 960.0 且 < strong>宽度是1536.0!!!

有什么问题吗?
提前致谢。

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

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

Current resolution of my screen is 1920*1200, but height is 960.0 and width is 1536.0 !!!

What's wrong with it ?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

蓝眼泪 2024-08-27 21:41:58

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

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

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.

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.

三寸金莲 2024-08-27 21:41:58

为了实现更稳健的实施,您应该计算系统上的 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;
    }

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

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

ScreenHeight 和 ScreenWidth 的值应与您在显示器中看到的值相匹配属性窗口。

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:

    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.

不可一世的女人 2024-08-27 21:41:58

请在 Windows 加载后调用此方法。

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

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);
    }
}
我只土不豪 2024-08-27 21:41:58

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

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

谁的年少不轻狂 2024-08-27 21:41:58

如果选中“使用 Windows XP 样式 DPI 缩放”,则“Screen.PrimaryScreen.Bounds”的返回值是正确的。如果不是,则返回值将按 DPI 值(默认值)按比例缩小。

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

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).

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

泛滥成性 2024-08-27 21:41:58

你可以使用它:

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;

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;
寂寞陪衬 2024-08-27 21:41:58

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文