WPF中的屏幕分辨率问题?

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

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

屏幕的当前分辨率为1920 * 1200,但height为960.0, width为1536.0!

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

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

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

为了实现更强大的实施,您应该计算系统上的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的值应与您在监视器的“属性”窗口中看到的值相匹配。

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

您可以使用此代码: http : //msdn.microsoft.com/en-us/library/system.windows.forms.screen.primaryscreen.aspx

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

若要查找“使用Windows XP样式DPI缩放”复选框,请在以下链接中展开“在非DPI设计的程序中使文本和屏幕上的项目更清晰”链接: http : //windows.microsoft。 COM / EN-US / Windows的VISTA /化妆的文本上,您的屏幕,更大有或更小

请在加载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); } } 

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

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