Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make a cursor automatically move

Status
Not open for further replies.

Micky3

Programmer
Feb 26, 2004
2
GB
Hello, im trying to get a cursor to move from one point of the screen to another, as if someone was moving the mouse. I do not want it to suddenly appear on at point b I would like it to go gradually in a straight line to point b.

Does anybody have any suggestions how i might achieve this.
 
I can't remember, but I think because the mouse is an input device, you can't control it like that. I suppose you could just make the mouse cursor invisible, then draw your icon over the background, moving it from point A to point B using linear interpolation.

Chris
 
One way to handle it would be to use a TTimer component. In the OnTimer event, you can read the current cursor position and then modify the cursor position until the cursor is where you want it, then disable the timer.

Here's an example that shows moving the cursor in a OnTimer event, you can modify it depending on your requirements:

Code:
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  POINT p;
  static int x=0,y;
  #define finalX 500
  #define finalY 200

  if (!x) {
    GetCursorPos(&p);
    x = p.x;
    y = p.y;
  }
  if (x < finalX) x++;
  else if (x > finalX) x--;
 
  if (y < finalY) y++;
  else if (y > finaly) y--;

  SetCursorPos(x,y);

  if (x == finalX && y == finalY) 
    Timer1->Enabled = false;

}

You can play with the timer interval etc. to control the speed of the cursor movement. You should also be able to modify the code with linear interpolation included to move the cursor in a straight line. Also, the x & y of the point from GetCursor are screen coordinates, you'll probably need to interpolate these points to your form coordinates.

Hope this helps.
 
Hello, this has helped yet I do not know much about linear interpolation, or how I would be able to implement linear interpolation in c++.

Any more help would be appreciated. Cheers Micky.
 
Linear interpolation simply means that if you should move the cursor from 10,10 to 110,310 (X,Y), you would then have to move the cursor position 3X for each Y until you reach the target.

There are different ways to accomplish that but you'll have to figure that out yourself.

Totte
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top