?? 《新》編寫控件的高級技巧修改稿 (2001年2月22.txt
字號:
《新》編寫控件的高級技巧<修改稿> (2001年2月22日)
網(wǎng)友更新 分類:控件制作 作者:宋爽 推薦:ss 閱讀次數(shù):729
(http://www.codesky.net)
--------------------------------------------------------------------------------
在DELPHI中編寫控件,即如何定義一個新的屬性類型,來實現(xiàn)自己的目的?例如有的控件在DELPHI的IDE中的屬性窗口中加上作者的信息或者雙擊控件彈出關(guān)于窗口等。下面就以“關(guān)于”窗口來說明該技巧的實現(xiàn)方法。
先定義一個新的控件“TTestComponent”。所有代碼均在里面加入。
1、讓我們先定義一個新的屬性類型“TTestAbout”。
type
TTestAbout = class(TPropertyEditor)
public
procedure Edit; override;
function GetAttributes: TPropertyAttributes; override;
function GetValue:string; override;
end;
{ TsongAbout }
當(dāng)用戶雙擊或單擊帶有3個小圓點的按扭時,EDIT方法被調(diào)用。下面的TquickAboutBox為事先做好的窗體單元。
procedure TTestAbout.Edit;
var
dd:TQuickAboutBox;
begin
inherited;
try
dd:=TQuickAboutBox.Create(Application);
dd.ShowModal;
finally
dd.free ;
end;
end;
GetAttributes方法返回一個TpropertyAttributes集,指明屬性編輯器的一些特性,決定了屬性是否有下拉列表、是否能多重選擇等。我們指定為[paDialog],使對象檢查器中出現(xiàn)(厎)按扭。
function TTestAbout.GetAttributes: TPropertyAttributes;
begin
result:=[paDialog];
end;
GetValue方法返回該屬性出現(xiàn)在對象檢查器中出現(xiàn)的字符串。
function TTestAbout.GetValue: string;
begin
result:='About';
end;
2、下面是實現(xiàn)在DELPHI的IDE環(huán)境下鼠標(biāo)右擊控件彈出的菜單加入新的命令。
type
TTesteditor = class(Tdefaulteditor)
public
function getverb(index:integer):string;override;
function getverbcount:integer;override;
procedure executeVerb(index:integer);override;
procedure edit;override;
end;
{ Tmyeditor }
當(dāng)用戶雙擊一個控件時,EDIT方法被調(diào)用,告訴控件做的事件。
procedure Testeditor.edit;
var
dd:TQuickAboutBox;
begin
inherited;
try
dd:=TQuickAboutBox.Create(Application);
dd.ShowModal;
finally
dd.free ;
end;
end;
當(dāng)用戶選擇窗體編輯器的彈出菜單中為控件定義的菜單項時,executeVerb方法 被調(diào)用,他傳遞一個從0開始的Index參數(shù)。本方法作為TcomponentEditor的EDIT方法的缺省動作被調(diào)用
procedure Testeditor.executeVerb(index: integer);
begin
inherited;
EDIT;
end;
當(dāng)用戶右鍵單擊控件時Getverb方法被調(diào)用,他根據(jù)Index參數(shù)返回需要被添加到彈出菜單上項目的字符值。
function Testeditor.getverb(index: integer): string;
begin
case index of
0:result:='關(guān)于(&A)...';
end;
end;
每當(dāng)用戶右鍵單擊控件時調(diào)用GetverbCount方法,它返回需要添加到窗體編輯器的彈出菜單里的菜單項數(shù)目,缺省值為0。
function Testeditor.getverbcount: integer;
begin
result:=1;
end;
3、控件中如何調(diào)用此屬性。
type
TMyAbout=Class(TClassProperty)
end;
type
TTestComponent = class(TComponent)
private
Ftest: TMyAbout;
卨och卲ar published
property sabout:TMyAbout read Ftest ;
卨och卲ar end;
卨och卲ar
4、最重要的一步是要分別注冊它們。
procedure Register;
begin
RegisterComponents('MyAbout', [TTestComponent]);
RegisterPropertyEditor(TypeInfo( TMyAbout ), TTestComponent, '', TTestAbout );
RegisterComponentEditor(TTestComponent, TTestEditor);
end;
注冊控件編輯器函數(shù) procedure RegisterComponentEditor(ComponentClass:TcomponentClass; ComponentEditor: TcomponentEditorClass);第一個參數(shù)是控件類名,第二個是控件編輯器類名。
注冊控件屬性編輯器函數(shù)procedure RegisterPropertyEditor(PropertyType: PTypeInfo; ComponentClass: TClass; const PropertyName: string; EditorClass: TPropertyEditorClass); 第一個參數(shù)是一個指針,可以把屬性類型強制轉(zhuǎn)換為TypeInfo得到;第二個是所適用的空間類型。如為NULL,則所適用于第一個參數(shù)所指的所有屬性;第三是所適用的屬性名; 第四是給定屬性的屬性編輯器類型。
在控件編譯器中編譯安裝之后,就可以應(yīng)用剛才做的控件了。《此程序在Windows98,Delphi 5.0下調(diào)試通過。》
江蘇常州托普軟件園研究院開發(fā)一室 宋爽 [213000] songshuang@topgroup.com.cn
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -