当前位置: 首页 > 图文教程 > 开发语言 > Delphi > 属性和控件编辑器

Delphi
Delphi实现窗体控件自由摆布
利用Delphi编制IP地址转换器
用Indy组件开发Socket应用程序
Delphi模拟最小化恢复关闭按纽
简析XML及其在Delphi中的应用
Delphi开发基于DCOM的聊天室
Delphi实现远程串口的数据采集
Delphi托盘编程实战演练
在Delphi中使用电子邮件
Delphi开发98屏幕保护预览程序
Delphi实现同类型文档自动合并
Delphi 8 For .NET 抢先预览
Delphi图像存取另类解决方案
用Delphi实现动态获取版本信息
用Delphi客户端访问EJB组件
Delphi中数据网格DBGrid应用
Delphi数据库控件使用入门
Delphi下的COM编程技术简介
用Delphi轻松实现背景播放
也谈TTreeView、TListView用法

Delphi 中的 属性和控件编辑器


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 181 ::
收藏到网摘: n/a

 
Delphi提供了开放的API,是程序员可以增强Delphi IDE的功能。共有4种开放工具的APIs:属性编辑器、控件编辑器、专家/导航和版本控制系统。本文讨论属性编辑器和控件编辑器,给出的例子说明如何写自己的Delphi属性、控件编辑器。

属性编辑器
属性编辑器是Delphi IDE的扩展。这听起来非常复杂和困难,但是实际上是很简单的。我们可以为枚举类型构造一个属性编辑器。记得TForm的颜色属性吗?当我们想改变它的值,看到了下拉框中列出了所有的可选值。那就是枚举类型的属性编辑器,我们也同样能做到,只需要几行代码,没什么特别的。注意到程序员并没有写一个属性编辑器,而是通知Delphi使用枚举类型的属性编辑器,为它的枚举特别定义的。

现有的属性编辑器

在我们搞清楚属性编辑器到底内部是什么之前,先看看Delphi中已有的。开始一个新工程,在implementation中加入"uses DsgnIntf;"编译,打开browser查找TPropertyEditor(只要输入TPrope):

Object Browser

如果没算错的话,在DSGNINTF中注册了至少21个客户属性编辑器(custom property editors),注意:事实上,还有更多的属性编辑器在其他单元中,例如C:\DELPHI\LIB\PICEDIT.DCU.中的TPictureEditor。

TPropertyEditor

对象察看器为所有的属性提供缺省的编辑。我们可以使用不同的方法重载这种行为,来使用特别的属性编辑器(21种预制的属性编辑器都扩充了对象察看器来处理其属性)。那么,究竟是怎样工作的呢?它是起源一个基类,我们必需重载已达到我们的目的。五个新的Delphi 2.0的方法-其中三个是变量相关的-在编译开关{$IFDEF WIN32}中一保证一下代码在所有的delphi版本中适用。

TypeTPropertyEditor = classprotectedfunction GetPropInfo: PPropInfo;function GetFloatValue: Extended;function GetFloatValueAt(Index: Integer): Extended;function GetMethodValue: TMethod;function GetMethodValueAt(Index: Integer): TMethod;function GetOrdValue: Longint;function GetOrdValueAt(Index: Integer): Longint;function GetStrValue: string;function GetStrValueAt(Index: Integer): string;{$IFDEF WIN32}function GetVarValue: variant;function GetVarValueAt(Index: Integer): variant;{$ENDIF}procedure Modified;procedure SetFloatValue(Value: Extended);procedure SetMethodValue(const Value: TMethod);procedure SetOrdValue(Value: Longint);procedure SetStrValue(const Value: string);{$IFDEF WIN32}procedure SetVarValue(const Value: variant);{$ENDIF}
publicdestructor Destroy; override;
procedure Activate; virtual;function AllEqual: Boolean; virtual;procedure Edit; virtual;function GetAttributes: TPropertyAttributes; virtual;function GetComponent(Index: Integer): TComponent;function GetEditLimit: Integer; virtual;function GetName: string; virtual;procedure GetProperties(Proc: TGetPropEditProc); virtual;function GetPropType: PTypeInfo;function GetValue: string; virtual;procedure GetValues(Proc: TGetStrProc); virtual;procedure Initialize; virtual;{$IFDEF WIN32}procedure Revert;{$ENDIF}procedure SetValue(const Value: string); virtual;{$IFDEF WIN32}procedure ValueAvailable: Boolean;{$ENDIF}
property Designer: TFormDesigner read FDesigner;property PrivateDirectory: string read GetPrivateDirectory;property PropCount: Integer read FPropCount;property Value: string read GetValue write SetValue;end;

TPropertyEditor编辑对象察看器中一个或是一串控件的一个属性。属性编辑器根据属性的类型而被创建,由RegisterPropertyEditor注册的类型决定。稍候有一个指示程序员如何使用这些工程的例子。所有的published属性都将出现在对象察看器中,当设计者进行读写属性的值时,其属性编辑器(为这种属性类型的)将被使用。