delphi按钮第二次移动时消失
我试图使按钮从(0.0)移动到(500.500),为此我使用了循环和线程休眠过程,如上面的代码所示:delphi按钮第二次移动时消失,delphi,button,delphi-xe8,Delphi,Button,Delphi Xe8,我试图使按钮从(0.0)移动到(500.500),为此我使用了循环和线程休眠过程,如上面的代码所示: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
TbuttonAction: TButton;
procedure show(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.show(Sender: TObject);
var
i: Integer;
begin
TbuttonAction.Caption:='my first action';
for i := 0 to 500 do
begin
TThread.Sleep(10);
TbuttonAction.Top:=i;
TbuttonAction.Left:=i;
end;
end;
end.
对于第一次单击,按钮从0.0移动到500.500,但如果我再次单击(当按钮处于500.500时为第二次或第三次),按钮将消失,然后在一段时间后出现。请问如何解决这个问题?我今天开始学习delphi,但我对java有着丰富的经验(3年)。这可能是因为您没有使用消息队列。Windows应用程序需要主UI线程及时地为其消息队列提供服务,以便可以处理绘画和输入之类的事情。你用忙循环阻塞主线程 移除循环,改为添加计时器。计时器由消息循环生成的消息操作,因此不会阻塞主UI线程 给计时器一个适当的间隔,比如100ms。要开始设置动画时,请将计时器的
Enabled
属性设置为True
procedure TForm1.Show(Sender: TObject);
begin
Button1.Left := 0;
Button1.Top := 0;
Timer1.Interval := 100;
Timer1.Enabled := True;
end;
执行计时器的OnTimer
事件,如下所示:
procedure TForm1.Timer1Timer(Sender: TObject);
var
Pos: Integer;
begin
Pos := Button1.Left + 10;
Button1.Left := Pos;
Button1.Top := Pos;
if Pos >= 500 then
Timer1.Enabled := False;
end;
我重新命名了你的按钮。T
前缀用于类型,而不用于实例
作为一项广泛的指导原则,
Sleep
不应在UI程序的主线程中调用。我不认为有多少例外,如果真的有例外的话。睡眠会停止UI线程为其消息队列提供服务 我用的是embarcadero Xe8@JerryDodge我试过了,但还是消失了,循环从0.0开始,我认为使用线程睡眠结束了元素的绘制。是否有启动线程的方法(在移动运动中使用run方法创建新线程?)