My Windows settings are set to have a DPI of 96. However when I create the following Window
it will be a little smaller then my screen (which is set to the resolution 1920*1080):
我的Windows设置被设置为DPI为96.但是当我创建以下窗口时,它将比我的屏幕(设置为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>
According to this msdn blog post:
根据这篇msdn博客文章:
1 device independent pixel = 1/96 inch.
1 physical pixel = 1/DPI (dependent on the system DPI)1个设备独立像素= 1/96英寸。 1个物理像素= 1 / DPI(取决于系统DPI)
Default system settings usually choose a DPI of 96 so these two types of pixels come out to be the same size.
默认系统设置通常选择DPI为96,因此这两种类型的像素大小相同。
However this does not seem to be the case since my Window
is clearly less then 1920 pixel wide.
If I maximize the Window
and inspect its ActualWidth
it appears to be 1936
.
然而,这似乎并非如此,因为我的Window显然小于1920像素宽。如果我最大化窗口并检查其ActualWidth它似乎是1936年。
Why is this the case?
为什么会这样?
3 个解决方案
#1
4
The question is more along the lines of:
问题更多的是:
Why does Window.ActualWidth report a width that does not appear to be the 'real' width of the Window?
为什么Window.ActualWidth报告的宽度似乎不是窗口的“实际”宽度?
The answer is that what looks like the full width of the window isn't exactly the full width.
答案是看起来窗口的整个宽度并不完全是宽度。
When you set the Width
, query the ActualWidth
, or even use GetWindowRect
to get the "no-lies what does windows think my width is", you're setting or getting the width of the Window including invisible parts.
当您设置宽度,查询ActualWidth,或者甚至使用GetWindowRect来获得“没有谎言窗户认为我的宽度是什么”时,您正在设置或获取窗口的宽度,包括不可见的部分。
Those invisible parts, especially on Windows 10, includes a little bit of extra space for making it easy for users to resize the window even if they're a little bit off of the border:
这些不可见的部分,特别是在Windows 10上,包含一些额外的空间,使用户可以轻松调整窗口的大小,即使它们有点偏离边界:
(note that my mouse is not on the border but rather a bit off to the right)
(注意我的鼠标不在边框上,而是在右边有点偏离)
This means that when you maximize your window, your width is still "padded" with that invisible space, and thus your window size will be bigger than your screen size.
这意味着当您最大化窗口时,您的宽度仍然会被“填充”该不可见空间,因此您的窗口大小将大于您的屏幕大小。
How can you retrieve the width of the extra space? GetSystemMetrics
with SM_CXSIZEFRAME
and SM_CXPADDEDBORDER
is your friend here (example includes code from pinvoke.net):
如何检索额外空间的宽度?带有SM_CXSIZEFRAME和SM_CXPADDEDBORDER的GetSystemMetrics是你的朋友(例子包括来自pinvoke.net的代码):
int extraBorderStuff = GetSystemMetrics(SystemMetric.SM_CXSIZEFRAME)
+ GetSystemMetrics(SystemMetric.SM_CXPADDEDBORDER);
double realWidthForSure = ActualWidth - borderSize * 2;
If you then maximize your window, you should notice that realWidthForSure
should equal your 1920px that you'd expect.
如果你然后最大化你的窗口,你应该注意到realWidthForSure应该等于你期望的1920px。
#2
3
The window really has a width of 1936 pixels!
窗口的宽度实际为1936像素!
When a window is maximized, the border around it is cropped. Although it is not visible any more, it is still considered part of the window. In the Aero theme, this border has a width of 8px on each side, hence the 16px extra:
当窗口最大化时,会裁剪其周围的边框。虽然它不再可见,但它仍然被视为窗口的一部分。在Aero主题中,这个边框的每边宽度为8px,因此额外增加了16px:
If you switch to the Classic theme, it's only 4px on each side, so your window would be 1928 pixels wide.
如果切换到经典主题,每侧只有4像素,因此您的窗口宽度为1928像素。
#3
-1
What are your screen specs? for e.g. my monitor has pitch = 0.282 mm; which equals to 90.0709... ppi. A 1920 pixels wide window would have actual width of 2,046 units in WPF.
你的屏幕规格是什么?例如我的显示器的间距= 0.282毫米;等于90.0709 ... ppi。宽度为1920像素的窗口在WPF中的实际宽度为2,046个单位。
determine your screen's DPI (25.4/pitch if you have pitch in mm)
确定你的屏幕的DPI(如果你的音高为mm,则为25.4 /音高)
96/DPI * 1920 => Actual Width
96 / DPI * 1920 =>实际宽度
#1
4
The question is more along the lines of:
问题更多的是:
Why does Window.ActualWidth report a width that does not appear to be the 'real' width of the Window?
为什么Window.ActualWidth报告的宽度似乎不是窗口的“实际”宽度?
The answer is that what looks like the full width of the window isn't exactly the full width.
答案是看起来窗口的整个宽度并不完全是宽度。
When you set the Width
, query the ActualWidth
, or even use GetWindowRect
to get the "no-lies what does windows think my width is", you're setting or getting the width of the Window including invisible parts.
当您设置宽度,查询ActualWidth,或者甚至使用GetWindowRect来获得“没有谎言窗户认为我的宽度是什么”时,您正在设置或获取窗口的宽度,包括不可见的部分。
Those invisible parts, especially on Windows 10, includes a little bit of extra space for making it easy for users to resize the window even if they're a little bit off of the border:
这些不可见的部分,特别是在Windows 10上,包含一些额外的空间,使用户可以轻松调整窗口的大小,即使它们有点偏离边界:
(note that my mouse is not on the border but rather a bit off to the right)
(注意我的鼠标不在边框上,而是在右边有点偏离)
This means that when you maximize your window, your width is still "padded" with that invisible space, and thus your window size will be bigger than your screen size.
这意味着当您最大化窗口时,您的宽度仍然会被“填充”该不可见空间,因此您的窗口大小将大于您的屏幕大小。
How can you retrieve the width of the extra space? GetSystemMetrics
with SM_CXSIZEFRAME
and SM_CXPADDEDBORDER
is your friend here (example includes code from pinvoke.net):
如何检索额外空间的宽度?带有SM_CXSIZEFRAME和SM_CXPADDEDBORDER的GetSystemMetrics是你的朋友(例子包括来自pinvoke.net的代码):
int extraBorderStuff = GetSystemMetrics(SystemMetric.SM_CXSIZEFRAME)
+ GetSystemMetrics(SystemMetric.SM_CXPADDEDBORDER);
double realWidthForSure = ActualWidth - borderSize * 2;
If you then maximize your window, you should notice that realWidthForSure
should equal your 1920px that you'd expect.
如果你然后最大化你的窗口,你应该注意到realWidthForSure应该等于你期望的1920px。
#2
3
The window really has a width of 1936 pixels!
窗口的宽度实际为1936像素!
When a window is maximized, the border around it is cropped. Although it is not visible any more, it is still considered part of the window. In the Aero theme, this border has a width of 8px on each side, hence the 16px extra:
当窗口最大化时,会裁剪其周围的边框。虽然它不再可见,但它仍然被视为窗口的一部分。在Aero主题中,这个边框的每边宽度为8px,因此额外增加了16px:
If you switch to the Classic theme, it's only 4px on each side, so your window would be 1928 pixels wide.
如果切换到经典主题,每侧只有4像素,因此您的窗口宽度为1928像素。
#3
-1
What are your screen specs? for e.g. my monitor has pitch = 0.282 mm; which equals to 90.0709... ppi. A 1920 pixels wide window would have actual width of 2,046 units in WPF.
你的屏幕规格是什么?例如我的显示器的间距= 0.282毫米;等于90.0709 ... ppi。宽度为1920像素的窗口在WPF中的实际宽度为2,046个单位。
determine your screen's DPI (25.4/pitch if you have pitch in mm)
确定你的屏幕的DPI(如果你的音高为mm,则为25.4 /音高)
96/DPI * 1920 => Actual Width
96 / DPI * 1920 =>实际宽度