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

Delphi
在线播放器DIY
关于VisiBroker For Delphi的使用(3)
关于VisiBroker For Delphi的使用(2)
关于VisiBroker For Delphi的使用(1)
Delphi的两个实用技巧(2)巧用Windows的API函数
Delphi的两个实用技巧(1)播放Flash
delphi学习:两种方法使用xml文档
Delphi与Word之间的融合技术
Delphi中动态链接库(DLL)的建立和使用
Delphi基础:Window 消息大全使用详解下
Delphi基础:Window 消息大全使用详解上
教你在DELPHI中如何调用系统对话框
Delphi开发单机瘦小数据库程序要点
用Delphi + DirectX开发简单RPG游戏
Delphi7从入门到精通之认识Delphi编辑器
Delphi7从入门到精通之历数Delphi七个版本
Delphi学习:图像放大漫游攻略
用编程来实现24小时制到12小时制的转换
一个实际的OLE服务器的开发
Delphi一点通:如何将源代码学好

Delphi 中的 属性和控件编辑器


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 189 ::
收藏到网摘: 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属性都将出现在对象察看器中,当设计者进行读写属性的值时,其属性编辑器(为这种属性类型的)将被使用。