多个显示器上的坐标错误
我有一个数据网格,我想知道数据单元格的位置以将其与窗口覆盖。
仅在一台显示器上工作正常,但在多台显示器上,窗口会移位。
这是代码:
Point point = cell.PointToScreen(new Point(0, 0));
...
Window myWindow = new Window();
myWindow.Top = point.Y;
myWindow.Left = point.X;
有人有在多个显示器上定位的经验吗?
编辑:
我做了以下测试:
public MyWindow()
{
...
this.LocationChanged += MyWindow_LocationChanged;
}
void MyWindow_LocationChanged(object sender, EventArgs e)
{
Console.WriteLine(this.Top + " <--> " + this.PointToScreen(new Point(0, 0)).Y);
}
结果:
- 单显示器:0 <--> 30; 20<--> 50;第100章130
==>总是相差30(可能是标题栏造成的)
- 双显示器:0 <--> 30; 20<--> 55;第100章153
==>在 0,0 处相差 30。但是我将窗口移离 0,0 越远。差异越大,但应该保持不变。很奇怪!
编辑2:
这是我的解决方案,感谢 CodeNaked 的提示:
Point point = cell.PointToScreen(new Point(0, 0));
...
Window myWindow = new Window();
PresentationSource source = PresentationSource.FromVisual(this);
myWindow.Top = point.Y / source.CompositionTarget.TransformToDevice.M22;
myWindow.Left = point.X / source.CompositionTarget.TransformToDevice.M11;
I have a Datagrid and I want to know the position of a datacell for overlaying it with a window.
It works fine with only one monitor, but with multiple monitors, the window is displaced.
Here's the code:
Point point = cell.PointToScreen(new Point(0, 0));
...
Window myWindow = new Window();
myWindow.Top = point.Y;
myWindow.Left = point.X;
Somebody has some experience with positioning on multiple monitors?
EDIT:
I made following test:
public MyWindow()
{
...
this.LocationChanged += MyWindow_LocationChanged;
}
void MyWindow_LocationChanged(object sender, EventArgs e)
{
Console.WriteLine(this.Top + " <--> " + this.PointToScreen(new Point(0, 0)).Y);
}
Results:
- Single-Monitor: 0 <--> 30; 20 <--> 50; 100 <--> 130
==> Always difference of 30 (may be caused by title bar)
- Dual-Monitor: 0 <--> 30; 20 <--> 55; 100 <--> 153
==> At 0,0 difference of 30. But the more I moved the window away from 0,0. the greater becomes the difference, but should stay the same. Very strange!
EDIT2:
Here's my solution, thanks to CodeNaked for the hint:
Point point = cell.PointToScreen(new Point(0, 0));
...
Window myWindow = new Window();
PresentationSource source = PresentationSource.FromVisual(this);
myWindow.Top = point.Y / source.CompositionTarget.TransformToDevice.M22;
myWindow.Left = point.X / source.CompositionTarget.TransformToDevice.M11;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能与非标准 DPI 设置有关,但我很确定该设置会影响所有显示器。 此博客 显示如何获得正确的位置。但代码实际上是:
This may have to do with a non-standard DPI setting, but I'm pretty sure that setting affects all monitors. This blog shows how to get the correct position. But the code is effectively:
您描述的行为不正确,我无法重现它。
我使用以下代码创建了一个简单的窗口:
最后一行(=两个坐标之间的差异)永远不会改变。
The behavior you described is not correct and I can't reproduce it.
I created a simple Window with the following code:
The last line (= the difference between the two coordinates) never changes.
我无法重现您遇到的问题。窗口客户区的左上角(由
PointToScreen
返回的点)始终从窗口左上角水平平移 8 个像素,垂直平移 30 个像素。这是在两个显示器设置上进行的。您应该能够从
SystemParameters
class,但是我必须承认,我不确定到底使用哪些参数来获得系统上的实际值。I'm unable to reproduce the problem your experience. The upper left corner of the client area of the window (the point returned by
PointToScreen
) is always translated 8 pixels horizontally and 30 pixels vertically from the upper left corner of the window. This is on a two-monitor setup.You should be able to compute the values 8 and 30 from the
SystemParameters
class, however I must admit that I'm not sure exactly what parameters to use to arrive at the actual values on my system.