gpt4 book ai didi

delphi - 只有最后添加的 Delphi 组件才会执行操作

转载 作者:行者123 更新时间:2023-12-02 12:19:18 25 4
gpt4 key购买 nike

我在Delphi中创建了自己的组件(它就像一个可以移动的按钮),并安装了它。然后我创建一个新项目并从那里添加一些新的组件元素。但只有最后添加的一个能够移动!其他人则不然。为什么会发生这种情况?我该如何解决它?

这是组件代码:

unit ModifiedButton;

interface

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


var Timer: TTimer;

type
TSpeed = (Slow,Normal,Fast);
TModifiedButton = class(TButton)
private
{ Private declarations }
FCount:integer;
Velocity:integer;
FSpeed:TSpeed;

protected
{ Protected declarations }
procedure Click;override;
procedure Move(Vel:Integer);
procedure OnTimer(Sender: TObject);
public
{ Public declarations }
procedure ShowCount;
published
{ Published declarations }
property Count:integer read FCount write FCount;
property Speed: TSpeed read FSpeed write FSpeed;

constructor Create(aowner:Tcomponent); override;

end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('MyComponents', [TModifiedButton]);
end;

{ TModifiedButton }

procedure TModifiedButton.Click;
begin
inherited Click;
FCount:=FCount+1;
if (Speed = Slow) then
Velocity:=2;
if (Speed = Normal) then
Velocity:=10;
if (Speed = Fast) then
Velocity:= 20;
Timer.Enabled:=True;


end;

constructor TModifiedButton.Create(aowner: Tcomponent);
begin
inherited Create(aowner);
Timer:=TTimer.Create(self);
Timer.Enabled:=false;
Timer.OnTimer:=OnTimer;
Timer.Interval:=10;

end;

procedure TModifiedButton.Move(Vel: Integer);
begin
Left:=Left + Vel;
end;

procedure TModifiedButton.OnTimer(Sender: TObject);
begin
Move(Velocity);
end;

procedure TModifiedButton.ShowCount;
begin
ShowMessage('You cliked '+ caption+' for '+inttostr(FCount)+' times');
end;

end

.

最佳答案

由于Timer 是一个全局变量,因此您创建的每个新按钮都会覆盖前一个按钮的OnTimer 事件处理程序。解决方案,使计时器成为您的 TModifiedButton 类的成员:

unit ModifiedButton;

interface

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

type
TSpeed = (Slow,Normal,Fast);
TModifiedButton = class(TButton)
private
{ Private declarations }
FCount:integer;
Velocity:integer;
FSpeed:TSpeed;
Timer: TTimer;

protected
{ Protected declarations }
procedure Click;override;
procedure Move(Vel:Integer);
procedure OnTimer(Sender: TObject);
public
{ Public declarations }
procedure ShowCount;
published
{ Published declarations }
property Count:integer read FCount write FCount;
property Speed: TSpeed read FSpeed write FSpeed;
constructor Create(aowner:Tcomponent); override;
destructor Destroy; override;
end;

procedure Register;

implementation

{ TModifiedButton }

procedure TModifiedButton.Click;
begin
inherited Click;
FCount:=FCount+1;
Case Speed of
Slow : Velocity:=2;
Normal : Velocity:=10;
Fast : Velocity:= 20;
end;
Timer.Enabled:=True;
end;

procedure TModifiedButton.Move(Vel: Integer);
begin
Left:=Left + Vel;
end;

procedure TModifiedButton.OnTimer(Sender: TObject);
begin
Move(Velocity);
end;

procedure TModifiedButton.ShowCount;
begin
ShowMessage(Format('You clicked %s for %d times', [Caption, FCount]));
end;

constructor TModifiedButton.Create(AOwner: Tcomponent);
begin
inherited Create(AOwner);
Timer := TTimer.Create(self);
Timer.Enabled:=false;
Timer.OnTimer:=OnTimer;
Timer.Interval:=10;
end;

destructor Destroy;
begin
Timer.Enabled := False;
Timer.Free;
end;

procedure Register;
begin
RegisterComponents('MyComponents', [TModifiedButton]);
end;

end.

关于delphi - 只有最后添加的 Delphi 组件才会执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20444548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com