線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1392
推到 Plurk!
推到 Facebook!

如何設計可自動顯示時間的 Label

答題得分者是:william
bruce0211
版主


發表:157
回覆:668
積分:279
註冊:2002-06-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-11-18 15:00:09 IP:211.21.xxx.xxx 未訂閱
我找到一些資料關於設計可自動顯示時間的 Label 元件 source 但都是動態建立一個 TTimer 做時間的顯示 而我卻想利用 WinAPI - SetTimer() 來做時間的顯示的驅動器 以下為我的元件程式碼 但在傳入參數 Handle 的部分一直出現錯誤 我猜是因為繼承 TLabel 沒有 Handle 的原因 但如此就無法使用SetTimer() 了嗎 ? 另外,繼承 TLabel 的元件,不需要 destructor TMyTimerLabel.Destroy; 這個解構函式嗎 ? 我看別人的元件範例都沒有,但此例我還是把它加入 ...    
 
unit MyTimerLabel;    interface    uses
  SysUtils, Classes, Controls, StdCtrls, Messages, Windows;    type
  TMyTimerLabel = class(TLabel)
  private
    { Private declarations }
  protected
    { Protected declarations }
    procedure WMTimer(var Message:TMessage); message WM_TIMER;
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;
    destructor  Destroy; override;      published
    { Published declarations }
  end;    procedure Register;    implementation    constructor TMyTimerLabel.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  SetTimer(Handle,1,1000,nil);
end;    destructor TMyTimerLabel.Destroy;
begin
  KillTimer(Handle,1);
  inherited Destroy;
end;    procedure TMyTimerLabel.WMTimer(var Message:TMessage);
var S:string;
begin
  s:=DateTimeToStr(Now);
  Caption:=s;
end;    procedure Register;
begin
  RegisterComponents('MyWay', [TMyTimerLabel]);
end;    end.
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-11-18 15:44:09 IP:147.8.xxx.xxx 未訂閱
 
unit MyTimerLabel;    interface    uses
  SysUtils, Classes, Controls, StdCtrls, Messages, Windows;    type
  TMyTimerLabel = class(TLabel)
  private
    { Private declarations }
    Handle: THandle;
  protected
    { Protected declarations }
    procedure WMTimer(var Message:TMessage); message WM_TIMER;
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;
    destructor  Destroy; override;      published
    { Published declarations }
  end;    procedure Register;    implementation    constructor TMyTimerLabel.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  Handle := AllocateHWnd(WMTimer);
  SetTimer(Handle,1,1000,nil);
end;    destructor TMyTimerLabel.Destroy;
begin
  KillTimer(Handle,1);
  DeallocateHWnd(Handle);
  inherited Destroy;
end;    procedure TMyTimerLabel.WMTimer(var Message:TMessage);
var S:string;
begin
  if Message.Msg=WM_TIMER then begin
    s:=DateTimeToStr(Now);
    Caption:=s;
  end;
  Message.Result := 0;
end;    procedure Register;
begin
  RegisterComponents('MyWay', [TMyTimerLabel]);
end;    end.
bruce0211
版主


發表:157
回覆:668
積分:279
註冊:2002-06-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-11-18 15:57:30 IP:211.21.xxx.xxx 未訂閱
william 兄果然是高手 測試 OK 程式中有些地方我還看不太懂 不過這篇我會好好研究
系統時間:2024-05-18 15:14:43
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!