人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

通過案例看VCL組件開發全過程(二)

通過案例看VCL組件開發全過程(二)

更新時間:2022-09-07 文章作者:未知 信息來源:網絡 閱讀次數:

(接上文)

組件的代碼由于假設你已經熟悉delphi開發(它和一般開發沒什么不同),我們就直接貼出來并加上適當的注釋:



unit Clock;



interface



uses

  SysUtils, Classes, Controls, StdCtrls,ExtCtrls;



type

  TState=(StClock,StRunClock,StBackClock);//定義枚舉類表示控件的3種狀態:時鐘、跑表、倒計時鐘



  TClock = class(TCustomLabel)

  private

    fState:TState;

    fTimer:TTimer;//為什么使用這個組件作為我們組件的私有成員就不用說了吧

    RCD:array[1..8] of integer;//跑表中的各個數位。

    fBeginTime:string;//到計時時的開始時鐘,之所以沒用TTime類型是為了在后面演示屬性編輯器

    fWakeTime:string;//鬧鐘時間,出于和上面同樣的理由

    fAllowWake:boolean;//是否開啟鬧鐘功能

    fOnWakeUp:TNotifyEvent;//為了使組件更加完美,我們允許組件用戶能夠響應鬧鐘到來時的時件

    fOnTimeUp:TNotifyEvent;//同上能夠響應倒計時種完成時的事件,我們將發布這兩個事件

    function GetActive:boolean;//控制Timer是否工作以控制3種狀態的鐘是否工作

    procedure SetActive(Value:boolean);

    procedure SetState(Value:TState);

    procedure SetBeginTime(Value:string);

    procedure SetWakeTime(Value:string);

  protected

    procedure WalkClock(sender:TObject);//作為時鐘時走種的事件

    procedure RunClock(sender:TObject); //跑表

    procedure BackClock(sender:TObject);//倒計時

  public

    constructor Create(AOwner:TComponent);override;//完成一些初始化工作

    procedure ReSetRunClock; //跑表和倒計時都需要一個復位方法給組件使用者調用

    procedure ReSetBackClock;

  published

    property State:TState read fState write SetState default StClock;//默認為時鐘狀態

    property Active:boolean read GetActive write SetActive;//控制3種狀態的鐘是否工作

    property BeginTime:string read fBeginTime write SetBeginTime;

    property WakeTime:string read fWakeTime write SetWakeTime;

    property AllowWake:boolean read fAllowWake write fAllowWake;

    property OnWakeUp:TNotifyEvent read fOnWakeUp write fOnWakeUp;

    property OnTimeUp:TNotifyEvent read fOnTimeUp write fOnTimeUp;

    //最后我們再發布一些被TCustomLabel所隱藏而我們又需要的屬性

    property Align;

    property Alignment;

    property Color;

    property Font;

    property ParentColor;

    property ParentFont;

    property ParentShowHint;

    property PopupMenu;

    property ShowHint;

    property Visible;

    property Transparent;

    property OnClick;

  end;



procedure Register;



implementation



procedure Register;

begin

  RegisterComponents('ClockAndTime', [TClock]);

end;



{ TClock }



constructor TClock.Create(AOwner: TComponent);

begin

  inherited Create(AOwner);

  //設置默認值

  fTimer:=TTimer.Create(self);

  //將它屬于我們的組件,這樣便不用編寫析構函數,而可以自動在釋放本組件時釋放Timer

  Active:=false;

  AllowWake:=false;

  State:=StClock;

  BeginTime:='00:00:00';

  WakeTime:='00:00:00';

end;



function TClock.GetActive: boolean;

begin

result:=fTimer.Enabled;

end;



procedure TClock.SetActive(Value: boolean);

begin

fTimer.Enabled:=Value;

end;



procedure TClock.SetState(Value: TState);

var

i:integer;

begin

case Value of

  StClock:

   begin

    Active:=false;

    fTimer.Interval:=1000;

    fTimer.OnTimer:=WalkClock;

    Active:=true;

   end;

  StRunClock://由于Time類型不好處理微秒操作,我們只有手工模仿這個操作,代碼會稍微煩瑣

   begin

    Active:=false;

    for i:=1 to 8 do RCD[i]:=0;

    Caption:=IntToStr(RCD[8])+IntToStr(RCD[7])+':'+IntToStr(RCD[6])+IntToStr(RCD[5])+':'+IntToStr(RCD[4]);

    Caption:=Caption+IntToStr(RCD[3])+':'+IntToStr(RCD[2])+IntToStr(RCD[1]);

    fTimer.Interval:=10;

    //經過測試,這個秒表的效果很好,然而這只是一個技術上的演示,

    //實際上這么頻繁(1/100秒)的不斷執行RunClock會使CPU的占用一直達到100%

    //這并不是一個好注意。事實上要想在跑表中顯示微秒級別并做到合理的占用CPU

    //這需要更加靈活和復雜的編程

    fTimer.OnTimer:=RunClock;

   end;

  StBackClock:

   begin

    Active:=false;

    Caption:=BeginTime;

    fTimer.Interval:=1000;

    fTimer.OnTimer:=BackClock;

   end;

end;

fState:=Value;

end;



procedure TClock.SetBeginTime(Value: string);

begin

  try

   StrToTime(Value);

   fBeginTime:=Value;

   if State=StBackClock then

   begin

    Active:=false;

    Caption:=Value;

   end;

  except

   on Exception do

   begin

    fBeginTime:='00:00:00';

    if State=StBackClock then Caption:='00:00:00';

   end;

  end;

end;



procedure TClock.SetWakeTime(Value: string);

begin

try

   StrToTime(Value);

   fWakeTime:=Value;

  except

   on Exception do

   begin

    fWakeTime:='00:00:00';

   end;

  end;

end;



procedure TClock.WalkClock(sender: TObject);

begin

Caption:=TimeToStr(Time);

if AllowWake and (StrToTime(Caption)=StrToTime(WakeTime)) then

begin

  Beep;//蜂鳴器

  if Assigned(fOnWakeUp) then

   fOnWakeUp(self);

end;

end;



procedure TClock.RunClock(sender: TObject);

begin

RCD[1]:=RCD[1]+1;

if RCD[1]=10 then begin RCD[2]:=RCD[2]+1;RCD[1]:=0; end;

if RCD[2]=10 then begin RCD[3]:=RCD[3]+1;RCD[2]:=0; end;

if RCD[3]=10 then begin RCD[4]:=RCD[4]+1;RCD[3]:=0; end;

if RCD[4]=6 then begin RCD[5]:=RCD[5]+1;RCD[4]:=0; end;

if RCD[5]=10 then begin RCD[6]:=RCD[6]+1;RCD[5]:=0; end;

if RCD[6]=6 then begin RCD[7]:=RCD[7]+1;RCD[6]:=0; end;

if RCD[7]=10 then begin RCD[8]:=RCD[8]+1;RCD[7]:=0; end;

if RCD[8]=10 then RCD[8]:=0; //我們的跑表最多可計99個小時;

Caption:=IntToStr(RCD[8])+IntToStr(RCD[7])+':'+IntToStr(RCD[6])+IntToStr(RCD[5])+':'+IntToStr(RCD[4]);

Caption:=Caption+IntToStr(RCD[3])+':'+IntToStr(RCD[2])+IntToStr(RCD[1]);

end;



procedure TClock.BackClock(sender: TObject);//可以在一天之類的時間倒計時

begin

if StrToTime(Caption)<>StrToTime('00:00:00') then

  Caption:=TimeToStr(StrToTime(Caption)-0.00001)

else

begin

  Active:=false;

  Beep;

  if Assigned(fOnTimeUp) then

   fOnTimeUp(self);

end;

end;



procedure TClock.ReSetBackClock;

var

i:integer;

begin

if State=StRunClock then

begin

  Active:=false;

  for i:=1 to 8 do RCD[i]:=0;

  Caption:='00:00:00:00';

end;

end;



procedure TClock.ReSetRunClock;

begin

if State=StBackClock then

begin

  Active:=false;

  Caption:=BeginTime;

end;

end;



end.



為了測試我們的組件,現在你就可以安裝這個組件包并建立一個應用測試它了,點擊組件包窗體中的install即可(注意:一但你安裝了組件包,當你想對組件修改時,在修改了原代碼以后只用點擊組件窗體的compile就可以了更新組件了),這時delphi的組件頁的最后多出了我們定義的頁,其中有了我們的組件!

然而這個組件到目前為止仍然不夠完善,還不能正式發布給用戶,在下一篇中我們將解決兩個重要的問題:1、給我們的組件添加一個默認的圖標。2、將這個組件雜亂的屬性歸類。 

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 金川县| 屏南县| 中西区| 望江县| 万州区| 乌鲁木齐市| 玛多县| 兴安盟| 永清县| 延庆县| 德钦县| 上思县| 疏附县| 益阳市| 黑河市| 皮山县| 乌拉特后旗| 吐鲁番市| 荥经县| 彰武县| 罗江县| 太原市| 宜城市| 隆德县| 公主岭市| 阳江市| 晋州市| 九江市| 黄石市| 肃北| 通城县| 政和县| 南昌县| 汤原县| 拜泉县| 河曲县| 河东区| 炉霍县| 阜新市| 马龙县| 阳信县|