Solved

Animated movement of my cursor

Posted on 2002-06-07
3
298 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
Comment Utility
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
Comment Utility
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
Comment Utility
Slick812,
Can you help me to do same work without a timmer ?
Alike While ...
Anothers 100 points for that.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now