How align mouse clicks X, Y with a specific window handle?

426 views Asked by At

With reference to my previous question, now i have this formula:

X := Round((X * ResolutionX) / Image1.Width); 
Y := Round((Y * ResolutionY) / Image1.Height); // where ResolutionX and ResolutionY is Client screen resolution.

to send mouse click coordenates to remote screen; is accurate and works fine, except when i wish send a click to specific handle of some window, the click not occurs on exactly place eg:

var
  Title: array [0 .. 255] of Char;
begin
  GetWindowText(GetForegroundWindow, Title, 255);
  if Title <> '' then
  begin
    if ContainsStr(string(Title), '- Google Chrome') then
    begin
      WindowHandle := FindWindow(nil, PChar(string(Title))); // works fine to this handle.
      WindowHandle := FindWindowEx(WindowHandle, 0, 'Chrome_RenderWidgetHostHWND', nil); // not works with this, click have a distance of exaclty local.
    end;
  end;
end;

Receiving mouse click:

PostMessage(WindowHandle, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(PosX, PosY));
PostMessage(WindowHandle, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(PosX, PosY));

There is some solution to this?

1

There are 1 answers

4
fpiette On

In WM_LBUTTONDOWN and WM_LBUTTONUP, the coordinates are relative to the window client area. You have to offset the X/Y position by the position of the window and the offset of the window borders.

Pay attention that a window can be a child window, so you must compute the offset for the chain of child-parent relation.

Windows API has an function do to that: MapWindowPoints.