非常奇怪的行为访问C#.NET

时间:2018-04-14 08:48:06

标签: c# wpf winforms

我在尝试访问C#.NET控制台应用程序中的系统屏幕尺寸时遇到了一些非常奇怪的行为。奇怪的是,我可以访问“System.Windows.SystemParameters.PrimaryScreenHeight”并获得一个双倍值,它恰好是实际屏幕高度的一半,我可以访问“System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height”并获得一个双倍值,也就是一半实际的屏幕高度。单独调用,两个返回都是错误的...但是,如果我只是调用SystemParameters屏幕高度并将其保存为我再也不会再触摸的虚拟双变量,则Forms.Screen调用的返回突然开始工作正确并返回准确的屏幕高度。

double dummyH = System.Windows.SystemParameters.PrimaryScreenHeight;
double dummyW = System.Windows.SystemParameters.PrimaryScreenWidth;
double height = Screen.PrimaryScreen.Bounds.Height;
double width = Screen.PrimaryScreen.Bounds.Width;

Console.WriteLine("System Param Height: " + dummyH);
Console.WriteLine("System Param Width: " + dummyW);
Console.WriteLine("System Screen Height: " + height);
Console.WriteLine("System Screen Width: " + width);

以下是我发布的3分钟YouTube视频:http://www.youtube.com/watch?v=y_HnF8f0GFQ&hd=1

谢谢!

1 个答案:

答案 0 :(得分:1)

您没有考虑显示器的DPI缩放比例。你需要乘以它。

以下是我的工作方式https://github.com/angelsix/fasetto-word/blob/develop/Source/Fasetto.Word/Window/WindowResizer.cs#L219,这是我拍摄的长视频https://www.youtube.com/watch?v=TDOxHx-AMqQ