Jump to content
ThomasRiedelDk

Simulate mousedrag

Recommended Posts

I have this code, that simulates a mouse drag, moving the form:

procedure TForm1.SimulateMouseDrag(FromX, FromY, ToX, ToY: Integer);
begin
  SetCursorPos(Fromx, Fromy);
  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);  // Simulate mouse down
  sleep(100);
  mouse_event(MOUSEEVENTF_MOVE, ToX-FromX, ToY-FromY, 0, 0);  // Simulate mouse move
  sleep(100);
  SetCursorPos(Tox, Toy);
  mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);  // Simulate mouse up
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   SimulateMouseDrag(left+5, top+5, left+200, top+200);
end;

 

It works, but the form shrinks during the operation. What am I missing?

Share this post


Link to post

No comments on why it shrinks but I don't think your Sleep(100) does what you think they do.

Your main thread isn't doing anything while the Sleep executes so if the purpose was to let it process the messages generated by mouse_event then you will have to do that some other way. Application.ProcessMessages *shudder* comes to mind.

Share this post


Link to post

Sleep does nothing, and that is intended..

2 minutes ago, Anders Melander said:

No comments on why it shrinks but I don't think your Sleep(100) does what you think they do.

Your main thread isn't doing anything while the Sleep executes so if the purpose was to let it process the messages generated by mouse_event then you will have to do that some other way. Application.ProcessMessages *shudder* comes to mind.

 

Share this post


Link to post
14 minutes ago, ThomasRiedelDk said:

Sleep does nothing, and that is intended..

I'm curious; What's the purpose then?

Share this post


Link to post
1 hour ago, ThomasRiedelDk said:

What am I missing?

At +5 pixel, most likely you are resizing the form from the corner, try to increase this, or pick a point where no resizing might happen, +10 do it , like this

SimulateMouseDrag(left+10, top+10, left+200, top+200);

 

Share this post


Link to post
8 minutes ago, Kas Ob. said:

At +5 pixel, most likely you are resizing the form from the corner, try to increase this, or pick a point where no resizing might happen, +10 do it , like this


SimulateMouseDrag(left+10, top+10, left+200, top+200);

 

Thanks for the reply, but it did not help, - still resizing the form. And the cursor turns into updown arrow shape.

Share this post


Link to post

The comment from @Kas Ob. is correct. To always be on the safe side, you can use the following code to calculate the correct position:

  var P0 := Point(Left, Top);
  var dx := Width div 2; // X center of form
  var dy := (Height - ClientHeight + (Width - ClientWidth) div 2) div 2; // Y center of title
  P0.Offset(dx, dy);
  var P1 := P0;
  P1.Offset(200, 200);
  SimulateMouseDrag(P0.X, P0.Y, P1.X, P1.Y);

Note that this might need some more calculations under high dpi situations.

Edited by Uwe Raabe

Share this post


Link to post
10 minutes ago, Uwe Raabe said:

The comment from @Kas Ob. is correct. To always be on the safe side, you can use the following code to calculate the correct position:


  var P0 := Point(Left, Top);
  var dx := Width div 2; // X center of form
  var dy := (Height - ClientHeight + (Width - ClientWidth) div 2) div 2; // Y center of title
  P0.Offset(dx, dy);
  var P1 := P0;
  P1.Offset(200, 200);
  SimulateMouseDrag(P0.X, P0.Y, P1.X, P1.Y);

Note that this might need some more calculations under high dpi situations.

Right you are.

Actually I did not even know, that forms were resizable from the top.

I tried
var dy := (Height - ClientHeight) div 2;

It works as well.

 

Thanks

 

Share this post


Link to post
13 minutes ago, ThomasRiedelDk said:

Thanks for the reply, but it did not help, - still resizing the form. And the cursor turns into updown arrow shape.

Thanks. You are right. The resize area in the Top bar is quite large, and I was not aware that it even existed.

Share this post


Link to post
37 minutes ago, ThomasRiedelDk said:

I tried
var dy := (Height - ClientHeight) div 2;

It works as well.

Technically you also get half of the bottom frame with that. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×