我的 Windows 设置的 DPI 设置为 96。但是,当我创建以下内容时 Window
它会比我的屏幕小一点(设置为分辨率 1920*1080):
<Window x:Class="WPF_Sandbox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_Sandbox"
mc:Ignorable="d"
Title="MainWindow"
Width="1920">
</Window>
根据 this msdn 博文:1 device independent pixel = 1/96 inch.
1 physical pixel = 1/DPI (dependent on the system DPI)Default system settings usually choose a DPI of 96 so these two types of pixels come out to be the same size.
然而,这似乎并非如此,因为我的
Window
显然小于 1920 像素宽。如果我最大化
Window
并检查其 ActualWidth
它似乎是 1936
.为什么会这样?
最佳答案
问题更多是在以下方面:
Why does Window.ActualWidth report a width that does not appear to be the 'real' width of the Window?
答案是看起来像窗口的全宽的并不完全是全宽。
当您设置
Width
,查询ActualWidth
,甚至使用 GetWindowRect
要获得“ window 认为我的宽度是什么”的谎言,您正在设置或获取 Window 的宽度,包括不可见部分。那些不可见的部分,尤其是在 Windows 10 上,包含一些额外的空间,即使用户稍微偏离边界,也可以轻松调整窗口大小:
(请注意,我的鼠标不在边框上,而是在右侧有点偏)
这意味着当你最大化你的窗口时,你的宽度仍然被那个不可见的空间“填充”,因此你的窗口大小将大于你的屏幕大小。
如何检索额外空间的宽度?
GetSystemMetrics
与 SM_CXSIZEFRAME
和 SM_CXPADDEDBORDER
是你的 friend 吗(示例包括来自 pinvoke.net 的代码):int extraBorderStuff = GetSystemMetrics(SystemMetric.SM_CXSIZEFRAME)
+ GetSystemMetrics(SystemMetric.SM_CXPADDEDBORDER);
double realWidthForSure = ActualWidth - borderSize * 2;
如果然后最大化您的窗口,您应该注意到
realWidthForSure
应该等于您期望的 1920px。
关于wpf - 为什么 1920 与设备无关的单元不是具有 96 DPI 的 1920 像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36182487/