Solved

Animated movement of my cursor

Posted on 2002-06-07
3
305 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
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 33

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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