⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 delphi编写控件的高级技巧 (2001年2月15日).txt

📁 delphi 编程技巧
💻 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 + -