Moh*_*yan 25 .net c# wpf resolution screen
我将在WPF中使用以下代码检测分辨率:
double height = System.Windows.SystemParameters.PrimaryScreenHeight;
double width = System.Windows.SystemParameters.PrimaryScreenWidth;
Run Code Online (Sandbox Code Playgroud)
我屏幕的当前分辨率是1920*1200,但是height
960.0并且width
是1536.0 !!!
它出什么问题了 ?
提前致谢.
Han*_*ant 34
请记住,所有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",而不是像素.
Jef*_*son 25
为了实现更强大的实施,您应该计算系统上的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;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用这些比率来获取最终值:
CalculateDpiFactors();
double ScreenHeight = SystemParameters.PrimaryScreenHeight * thisDpiHeightFactor;
double ScreenWidth = SystemParameters.PrimaryScreenWidth * thisDpiWidthFactor;
Run Code Online (Sandbox Code Playgroud)
然后,ScreenHeight和ScreenWidth的值应与您在监视器的"属性"窗口中看到的值相匹配.
归档时间: |
|
查看次数: |
21315 次 |
最近记录: |