将按钮位置更改为表格上的随机位置

时间:2019-07-17 12:32:26

标签: delphi

我在表单上有一个按钮,我希望当用户单击按钮时将按钮移动到表单上的随机位置。每次用户单击按钮时,它都必须移到新的随机位置。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

VCL

Randomize;
(Sender as TButton).Left := Random(ClientWidth - (Sender as TButton).Width);
(Sender as TButton).Top := Random(ClientHeight - (Sender as TButton).Height);

FMX

Randomize;
(Sender as TButton).Position.X := Random(ClientWidth - Round((Sender as TButton).Width));
(Sender as TButton).Position.Y := Random(ClientHeight - Round((Sender as TButton).Height));

IntraWeb

Randomize;
(Sender as TIWButton).Left := Random(ClientWidth - (Sender as TIWButton).Width);
(Sender as TIWButton).Top := Random(ClientHeight - (Sender as TIWButton).Height);