C# Get the mouse position relative to another process Window [closed]

Once you have WindowA rectangle, find the offset position of the current cursor position relative to the top left of the WindowA rectangle:

Point offsetA = new Point(Cursor.Position.X - windowARect.Left, Cursor.Position.Y - windowARect.Top);

Now compute the “percentage” of this offset relative to the size (width/height) of WindowA:

double xPct = (double)offsetA.X / (double)(windowARect.Right - windowARect.Left + 1);
double yPct = (double)offsetA.Y / (double)(windowARect.Bottom - windowARect.Top + 1));

Now you can find the “same” spot in WindowB by finding its width/height, multiplying by the “percent”, and adding that number to the top left of WindowB:

int xOffsetB = (int)((double)(windowBRect.Right - windowBRect.Left + 1) * xPct);
int yOffsetB = (int)((double)(windowBRect.Bottom - windowBRect.Top + 1) * yPct);
Point offsetB = new Point(windowBRect.Left + xOffsetB, windowBRect.Top + yOffsetB);

Now you can use the X, Y values in offsetB to know where to click in the second window.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top