【问题标题】:Screen Resolution Problem In WPF?WPF中的屏幕分辨率问题?
【发布时间】:2010-02-10 10:52:00
【问题描述】:

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

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

我的屏幕当前分辨率是 1920*1200,但<strong>height</strong> 是 960.0 而<strong>width</strong> 是 1536.0 !!!

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

【问题讨论】:

    标签: c# .net wpf screen resolution


    【解决方案1】:

    请记住,所有 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",而不是像素。

    【讨论】:

      【解决方案2】:

      要实现更稳健的实施,您应该计算系统上的 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 的值应该与您在显示器的“属性”窗口中看到的值相匹配。

      【讨论】:

      • 这很好,但你提前有一个窗口。如何计算没有窗口的 DPI?应该有可能——dpi 是桌面的属性,而不仅仅是窗口。
      • 谢谢,这很有帮助。
      • 这里似乎有一些与此相关的代码:mesta-automation.com/tecniques-scaling-wpf-application
      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          【讨论】:

          • 我测试过,结果和我的第一篇一样!
          • 好吧,这很奇怪。如果您从控制面板更改屏幕分辨率会怎样?
          • 我把分辨率改成1680*1050,结果:height = 1002.0;宽度=1680.0。顺便说一句:DPI 是 125%
          【解决方案6】:

          如果勾选“使用 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

          【讨论】:

          • 仅供参考:该链接现在自动转到 Windows 8.x 说明,而不是 XP/Vista/Win7
          【解决方案7】:

          你可以使用它:

          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;
          

          【讨论】:

            【解决方案8】:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-11-15
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多