Mouse Click without Moving Screen Cursor

Asked

Viewed 3,236 times

1

I’m making a simple system, in this FORM has 2 button, one that performs the function and another that I put a SHOWMESSAGE('teste ok'); and I took the POSITION of this second BUTTON, remembering that I need to use the SENDMESSAGE for the MOUSE CURSOR not to move on the screen, so I made the following program:

Procedure SendMouseClick(x,y:Integer);
var
h:THandle;
begin
h := FindWindow(nil, 'TForm1');
SendMessage(h, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x,y));
sleep(10);
SendMessage(h, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(x,y));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
SendMouseClick(1042,538);
end;

It turns out I click and just do nothing on my FORM. What I might be missing?

3 answers

3

Maybe I misunderstood, but from what I understand you want to just fire the function of the 2nd button click. If so, just call the function directly. Example:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Button2Click(Sender);  // pode ser chamada em qualquer lugar, em uma outra função, em um timer, em um evento OnClick do form...
  // Se for o caso em algumas funções não terás o Sender, basta substituir por qualquer outro objeto. Sender identifica que objeto está "disparando" o click.
end;

1

At the event OnClick of Button1 put the following code:

SendMessage(Button2.Handle, WM_LBUTTONDOWN, MK_LBUTTON, 0); 
SendMessage(Button2.Handle, WM_LBUTTONUP, MK_LBUTTON, 0); 

This will make clicking on Button1 the message will be sent WM_LBUTTONDOWN to the Handle of Button2. The message WM_LBUTTONDOWN is used when the user presses left mouse button, already the message WM_LBUTTONUP is used when we want release the left mouse button.

To get more organized create a procedure that does this.

procedure SimularClick(H: HWND);
begin
SendMessage(H, WM_LBUTTONDOWN, MK_LBUTTON, 0); 
SendMessage(H, WM_LBUTTONUP, MK_LBUTTON, 0);
end;

In the OnClick of Button just call:

SimularClick(Button2.Handle);

Good luck.

  • Friend, thank you her reply.... and if I want to click a certain position out of the FORM, as I should do ?

  • @user7605 Stackoverflow is not like a forum. If the answer satisfied your question you should accept it as an answer. If you want a slightly different answer, change your question including what you need. If you need to ask another question then, well... ask another question.

  • I already changed my question friend!!

1

Try to use Mouse_event

{Move the mouse}

Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE, COORDENADA X, COORDENADA Y, 0, 0);

{Simulates pressing the left mouse button}

Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN, COORDENADA X, COORDENADA Y, 0, 0);

{ Simulate dropping the left mouse button }

Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, COORDENADA X, COORDENADA Y, 0, 0);

Light: http://www.devmedia.com.br/delphi-simular-clique-do-mouse/12109

  • 1

    Friend, Mouse_event unfortunately MOVE THE MOUSE CURSOR. I would like to use SENDMESSAGE or POSTMESSAGE that does not "move" Cursor understands ?

  • So try to put Findwindow('Tform1', nil)

  • Put Findwindow where ? Using Mouse_event ? Or Sendmessage ? If you go with SENDMESSAGE I’ve done it up there, check it out.. thanks!

  • Try inverting the parameters of h := Findwindow(nil, 'Tform1'); for h := Findwindow('Tform1', nil);

  • I tested now, same thing friend, just does nothing on the screen.... h := Findwindow('Tform1', nil);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.