1

I have application with the TWebBrowser component on the main form.

I need to emulate clicks in the TWebBrowser window and I use Windows messages for that.

I send WM_MOUSEMOVE, WM_SETCURSOR, WM_LBUTTONDOWN, WM_MOUSEACTIVATE, WM_LBUTTONUP and this work fine when application is in normal state, browser acts as was real mouse clicked.

Also this works fine when application is in minimized state

But in moment of application minimizing or restoring, browser don't react on the clicks and I have missed clicks (this is critical for my app)

What wrong with messages in moment of application minimizing/restoring?

Or

Is this possible to detect that application is in minimizing/restoring state right now?

Vlad__1
  • 17
  • 4

1 Answers1

0

I have an application I wrote that emulates clicking in Windows. The code below isn't a complete program, but should help you to simulate a click. I actually used a IdThreadComponent to create mine instead of the Timer, but the code I have would be too long for demonstration of this code.

The problem with your method relies on windows messaging system. The code below will create "actual mouse clicks" instead of simulated. The higher your priorityclass of your application the more reliable the clicking will be.

You might need to run your program "Run as Administrator", but not sure. If it doesn't work, try to see if running in Administrator helps?

You also might want to set your application to a higher priority.

Here is some of the code I extracted from a program I wrote for simulating clicks:

uses
  Winapi.Windows

var
  P: TPoint; // Global variable

procedure TForm1.SetPriorityLevel(P: byte);
begin
  case p of
  1: Setpriorityclass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  2: Setpriorityclass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
  3: Setpriorityclass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  4: Setpriorityclass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  eu: array [0..1] of TInput;
begin
    SetCursorPos(P.X,P.Y);

    ZeroMemory(@eu,sizeof(eu));

    eu[0].Itype := INPUT_MOUSE;
    eu[0].mi.dwFlags := MOUSEEVENTF_LEFTDOWN;

    eu[1].Itype := INPUT_MOUSE;
    eu[1].mi.dwFlags :=MOUSEEVENTF_LEFTUP;

    if SendInput(2,eu[0],sizeof(TInput)) > 0 then
    begin
        inc(ClickCount);
    end
    else
    begin
      Label1.Caption := 'SendInput Failed..Sleeping';
      sleep(50);
    end;
end;

Please let me know if you have other questions.

More information about SendInput from here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput and here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput

El Diablo
  • 168
  • 1
  • 12
  • Thanks for your answer, but program should work in minimized state and not interfere with the user. So I need exactly emulation without changing the real cursor position... – Vlad__1 Mar 11 '20 at 15:27
  • Are you using FindWindow to get the handle of your application or ?? – El Diablo Mar 14 '20 at 00:09
  • The answer here isn't in Delphi, but should help you: https://stackoverflow.com/questions/10279812/simulate-click-into-a-hidden-window – El Diablo Mar 14 '20 at 00:13