Solved

Animated movement of my cursor

Posted on 2002-06-07
3
326 Views
Last Modified: 2010-04-04
I know how can I move my mouse, but I wish to create an animated movement and not just a jump to the new position.
Can give me some codes for how can I move animated (step by step )the cursor from Point1 to Point2?

Nick
0
Comment
Question by:ginsonic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7064043
a sample to move the mouse in 250 steps

procedure TForm1.Button1Click(Sender: TObject);
var
  p : Tpoint;
  I : Integer;
  stepx, Stepy : Integer;
begin
  getcursorpos(p);
  setcursorPos(0,0);
  stepx := screen.width div 250;
  stepy := screen.height div 250;
  for i := 1 to 250 do
    setcursorpos(stepx*i,stepy*i);
  setcursorpos(p.x,p.y);
end;

meikl ;-)
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 100 total points
ID: 7064833
hello, here's some code that will move the cursor using a Multi Media Timer, and some Trig functions to calculate the distance and relative X and Y movement of the cursor.


uses mmsystem, Math;

private
    { Private declarations }
    EndPos, StartPos, Mover: TPoint;
    PixelMov: Word;
    Radian1: Extended;
    TimerID: Cardinal;
    TimeCaps: TTimeCaps;
    procedure MoveCursor;


procedure TForm1.FormCreate(Sender: TObject);
begin
TimeGetDevCaps(@TimeCaps, sizeof(TimeCaps));
{the line above is NEEDED to set up your MultiMedia Timer
which is much better for movement, animation, sprite drawing than
a TTimer.}
end;


procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
begin
{the MM Timer is running outside of your app, so be sure to kill it}
timeKillEvent(TimerID);
TimeEndPeriod(TimeCaps.wPeriodMin);
end;


procedure TimerProc(uTimerID, uMessage: cardinal; dwUser, dw1, dw2: integer); stdcall;
begin
Form1.MoveCursor;
{this procedure is called no matter if the code from the last call has finished or not.
so it's probally better to not run any code here but just call another procedure.
With only one timer, I don't need to use any of the Params here either}
end;

procedure TForm1.MoveCursor;
begin
SetCursorPos(Round(StartPos.x-Cos(Radian1)*(PixelMov*Mover.x)),
             Round(StartPos.y-Sin(Radian1)*(PixelMov*Mover.x)));
Inc(Mover.x);
if (Mover.x > Mover.y) or (Mover.x > 3000) then
  begin
  timeKillEvent(TimerID);
  TimeEndPeriod(TimeCaps.wPeriodMin);
  SetCursorPos(EndPos.x, EndPos.y)
  end;
end;

procedure TForm1.sbut_MoveCurClick(Sender: TObject);
var
i: Integer;
Interval,Accur: Word;
begin
GetCursorPos(StartPos);
EndPos.x := Screen.Width div 2;
EndPos.y := Screen.Height div 2;
PixelMov := 13;
{the PixelMov is the amount of pixel movement for ONE call to MoveCursor}
Mover.x := 0;
Mover.y := Round(Sqrt(SumOfSquares([abs(EndPos.y-StartPos.y),abs(EndPos.x-StartPos.x)]))) div PixelMov;
Radian1 := DegToRad(180 * (1 + ArcTan2(EndPos.y-StartPos.y, EndPos.x-StartPos.x) / Pi));
timeKillEvent(TimerID);
TimeEndPeriod(TimeCaps.wPeriodMin);
TimeBeginPeriod(TimeCaps.wPeriodMin);

Interval := 34;
{intervals less than 20 use up alot of processor time, it's better to Increase
PixelMove than decrease Interval}
Accur := Interval div 2;
if Accur < TimeCaps.wPeriodMin then
Accur := TimeCaps.wPeriodMin;
if Accur > TimeCaps.wPeriodMax then
Accur := TimeCaps.wPeriodMax;
TimerID := TimeSetEvent(Interval, Accur, @TimerProc, 10, TIME_PERIODIC);
end;

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

the mmtimer will keep running even if there is a System Modal dialog Box up (Ctrl+Alt+Del), it does not matter so much with
cursor movement, but other things might. This works for me,
Wes
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7092705
Slick812,
Can you help me to do same work without a timmer ?
Alike While ...
Anothers 100 points for that.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question