unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    MyFirstButton: TButton;
    procedure MyFirstButtonClick(Sender: TObject);
    procedure MyFirstButtonMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MyFirstButtonClick(Sender: TObject);
begin
 Close;
end;

procedure TForm1.MyFirstButtonMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var
 index:integer;
begin
 index:=random(4);
 case index of
  0: MyFirstButton.Left:=MyFirstButton.Left+MyFirstButton.Width;
  1: MyFirstButton.Left:=MyFirstButton.Left-MyFirstButton.Width;
  2: MyFirstButton.Top:=MyFirstButton.Top+MyFirstButton.Height;
  3: MyFirstButton.Top:=MyFirstButton.Top-MyFirstButton.Height;
 end;

 if MyFirstButton.Left<0 then MyFirstButton.Left:=0;
 if (MyFirstButton.Left+MyFirstButton.Width)>Form1.Width then MyFirstButton.Left:=Form1.Width-MyFirstButton.Width;
 if MyFirstButton.Top<0 then MyFirstButton.Top:=0;
 if (MyFirstButton.Top+MyFirstButton.Height)>Form1.Height then MyFirstButton.Top:=Form1.Height-MyFirstButton.Height;
end;

end.
