一个简单控件,可是有点问题,请大家帮忙看看? (100分)

  • 主题发起人 主题发起人 book523
  • 开始时间 开始时间
B

book523

Unregistered / Unconfirmed
GUEST, unregistred user!
写了一个简单控件,
显示时间的label,
可是还有2个问题:
1。timer属性在设计时,修改不起作用,总是等于1899-12-30+系统时间
2。把该控件拖到窗体上时,caption为timepanel+序号,为什么不等于系统时间(我在
create事件中已经设置了);
3。把该控件从窗体上删除时,会发生错误,好像是地址访问错误,是不是timer的
释放方法不对,应该怎么释放?

unit MyCustomPanel;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,DateUtils;

type
TTimePanel = class(TCustomPanel)
private
{ Private declarations }
FTime:TTime;
FMyTimer:TTimer;
procedure Ontimer(Sender:TObject);
procedure SetTime(value:TTime);
protected
{ Protected declarations }
public
property DockManager;
constructor Create(AOwner: TComponent); override;
Destructor Destroy;Override;
procedure ResetTime;
{ Public declarations }
published
{ Published declarations }
property Align;
property Alignment;
property Anchors;
property AutoSize;
property BevelInner;
property BevelOuter;
property BevelWidth;
property BiDiMode;
property BorderWidth;
property BorderStyle;
property Caption;
property Color;
property Constraints;
property Ctl3D;
property UseDockManager default True;
property DockSite;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property FullRepaint;
property Font;
property Locked;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnCanResize;
property OnClick;
property OnConstrainedResize;
property OnContextPopup;
property OnDockDrop;
property OnDockOver;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGetSiteInfo;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property OnUnDock;
property Time:TTime Read FTime Write SetTime;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('hxw', [TTimePanel]);
end;
Constructor TTimePanel.Create(AOwner:TComponent);
begin
Inherited;
FMyTimer:=TTimer.Create(self);
FMyTimer.Interval:=1000;
FMyTimer.Ontimer:=OnTimer;
FMyTimer.Enabled:=True;
Ftime:=SysUtils.Time;
Caption:=TimeToStr(Ftime);

end;
Destructor TTimePanel.Destroy;
begin
FMyTimer.Free;
Inherited;
end;
procedure TTimePanel.SetTime(Value:TTime);
begin
if FTime <> Value then
begin
Ftime:=Value;
Caption:=TimeToStr(FTime);
end;

end;
procedure TTimePanel.OnTimer(Sender:Tobject);
var
T:TDateTime;
begin
T:=FTime;
FTime:=IncSecond(T,1);
Caption:=TimeToStr(FTime);
end;
procedure TTimePanel.ResetTime ;
begin
Ftime:=StrToTime('00:00:00');
Caption:='00:00:00';
end;
end.
 
找到问题了,
Constructor TTimePanel.Create(AOwner:TComponent);
begin
Inherited;
FMyTimer:=TTimer.Create(self);
FMyTimer.Interval:=1000;
FMyTimer.Ontimer:=OnTimer;
FMyTimer.Enabled:=True;
Caption:=TimeToStr(SysUtils.Time);//顺序换了一下,就行了
Ftime:=StrToTime(Caption);//
end;
大家讨论一下,这两句顺序不一样,有什么不同吗?
 
这有什么好讨论的
 
可是问什么开始会出那样的问题?
大家发发言啊????????、
 
不好意思,我试过没出错,
理论上也不会出错。
重启,
查毒吧。
哈。
 
现在说正题:(我有个骂人的习惯,先说过)
>>>>1。timer属性在设计时,修改不起作用,总是等于1899-12-30+系统时间
我呸!你自己写的代码还问我为什么, 在构造过程中加下面的代码就这样
FMyTimer.Enabled:=True;
是不是还没从Easy programe 中解脱出来呀。
按你的想法,最牛逼是用csDesignning in componentstate 这个判断。
不过你的想法不太想OOPer的想法。
>>>>2。把该控件拖到窗体上时,caption为timepanel+序号,为什么不等于系统时间
>>>>(我在create事件中已经设置了);
我对,没办法
>>>>3。
不说了你的机器一定有问题, 不行就重装吧。


拿钱。哈
 
我也有一个小问题,我继承ToolWin然后在上面创建button,但是对button设置visible:=false根本不管用.
不知道为什么?
 
找到原因了。这是原码,大家试用一下:
unit MyCustomPanel;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,DateUtils;

type
TTimePanel = class(TCustomPanel)
private
{ Private declarations }
FTime:TTime;
FMyTimer:TTimer;
procedure Ontimer(Sender:TObject);
procedure SetTime(value:TTime);
function GetActive:boolean;
procedure SetActive(Value:Boolean);
protected
{ Protected declarations }
public
property DockManager;
constructor Create(AOwner: TComponent); override;
Destructor Destroy;Override;
procedure ResetTime;
{ Public declarations }
published
{ Published declarations }
property Align;
property Alignment;
property Anchors;
property AutoSize;
property BevelInner;
property BevelOuter;
property BevelWidth;
property BiDiMode;
property BorderWidth;
property BorderStyle;
property Color;
property Constraints;
property Ctl3D;
property UseDockManager default True;
property DockSite;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property FullRepaint;
property Font;
property Locked;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnCanResize;
property OnClick;
property OnConstrainedResize;
property OnContextPopup;
property OnDockDrop;
property OnDockOver;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGetSiteInfo;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property OnUnDock;
property Time:TTime Read FTime Write SetTime;
property Active:Boolean Read GetActive Write SetActive;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('hxw', [TTimePanel]);
end;
Constructor TTimePanel.Create(AOwner:TComponent);
begin

Inherited;
FMyTimer:=TTimer.Create(self);
FMyTimer.Interval:=1000;
FMyTimer.Ontimer:=OnTimer;
FMyTimer.Enabled:=True;
Caption:=TimeToStr(SysUtils.Time);
Ftime:=StrToTime(Caption);
end;
Destructor TTimePanel.Destroy;
begin
FMyTimer.Free;
Inherited;
end;
procedure TTimePanel.SetTime(Value:TTime);
begin
if FTime <> Value then
begin
Ftime:=Value;
Caption:=TimeToStr(FTime);
end;

end;
procedure TTimePanel.OnTimer(Sender:Tobject);
var
T:TDateTime;
begin
T:=FTime;
FTime:=IncSecond(T,1);
Caption:=TimeToStr(FTime);
end;
procedure TTimePanel.ResetTime ;
begin
Ftime:=StrToTime('00:00:00');
Caption:='00:00:00';
end;
function TTimePanel.GetActive;
begin
Result:=FMytimer.Enabled;
end;
Procedure TTimePanel.SetActive(Value:boolean);
begin
if FMyTimer.Enabled <>Value then
FMyTimer.Enabled :=Value;
end;
end.
 
Caption:=TimeToStr(SysUtils.Time);//这里为什么要用SysUtils.Time,而不用Now
//还有FormatDateTime(LongTimeFormat,Now);,用了以后,就可以保持与系统设定同步,
//相关函数还有:ShortTimeFormat,LongDateFormat,ShortDateFormat,
//其实,你还可以加上显示日期的,单显示时间,好象有点简单了。
//还可以来点排列方法,如时间在日期的下面等等

一点点小建议。
 
多人接受答案了。
 
后退
顶部