Incorrect coordinates on multiple monitors

I have a Datagrid and I want to know the position of the datacell to overlay it on the window.
It works great with just one monitor, but with multiple monitors the window shifts.
Here is the code:

Point point = cell.PointToScreen(new Point(0, 0)); ... Window myWindow = new Window(); myWindow.Top = point.Y; myWindow.Left = point.X; 

Does anyone have experience positioning on multiple monitors?

EDIT:
I did the 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 & ==> There is always a difference of 30 (may be caused by the header)
- Dual monitor: 0 ↔ 30; 20 ↔ 55; 100 ↔ 153 & ==> 0.0 times the difference is 30. But the more I moved the window from 0.0. the greater the difference becomes, but it must remain unchanged. Very strange!

EDIT2:
Here is my solution, thanks to CodeNaked for a 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; 
+6
source share
3 answers

This may be related to non-standard DPI settings, but I'm sure the settings affect all monitors. This blog shows how to get the right position. But the code is efficient:

 PresentationSource source = PresentationSource.FromVisual(control); double dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11; double dpiY = 96.0 * source.CompositionTarget.TransformToDevice.M22; window.Left = point.X * 96.0 / dpiX; window.Top = point.Y * 96.0 / dpiY; 
+5
source

The behavior you described is incorrect, and I cannot reproduce it. I created a simple window with the following code:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); LocationChanged += (s, e) => { var screen = PointToScreen(new Point(0, 0)); var window = new Point(Left, Top); var diff = screen - window; textbox.Text = window.ToString() + Environment.NewLine + screen.ToString() + Environment.NewLine + diff; }; } } 

The last line (= the difference between the two coordinates) never changes.

+1
source

I can not reproduce the problem associated with your experience. The upper left corner of the client area of ​​the window (the point returned by PointToScreen ) always translates into 8 horizontal pixels and 30 vertical pixels from the upper left corner of the window. It is on a dual monitor setup.

You should be able to calculate values ​​8 and 30 from the SystemParameters class, however I must admit that I'm not sure exactly which parameters to use to get the actual values ​​on my system.

0
source

Source: https://habr.com/ru/post/888313/


All Articles