简体   繁体   English

Win10 WPF窗口定位错误

[英]win10 wpf window positioning bug

I've experienced a bug(?) in windows 10 with window positioning. 我在带有窗口定位的Windows 10中遇到了一个bug(?)。 I've reproduced this in WPF. 我在WPF中转载了此内容。 Lets say I create a window like so: 可以说我像这样创建一个窗口:

var d = new Window();
d.Width = 100;
d.Height = 100;
d.Left = 1000;
d.Top = 100;
d.WindowStyle = WindowStyle.None; 
d.ResizeMode = ResizeMode.NoResize;
d.Show();

I am on resolution 1920x1080, so I'd expect the left of the window to be just right of center at 1000px (as it was in windows 8), however It's not. 我的分辨率为1920x1080,所以我希望窗口的左侧位于中心的右侧,像素为1000px(就像在Windows 8中一样),但是事实并非如此。 With testing (by both hovering the mouse, and grabbing mouse coordinates, and also by testing with PointToScreen) it shows that the X (Left) position of the window is actually at 1250 and the Y (Top) position is 125. So all of the coordinates are being adjusted by a factor of 25%. 通过测试(通过悬停鼠标和获取鼠标坐标,以及通过PointToScreen进行测试),它显示窗口的X(左)位置实际上是1250,而Y(顶部)是125。坐标将调整25%。 However, if you check the Window.Left property, it still says 1000 dispite the window actually being located at 1250. 但是,如果您检查Window.Left属性,它仍然显示1000,尽管该窗口实际位于1250。

I immediately went looking for display settings in windows and I found this: 我立即去寻找Windows中的显示设置,结果发现:

在此处输入图片说明

So I figured that if this was the culprit it was just a matter of detecting this some how, except I found that even changing this to 100% instead of 125% actually makes no change and the bug is still the same. 因此,我认为如果这是问题的根源,那只是检测它的方式而已,只是我发现即使将其更改为100%而不是125%实际上也没有任何改变,并且该错误仍然相同。

So has anyone else ran into this bug and if it is a matter of a windows display setting, is there any way to detect it? 因此,是否还有其他人遇到此错误,如果仅是Windows显示设置,是否有任何方法可以检测到该错误?

With help from Cyral in the comments of the question, Find the DPI as such: 在Cyral的问题注释帮助下,找到DPI如下:

var dpiX = (int)typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
var dpiY = (int)typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);

And then make the correction of: [Desired Pixel Coordinate] * (96 / [dpi]) 然后进行以下校正: [Desired Pixel Coordinate] * (96 / [dpi])

This is due to the fact that WPF uses DPI and virtual pixels for layout instead of physical pixels. 这是因为WPF使用DPI和虚拟像素而不是物理像素进行布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM