组件高手请看。在组件中添加一个定时器,怎样使定时器的OnTimer事件发生。 (300分)

  • 主题发起人 主题发起人 troyliu
  • 开始时间 开始时间
T

troyliu

Unregistered / Unconfirmed
GUEST, unregistred user!
Enabled:=True;不行呀,怎么办
 
当然可以啊!肯定是别的地方有问题,你把代码贴多点
 
unit BattShape;

interface

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

type
TBattShape = class(TPanel)
Timer1:TTimer;
procedure Timer1Timer(Sender: TObject);
procedure display;
private
{ Private declarations }
BattNumber:integer; //设置字段
BattState:integer;

protected
{ Protected declarations }
procedure WMSize(var Message:TWMSize);message WM_SIZE;
procedure SetNumber(Value:integer);
function GetNumber:integer;
procedure SetState(Value:integer);
function GetState:integer;
procedure SetOnMouseSelect(Value:TMouseMoveEvent);
function GetOnMouseSelect:TMouseMoveEvent;

public
{ Public declarations }
PanelOrder:TPanel; //
BattPanel:TPanel;
LabelState:TLabel; //
Panel1:TPanel; //
constructor Create(AOwner:TComponent);override;

published
{ Published declarations }
property Number:integer read GetNumber write SetNumber; //设置属性
property State:integer read GetState write SetState;
property OnMouseSelect:TMouseMoveEvent read GetOnMouseSelect Write SetOnMouseSelect;
end;

var
state:integer;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TBattShape]);
end;

procedure TBattShape.WMSize(var Message:TWMSize);
begin
inherited;
BattPanel.Width:=Width-8;
BattPanel.Height:=Height - 20;
BattPanel.Height:=Height - 7;
BattPanel.Top:=20+3;
end;

procedure TBattShape.Timer1Timer(Sender:TObject) ;
begin
if state=1 then state:=2
else if state=2 then state:=3
else if state=3 then state:=4
else if state=4 then state:=5
else if state=5 then state:=6
else if state=6 then state:=7
else if state=7 then state:=8
else if state=8 then state:=9
else state:=1;
Timer1.Enabled := False;
display;
end;


constructor TBattShape.Create(AOwner:TComponent);
begin
Parentfont:=true; //电池的大小定义
inherited Create(AOwner);
Height:=200;
width:=90;
Caption:='';
BevelOuter:= bvNone;
Caption:='';

BattPanel:=TPanel.Create(Self);
BattPanel.Parent:=Self;
BattPanel.Width:=Width;
BattPanel.Height:=Height;
BattPanel.left:=0;
BattPanel.Top:=0;
BattPanel.caption:='';
BattPanel.BevelInner :=bvLowered;
BattPanel.BevelOuter :=bvLowered;
BattPanel.color:=clWhite;

LabelState:=TLabel.Create(Self);
LabelState.Parent:=BattPanel;
LabelState.Width:=Width-15;
LabelState.Left:=4;
LabelState.Height:=16;
LabelState.Top:=3;
LabelState.Alignment:=taCenter;
LabelState.AutoSize:=false;
LabelState.Caption:='空';

Timer1:=TTimer.Create(Self);
Timer1.Interval:=400;
Timer1.Enabled:=true;
// Timer1.OnTimer := Timer1.OnTimer ;

Panel1:=TPanel.Create(Self);
Panel1.Parent:=BattPanel;
Panel1.AutoSize:=false;
Panel1.Width:=Width-9;
Panel1.Height:=16;
Panel1.Left:=0;
Panel1.Top:=LabelState.Height;
Panel1.Caption:='';
Panel1.BevelInner :=bvRaised;
Panel1.BevelOuter :=bvRaised;

Number:=0;
State:=1;
end;

procedure TBattShape.SetNumber(Value:integer);
var s1:string[4];
begin
BattNumber:=Value;
str(Value,s1);
end;

function TBattShape.GetNumber:integer;
begin
Result:=BattNumber;
end;


procedure TBattShape.display ;
begin
BattShape.state:= State;
Timer1.Enabled := True;
end;

procedure TBattShape.SetState(Value:integer);
var i:integer;
begin
BattState:=Value;
i:=Value;
if Value = 01 then //无电池
begin
BattPanel.Color:=clRed;
Panel1.Color:=clBlue;
end
else if Value = 02 then //无参数
begin
BattPanel.Color:=clRed;
Panel1.Color:=clWhite;
end
else if Value = 03 then //无参数
begin
BattPanel.Color:=clRed;
Panel1.Color:=clPurple;
end
else if Value =04 then
begin
BattPanel.Color:=clRed;
Panel1.Color:=clFuchsia;
end
else if Value =05 then
begin
BattPanel.Color:=clRed;
Panel1.Color:=clNone;
end
else if Value =06 then
begin
BattPanel.Color:=clRed;
Panel1.Color:=clYellow;
end
else if Value =07 then
begin
BattPanel.Color:=clRed;
Panel1.Color:=clLime;
end
else if Value =08 then
begin
BattPanel.Color:=clRed;
Panel1.Color:=clInfoBk;
end
else if Value =09 then
begin
BattPanel.Color:=clRed;
Panel1.Color:=clSkyBlue;
end
end;


function TBattShape.GetState:integer;
begin
Result:=BattState;
end;

procedure TBattShape.SetOnMouseSelect(Value:TMouseMoveEvent);
begin
BattPanel.OnMouseMove:=Value;
LabelState.OnMouseMove:=Value;
end;

function TBattShape.GetOnMouseSelect:TMouseMoveEvent;
begin
Result:=BattPanel.OnMouseMove;
end;

end.
 
Timer1.Create后,不能直接设置Timer1的属性吗?不对吧,高手快来呀,可以再加分
 
constructor TBattShape.Create(AOwner:TComponent);
。。。。
Timer1:=TTimer.Create(Self);
Timer1.Interval:=400;
// Timer1.Enabled:=False;
// Timer1.OnTimer := Timer1.OnTimer ;
Timer1.OnTimer :=Timer1Timer ; (新增加)
。。。
 
不对呀,有人来吗
 
Timer1:=TTimer.Create(nil);
 
Timer1:=TTimer.Create(Self);
Timer1.Interval:=400;
Timer1.Enabled:=true;
// Timer1.OnTimer := Timer1.OnTimer ;
把注释的这句改成:Timer1.OnTimer := Timer1Timer;
如果你不想只触发一次Timer事件的话,修改Timer1Timer如下:
procedure TBattShape.Timer1Timer(Sender:TObject) ;
begin
Timer1.Enabled := False;
try
if state=1 then state:=2
else if state=2 then state:=3
else if state=3 then state:=4
else if state=4 then state:=5
else if state=5 then state:=6
else if state=6 then state:=7
else if state=7 then state:=8
else if state=8 then state:=9
else state:=1;
display;
finally
Timer1.Enabled := True;
end;
end;
 
接受答案了.
 
Yong对不起,点击错误了,把分都给了Delphi,下次一定加上
 
后退
顶部