wpf - 为什么 1920 与设备无关的单元不是具有 96 DPI 的 1920 像素?

标签 wpf screen-resolution dpi

我的 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 上,包含一些额外的空间,即使用户稍微偏离边界,也可以轻松调整窗口大小:

resize off of the window

(请注意,我的鼠标不在边框上,而是在右侧有点偏)

这意味着当你最大化你的窗口时,你的宽度仍然被那个不可见的空间“填充”,因此你的窗口大小将大于你的屏幕大小。

如何检索额外空间的宽度? GetSystemMetrics SM_CXSIZEFRAMESM_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/

相关文章:

WPF ToolTip在鼠标按下时消失

c# - 如何通过编程调整流文档中的段落间距

android - Android 设计的基本分辨率是多少?

css - 为移动网页设置页面宽度的设计最佳实践是什么?

python - 在定义的像素位置上绘制均匀间隔的垂直线

wpf - 加载 WPF 后 Windows 窗体调整大小

c# - 在 3D 空间中旋转图像的一部分

c# - 关闭时淡出 wpf 窗口

javascript - screen.width 与设备实际分辨率

c# - GUI 故障 : Checkbox text shown incomplete in Windows 8