1

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 Answers1

0

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.

fpiette
  • 11,983
  • 1
  • 24
  • 46
  • 2
    Look at using `ScreenToClient()` or `MapWindowPoints()` to address this. – Remy Lebeau May 03 '21 at 16:04
  • 1
    @RemyLebeau OK for MapWindowPoints but probably not OK for ScreenToClient because the former is Windows API while the later is Delphi object method working when you have the corresponding class reference at hand. The OP has to act on any window for any application. – fpiette May 03 '21 at 16:14
  • @fpiette I was referring to the Win32 [`ScreenToClient()`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-screentoclient) function, not the [`TControl.ScreenToClient()`](http://docwiki.embarcadero.com/Libraries/en/Vcl.Controls.TControl.ScreenToClient) class method. – Remy Lebeau May 03 '21 at 18:20
  • @RemyLebeau, i solved with [`ClientToScreen()`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-clienttoscreen) - `ClientToScreen(WindowHandle, Pt); PosX := PosX - Pt.X; PosY := PosY - Pt.Y;` –  May 03 '21 at 20:45