相關(guān)鏈接: 中國安全網(wǎng) 中國質(zhì)量網(wǎng) 中國論文網(wǎng) 中國資訊網(wǎng)
可視化編程工具Dephi以強(qiáng)大的功能,眾多的優(yōu)越特性,深受廣大使用者的喜愛,但如何編寫自己的Dephi構(gòu)件對(duì)許多使用者來說比較陌生,因此,有必要掌握構(gòu)件的創(chuàng)建方法。
一. 構(gòu)件創(chuàng)建的步驟:
1. 使用New Component 對(duì)話框開始創(chuàng)建
選擇File|New彈出Object Reposity,雙擊Object Reposity的Component圖標(biāo),顯示New Component對(duì)話框開始創(chuàng)建。
2. 為構(gòu)件增加屬性
Dephi中用關(guān)鍵字Property聲明屬性字段,屬性有read和 write可選從句,其語法格式為:
Property MyProperty :Atype
Read GetProperty write SetProperty;
GetProperty SetProperty分別為讀寫屬性過程。
3. 為構(gòu)件增加方法
在Dephi構(gòu)件創(chuàng)建中增加新的方法,需在 interface區(qū)的 private 、 protected 以 及public中的任一部分描述函數(shù)原型,若方法可能在以后被重載,則應(yīng)在尾部加上 virtual 或dynamic關(guān)鍵字,然后在 implementation區(qū)定義方法的具體實(shí)現(xiàn)代碼。
4. 為構(gòu)件增加事件
在Dephi中,事件也是使用關(guān)鍵字Property來聲明。其語法格式為:
Property OnNewEvent :Aevent Type
read FonNewEvent write FonNewEvent;
聲明事件后往往要將windows的消息傳遞與事件聯(lián)系起來才能真正實(shí)現(xiàn)事件驅(qū)動(dòng),因此,我們還要增加一種新的特殊類型的動(dòng)態(tài)的“消息管理”方法,其語法格式為:
Procedure MsgPro(var Msg :Tmessage) ;message AmsgIndex;
其中,關(guān)鍵字Message 聲明此為一個(gè)消息管理方法。 AmsgIndex 為一整數(shù)常數(shù),作 windows 消息動(dòng)態(tài)索引,當(dāng)索引為 AmsgIndex 的消息傳遞至應(yīng)用程序時(shí)便會(huì)執(zhí)行 MsgPro過程。
5. 測(cè)試構(gòu)件
測(cè)試構(gòu)件,確信構(gòu)件是按設(shè)計(jì)編譯和起作用的。這是構(gòu)件開發(fā)中非常關(guān)鍵的一步,為測(cè)試構(gòu)件,要先寫一個(gè)用作測(cè)試的應(yīng)用程序,由于不能拉出構(gòu)件選項(xiàng)板窗口上的構(gòu)件,還必須人工創(chuàng)建一個(gè)構(gòu)件。
6. 將新構(gòu)件添加到構(gòu)件選項(xiàng)板
當(dāng)構(gòu)件正常工作并令人感到滿意后,可添加到構(gòu)件選項(xiàng)板上,從主菜單選擇Component | Install Component顯示一個(gè)Install Component 對(duì)話框,該對(duì)話框能夠添加構(gòu)件到程序包。
二. 構(gòu)件創(chuàng)建實(shí)例
下面要開發(fā)的新構(gòu)TnewLabel 是從TLabel 類繼承來的,它與 TLabel 具有相似的焦點(diǎn)感應(yīng)能力:當(dāng)鼠標(biāo)移到Tlabel 上時(shí),文字顏色發(fā)生改變;當(dāng)鼠標(biāo)離開時(shí),顏色恢復(fù)。它還將增加一個(gè)EnterTextColor屬性以及 OnMouseLeave和OnMouseLeave事件。屬性 EnterTextColor中可以輸入鼠標(biāo)進(jìn)入構(gòu)件時(shí)文字的顏色, OnMouseEnter、 OnMouseLeave事件中則可輸入程序員自己的代碼來指定鼠標(biāo)進(jìn)入和離開構(gòu)件時(shí)的動(dòng)作。
1. 選中Component菜單項(xiàng)中的 New Component選項(xiàng),在 Ancestor Type 框填入Tlabel,在 class Name框中填入 TnewLabel ,在 Palette Page框中填入 Samples,在 Unit File Name 框中填入 NewCom.pas ,然后單擊按鈕 Create Unit就進(jìn)入到代碼編輯狀態(tài)。
2. 使用 Tools菜單中Image Editor來創(chuàng)建編輯文件NewCom.dcr,為 TnewLabel類建立位圖。
3. 增加新屬性EnterTextColor ,并為讀寫屬性增加私有方法 SetEnterTextColor和私有變
量 FenterTextColor。
加入如下的代碼:
Private
FenterTextColor : Tcolor;
SetEnterTextColor(Acolor :Tcolor);
Published
Property EnterTextColor : Tcolor
Read FenterTextColor Write SetEnterTextColor(Acolor);
…
Implementation
Procedure TnewLabel.SetEnterTextColor(Acolor :Tcolor);
Begin
FEnterTextColor:=Acolor;
End;
4. 增加保護(hù)方法MouseEnter、MouseLeave、CM_MouseEnter和 CM_MouseLeave
加入如下的代碼:
protected
Procedure MouseEnter; virtual; Procedure MouseLeave; virtual;
Procedure CM_MouseEnter(var msg : Tmessage);
Message CM_MouseEnter;
Procedure CM_MouseLeave(var msg : Tmessage);
Message CM_MouseLeave;
…
ImPlementation
Procedure MouseEnter;
Begin
If assigned(FonMouseEnter) then
FonMouseEnter(self);
End;
Procedure CM_MouseEnter(var msg :Tmessage);
Begin
Inherited;
MouseEnter;
End;
5. 增加事件OnMouseEnter 和 OnMouseLeave
加入如下的代碼:
Private
FonMouseEnter,FonMouseLeave :TnotifyEvent;
Published
OnMouseEnter : TnotifyEvent
Read FonMouseEnter write FonMouseEnter;
OnMouseLeave : TnotifyEvent
Read FonMouseLeave Write FonMouseLeave;
6. MouseLeave CM_MouseLeave的實(shí)現(xiàn)代碼與MouseEnter CM_MouseEnter的類似 。Dephi 為所有消息提供缺省處理。在寫消息句柄時(shí)應(yīng) 禁止缺省處理,因此在CM_MouseEnter和 CM_MouseLeave過程中調(diào)用了 inherited。
重載構(gòu)造和析構(gòu)方法的代碼如下:
Public Constructor Create(Aowner :Tcomponent);override;
Destructor Destroy;override;
……
Implementation
Constructor Create(Aowner :Tcomponent);
Begin
Inherited Create(Aowner);
FleaveTextColor:=Font.Color;
FenterTextColor:=Font.Color;
End;
Destructor TnewLabel.Destroy;
Begin
Inherited Destroy;
End;
三. 結(jié)束語
以上僅對(duì)創(chuàng)建Dephi構(gòu)件的基本知識(shí)進(jìn)行了討論,仍有一些創(chuàng)建構(gòu)件問題、技術(shù)未能深入探討,對(duì)這方面問題感興趣的Dephi使用者可進(jìn)一步通過閱讀有關(guān)資料提高自身創(chuàng)建構(gòu)件的能力。