编写控件的高级技巧

 在DELPHI中编写控件,即如何定义一个新的属性类型,来实现自己的目的?例如有的控件在DELPHI的IDE中的属性窗口中加上作者的信息或者双击控件弹出关于窗口等。下面就以“关于”窗口来说明该技巧的实现方法。
   先定义一个新的控件“TTestComponent”。所有代码均在里面加入。
   1、让我们先定义一个新的属性类型“TTestAbout”。
type
  TTestAbout =  class(TPropertyEditor)
   public
     procedure Edit;  override;
     function GetAttributes: TPropertyAttributes;  override;
     function GetValue: stringoverride;
end;

{ TsongAbout }
    当用户双击或单击带有3个小圆点的按扭时,EDIT方法被调用。下面的TquickAboutBox为事先做好的窗体单元。
procedure TTestAbout.Edit;
var
  dd:TQuickAboutBox;
begin
   inherited;
   try
  dd:=TQuickAboutBox.Create(Application);
  dd.ShowModal;
   finally
  dd.free ;
   end;
end;
   GetAttributes方法返回一个TpropertyAttributes集,指明属性编辑器的一些特性,决定了属性是否有下拉列表、是否能多重选择等。我们指定为[paDialog],使对象检查器中出现(厎)按扭。
function TTestAbout.GetAttributes: TPropertyAttributes;
begin
   result:=[paDialog];
end;
   GetValue方法返回该属性出现在对象检查器中出现的字符串。
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 }
   当用户双击一个控件时,EDIT方法被调用,告诉控件做的事件。
procedure Testeditor.edit;
var
  dd:TQuickAboutBox;
begin
   inherited;
   try
  dd:=TQuickAboutBox.Create(Application);
  dd.ShowModal;
   finally
  dd.free ;
   end;
end;
   当用户选择窗体编辑器的弹出菜单中为控件定义的菜单项时,executeVerb方法 被调用,他传递一个从0开始的 Index参数。本方法作为TcomponentEditor的EDIT方法的缺省动作被调用
procedure Testeditor.executeVerb( index: integer);
begin
   inherited;
  EDIT;
end;
   当用户右键单击控件时Getverb方法被调用,他根据 Index参数返回需要被添加到弹出菜单上项目的字符值。
function Testeditor.getverb( index: integer):  string;
begin
   case  index  of
   0:result:='关于(&A)...';
  end;
end;
   每当用户右键单击控件时调用GetverbCount方法,它返回需要添加到窗体编辑器的弹出菜单里的菜单项数目,缺省值为0。
function Testeditor.getverbcount: integer;
begin
   result:=1;
end;

3、控件中如何调用此属性。
  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;

 注册控件编辑器函数  procedure RegisterComponentEditor(ComponentClass:TcomponentClass; ComponentEditor: TcomponentEditorClass);第一个参数是控件类名,第二个是控件编辑器类名。
注册控件属性编辑器函数 procedure RegisterPropertyEditor(PropertyType: PTypeInfo; ComponentClass: TClass;  const PropertyName:  string; EditorClass: TPropertyEditorClass); 第 一个参数是一个指针,可以把属性类型强制转换为TypeInfo得到;第二个是所适用的空间类型。如为NULL,则所适用于第一个参数所指的所有属性;第 三是所适用的属性名; 第四是给定属性的属性编辑器类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值