-3

I have 2 windows, which can be different size / different position. The final goal is to be able to click on Window A at a certain position, and send a click on Window B on the same point relatively.

Here's a screen to explain what I'm trying to achieve :

enter image description here

For now I'm amble to retrieve :

  1. Window A Handle/Rect
  2. Window B Handle/Rect
  3. Cursor position on screen

I've heard about ScreenToClient/ClientToScreen and I understand that I need to find the relative mouse position when clicked on Window A, and send the click relatively to Window B

Also, to send my button click I use :

SendMessage(character.MainWindowHandle, 0x201, IntPtr.Zero, CreateParams(?,?);
SendMessage(character.MainWindowHandle, 0x202, IntPtr.Zero, CreateParams(?,?);

What i need to find the the two question marks

My code for now :

var windowAHandle = Win32Api.GetForegroundWindow();
var windowARect = Win32Api.GetWindowRectangle(windowAHandle);
                        
var windowB = PersosEnLigne.First(w => w.Nom == "Character 2");
var windowBRect = Win32Api.GetWindowRectangle(windowB.Process.MainWindowHandle);
var ptTopLeft = new Win32Api.POINT();
var ptBottomRight = new Win32Api.POINT();
ptTopLeft.x = windowARect.Left;
ptTopLeft.y = windowARect.Top;
ptBottomRight.x = windowARect.Right;
ptBottomRight.y = windowARect.Bottom;
Win32Api.ScreenToClient(windowB.MainWindowHandle, ref ptTopLeft);
Win32Api.ScreenToClient(windowB.MainWindowHandle, ref ptBottomRight);

//I'm not even sure if I need to use ScreenToClient or ClientToScreen
Keytrap
  • 421
  • 5
  • 14
  • Does this answer your question? [How to get window's position?](https://stackoverflow.com/questions/9668872/how-to-get-windows-position) – Thomas Weller Nov 01 '20 at 15:51
  • 3
    The rest is math – Thomas Weller Nov 01 '20 at 15:51
  • The problem is the maths, I can't find what I am supposed to calculate. (I'm on this since 2 days and my brain is getting tired sorry) – Keytrap Nov 01 '20 at 15:53
  • @andI'msureI'mmissingsome The link doesn't help me because I already know how to get the window rect – Keytrap Nov 01 '20 at 15:54
  • 4
    @user3673720 please add your attempt to the question - you're not showing any relevant code. What exactly is the issue with your calculations? Please clarify. – CoolBots Nov 01 '20 at 15:55

1 Answers1

1

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.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Thank you very much man ! That was exactly what I was looking for !! Another thing, as I use SendMessage to send click to the other window. How can I convert back the offsetB to position on my entire screen ? (SendMessage needs absolute coordinates) – Keytrap Nov 01 '20 at 16:43
  • 1
    The coords in `offsetB` already are in absolute coords, because [GetWindowRect](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowrect) gives you screen coords: "Retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen." – Idle_Mind Nov 01 '20 at 16:46