下面是我的一段测试程序,按a更改鼠标位置,将鼠标定位到一个label的中心
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
labellist:array of tlabel;
procedure MoveTOLabel(lab:tlabel);
public
{ Public declarations }
end;
var
Form1: TForm1;
x:integer;
implementation
{$R *.dfm}
procedure TForm1.MoveTOLabel(lab:tlabel);
var
p:tpoint;
begin
p.X:=lab.Left+lab.Width shr 1;
p.Y:=lab.Top+lab.Height shr 1;
p:=lab.Parent.ClientToScreen(p);
setcursorpos(p.X,p.Y);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
lab:tlabel;
i,t,l:integer;
begin
x:=0;
setlength(labellist,20);
t:=10;
l:=10;
for i:=0 to 19 do begin
lab:=tlabel.Create(self);
labellist:=lab;
lab.Caption:='Lable'+inttostr(i+1);
lab.SetBounds(l,t,50,20);
inc(l,2);
inc(t,25);
lab.Parent:=self;
end;
end;
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
if key='a' then begin
movetolabel(labellist[x]);
if x<length(labellist)-1 then
inc(x)
else
x:=0;
end;
end;
end.