procedure TForm1.Button4MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
i:integer;
begin
Randomize;
i:=Random(3);//随机定按钮移动的方向
case I of
0: Button4.Left:=button4.left+80;
1: Button4.Left:=button4.left-80;
2: Button4.top:=button4.top+80;
3: Button4.top:=button4.top-80;
end;
//如果按钮移动到窗体外面那么就把它拉回来
//也可以判断如果超出窗体那么随机定它的位置
//这里如果超出就拉回到一个固定位置
if Button4.left<0 then Button4.left:=120;
if Button4.left>Width-button4.Width then Button4.left:=120;
if Button4.top<0 then Button4.top:=120;
if Button4.top>self.ClientHeight-button4.Height then Button4.top:=120;
end;