?? delphi編寫控件的高級技巧 (2001年2月15日).txt
字號:
DELPHI編寫控件的高級技巧 (2001年2月15日)
網友更新 分類:數據庫 作者: songshuang(推薦) 推薦:songshuang 閱讀次數:792
(http://www.codesky.net)
--------------------------------------------------------------------------------
在DELPHI中編寫控件,如何定義一個新的屬性類型,來實現自己的目的?例如有的控件在DELPHI的IDE中的屬性窗口中加上作者的信息或者雙擊控件彈出關于窗口等。下面就以“關于”窗口來說明該技巧的實現方法。
先定義一個新的控件“TTestComponent”。所有代碼均在里面加入。
1、讓我們先定義一個新的屬性類型“TTestAbout”。
type
TTestAbout = class(TPropertyEditor)
public
procedure Edit; override;
function GetAttributes: TPropertyAttributes; override;
function GetValue:string; override;
end;
{ TsongAbout }
procedure TTestAbout.Edit;
var
dd:TQuickAboutBox;
begin
inherited;
try
dd:=TQuickAboutBox.Create(Application);
dd.ShowModal;
finally
dd.free ;
end;
end;
implementation
function TTestAbout.GetAttributes: TPropertyAttributes;
begin
result:=[paDialog];
end;
function TTestAbout.GetValue: string;
begin
result:='About';
end;
2、下面是實現在DELPHI的IDE環境下鼠標右擊控件彈出的菜單加入新的命令。
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 }
procedure Testeditor.edit;
var
dd:TQuickAboutBox;
begin
inherited;
try
dd:=TQuickAboutBox.Create(Application);
dd.ShowModal;
finally
dd.free ;
end;
end;
procedure Testeditor.executeVerb(index: integer);
var
dd:TQuickAboutBox;
begin
inherited;
try
dd:=TQuickAboutBox.Create(Application);
dd.ShowModal;
finally
dd.free ;
end;
end;
function Testeditor.getverb(index: integer): string;
begin
case index of
0:result:='關于(&A)...';
end;
end;
function Testeditor.getverbcount: integer;
begin
result:=1;
end;
3、控件中如何調用此屬性。
type
TMyAbout=Class(TClassProperty)
end;
type
TTestComponent = class(TComponent)
private
Ftest: TMyAbout;
……
published
property sabout:TMyAbout read Ftest ;
……
end;
……
4、最重要的一步是要分別注冊它們。
procedure Register;
begin
RegisterComponents('MyAbout', [TTestComponent]);
RegisterPropertyEditor(TypeInfo( TMyAbout ), TTestComponent, '', TTestAbout );
RegisterComponentEditor(TTestComponent, TTestEditor);
end;
在控件編譯器中編譯安裝之后,就可以應用剛才做的控件的奇妙之處了。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -