Author: Tomas Rutkauskas
As a joke, I am trying to have a button on a form move around the form to avoid the
mouse pointer. I was trying to use the OnMouseMove event of the form to accomplish
this, but have not had any success. Does anybody know a quick way of doing this?
Answer:
1 procedure TMainForm.Button1MouseMove(Sender: TObject; Shift: TShiftState;
2 X, Y: Integer);
3 var4 NewPoint: TPoint;
5 begin6 Randomize;
7 NewPoint.X := X;
8 NewPoint.Y := Y;
9 repeat10 NewPoint.X := NewPoint.X + Random((Sender as TButton).Width div 2);
11 NewPoint.Y := NewPoint.Y + Random((Sender as TButton).Height div 2);
12 until13 PtInRect(ClientRect, NewPoint) andnot PtInRect((Sender as TButton).ClientRect,
14 NewPoint);
15 (Sender as TButton).Left := NewPoint.X;
16 (Sender as TButton).Top := NewPoint.Y;
17 end;