VCL (三) 属性编辑器[1]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 dreamnan 的 blog

delphi的属性是一个很有特色的功能,这一特性在c#里得到了很好的继承。
对于一个类来说,属性是对类中的数据进行有效修改和访问的特殊接口。在vcl中,被published
的属性还有一个特性,就是可以在界面设计过程中对属性进行设置.
属性的类型
数字、字符、布尔、枚举、tstrings、集合,类对象或者接口类型等
示例:
tenumtest=(etnone,ettestint,etteststring);
ttestcontrol=class(tcustomcontrol)
 private
   fint : integer;
   fenumtest :tenumtest;
   fcaption :string;

  function getint :integer;
  funtion getenumtest:tenumtest;
  function caption:string;

  procedure setcatpion(avalue:string);
  procedure setint(avalue:integer);
  procedure setenumtest(avalue:string);
published
   property intprop :integer read getint write setint;
   property enumtest:tenumtest read getenumtest write setenumtest;
   property caption:string read fcaption write setcaption;
end;
  
read 和 write 指令指定了访问属性的有效方法,在这些方法里可以实现对属性的访问封装。

写vcl控件最重要的就是属性编写,属性编辑器又是其中的难点。
属性编辑器一般有数字、字符串、集合、类、color、font类型编辑器,delphi内建基本编辑器声明有
tpropertyeditor 所有的属性编辑器都必须继承于此或他的派生类
tordinalproperty 这是几个有序属性编辑器的基类,如tintegerproperty tcharproperty tenumproperty
tintegerproperty 适用编辑整型属性
tcharproperty 适用编辑字符型属性
tenumproperty 适用编辑枚举型属性
tboolproperty 适用编辑布尔型属性
tfloatproperty 适用编辑浮点型属性
tstringproperty 适用编辑字符串型属性
tclassproperty 适用编辑对象类型
tcomponentproperty 使用编辑引用一个组件属性
tsetproperty 适用编辑集合类型
上述可以查看designeditors.pas单元(delphi7)
对于一些基本数据类型有时并不需要创建属性编辑器,因为数据类型本身也会数据进行检查,
在当你有特殊的检查时,你可以从上述基本编辑器派生出自己的属性编辑器

对话框型属性编辑器的编写
定义一个正对字符型属性编辑的对话框界面,在界面上放一个memo, 一个label, 三个按钮
代码如下
unit ustringeditor;

interface

uses
  windows, messages, sysutils, variants, classes, graphics, controls, forms,
  dialogs, stdctrls, buttons,designintf,designeditors;

type
  //对话框属窗体类
  tmystringeditdlg = class(tform)
    memo: tmemo;
    okbutton: tbitbtn;
    cancelbutton: tbitbtn;
    label1: tlabel;
    bitbtn1: tbitbtn;
    procedure memokeydown(sender: tobject; var key: word;
      shift: tshiftstate);
    procedure formdestroy(sender: tobject);
    procedure formshow(sender: tobject);
    procedure updatestatus(sender: tobject);
    procedure bitbtn1click(sender: tobject);
  private
    { private declarations }
    fmodified: boolean;

  protected
    function getlines: tstrings;
    procedure setlines(const value: tstrings);
    function getlinescontrol: twincontrol;
  public
    { public declarations }
    property lines: tstrings read getlines write setlines;
  end;
   
   //对话框属性编辑器
    tmystringproperty = class(tstringproperty)
    protected
      function editdialog: tmystringeditdlg; virtual;
    public
      procedure edit; override;
      function getattributes: tpropertyattributes; override;
    end;

var
  mystringeditdlg: tmystringeditdlg;

implementation
{$r *.dfm}
const                                   //v1.73 19/08/2000)
  copyrightstr: pchar = 'string editor v1.0 (12/06/2003)'+#13+#13+
    'created by donson wan '#13+#13+   
    'compiled in '+
    {$ifdef ver80}  'delphi 1.0' {$endif}
    {$ifdef ver90}  'delphi 2.0' {$endif}
    {$ifdef ver100} 'delphi 3.0' {$endif}
    {$ifdef ver120} 'delphi 4.0' {$endif}
    {$ifdef ver130} 'delphi 5.0' {$endif}
    {$ifdef ver140} 'delphi 6.0' {$endif}
    {$ifdef ver150} 'delphi 7.0' {$endif}
    {$ifdef ver93}  'c++builder 1.0' {$endif}
    {$ifdef ver110} 'c++builder 3.0' {$endif}
    {$ifdef ver125} 'c++builder 4.0' {$endif};

var
  storedwidth, storedheight, storedleft, storedtop: integer;


function tmystringeditdlg.getlines: tstrings;
begin
  result:=memo.lines;
end;

function tmystringeditdlg.getlinescontrol: twincontrol;
begin

end;

本文关键:VCL (三) 属性编辑器
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top