在Form上放TLabel1...5五个,TTimer一个
Timer1.Interval := 1;
代码如下,在D5+Win2000下调试通过:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
iX0 = 200; //圆心X坐标
iY0 = 200; //圆心Y坐标
iRad = 100; //半径
var
Form1 : TForm1;
pMouse : TPoint; //鼠标
implementation
{$R *.DFM}
procedure TForm1.Timer1Timer(Sender: TObject);
var
iX, iY, iX1, iY1 : longint; //鼠标坐标
fX, fY : double; //圆周上的点坐标
fLength : double; //与圆心距离
begin
GetCursorPos(pMouse); //获得当前鼠标位置
iX := pMouse.x;
iY := pMouse.y;
if iX > Left then
iX := pMouse.x - Left
else
iX := pMouse.x;
if iY > Top then
iY := pMouse.y - Top
else
iY := pMouse.y;
Label1.Caption := inttostr(iX);
Label2.Caption := inttostr(iY);
fLength := sqrt((iX - iX0) * (iX - iX0) + (iY - iY0) * (iY - iY0));
Label5.Caption := floattostr(fLength);
fX := iRad * abs(iX - iX0) / flength;
fY := iRad * abs(iY - iY0) / flength;
Label3.Caption := floattostr(fX);
Label4.Caption := floattostr(fY);
if (iX > iX0) and (iY < iY0) then
begin
iX1 := Round(fX + iX0) + Left;
iY1 := abs(Round(fY - iY0)) + Top;
end;
if (iX > iX0) and (iY > iY0) then
begin
iX1 := Round(fX + iX0) + Left;
iY1 := Round(fY + iY0) + Top;
end;
if (iX < iX0) and (iY < iY0) then
begin
iX1 := abs(Round(iX0 - fX)) + Left;
iY1 := abs(Round(fY - iY0)) + Top;
end;
if (iX < iX0) and (iY > iY0) then
begin
iX1 := abs(Round(iX0 - fX)) + Left;
iY1 := Round(fY + iY0) + Top;
end;
if fLength > iRad then
SetCursorPos(iX1, iY1); //移动鼠标
end;
end.