0

I have an application with two windows, window A and window B. Window A has a button called CoordinateButton that I pass to the following function:

 private Point FindCoordinates(UIElement CoordinateButton)
 {
     return System.Windows.Input.Mouse.GetPosition(CoordinateButton);
 }

If I click on the button in Window A, this function returns a Point with small numbers like (15, 12), meaning that I am 15 pixels to the right of the top left corner of the button, and 12 pixels below - the cursor is really close to the measurement point.

If I click on a button in Window B, Window A will again call the function, and pass in the CoordinateButton. Even if the cursor is only a few pixels away from the CoordinateButton, GetPosition( ) still returns crazy values like (-1125, -2076), but the actual mouse is definitely within 100 pixels of the button. Any idea what is happening?

I've also tried calculating the difference using things like

System.Windows.Forms.Cursor.Position - element.PointToScreen(new Point(0,0))

but my calculation ends up a few dozen pixels off.

I have also looked into scaling suggestions like https://stackoverflow.com/a/26549677/5617177 , but they had no effect.

bwall
  • 984
  • 8
  • 22
  • I guess you mean `System.Windows.Input.Mouse.GetPosition`. Double-check in the debugger, whether your program really passes the correct element/button and not some other UI element. What happens if you let your code set the focus manually to the Window A before calling FindCoordinates? Would that change the result? –  Sep 14 '18 at 20:55
  • @elgonzo Oops. I did indeed mean to include 'Mouse' in that statement. After double checking the debugger, I can confirm that the correct element is passed into the function. I set the focus to window A using SetForegroundWindow( ) from the user32.dll, but that also had no effect. – bwall Sep 17 '18 at 15:03

0 Answers0