打印本文 打印本文 关闭窗口 关闭窗口
Delphi组件与属性编辑器
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3309  更新时间:2009/4/23 18:38:00  文章录入:mintao  责任编辑:mintao

Delphi组件与属性编辑器

(一)前言
本文将用一个例子描述组件开发与属性编辑器。
例子(TdsWaitDialogEx)是一个可视组件,调用其show方法后显示一个Dialog,
其中包含一个TAnimate,两个提示信息(即TLabel),一个进度条(TGauge)。
  枚举属性:DialogStyle,AVIPosition
  记录属性:Options
  属性集合对象从TPersistent继承,本文例中AVISource属性集合包含TAnimate
的动画属性CommonAVI、FileName
  属性编辑器应用与AVISource的FileName属性,即String型FileName编辑时弹出一个
TOpenDialog,其过滤Filter为*.avi

(二)组件包dsDlgPack.dpk
为了便于发布、安装等,要用到要组件包.dpk。
  在Delphi6以后的版本中(我不知D5以前的版本怎样),有若干文件Delphi没有发布,如Proxies。
安装组件时若用到这些文件,可绕过这些文件而用包含这些文件的包。
  本例属性编辑器用到DesignEditors文件,而DesignEditors中需要Proxies文件,因此在发布此组件
的包(.dpk)中包含designide,解决了Proxies不存在的问题,这样装组件就会成功

    package dsDlgPack;

    ...

    requires
      rtl,
      vcl,
      VclSmp,
      designide;       

    contains
      dsDlgWaitEx in ''''dsDlgWaitEx.pas'''' {DlgWaitEx},
      dsDlgWaitExReg in ''''dsDlgWaitExReg.pas'''';

    end.

(三)组件注册文件dsDlgWaitExReg.pas
问:为什么要多用这样一个文件? 因为:
如果dsDlgWaitExReg.pas中的代码合并到dsDlgWaitEx.pas中,虽然dsDlgPack.dpk中包含designide
解决了安装组件时Proxies不存在的问题,但是在应用程序调用此组件时仍出Proxies不存在的问题,
因为DesignEditors中需要用到Proxies文件;因此象下面这段代码单独形成文件,应用程序调用此组
件是不需用到dsDlgWaitExReg.pas,可绕过Proxies不存在问题。

    unit dsDlgWaitExReg;

    interface

    uses Classes, Dialogs, Forms, dsDlgWaitEx, DesignIntf, DesignEditors ;

    type

      TdsAVIFileNameProperty = class(TStringProperty) //属性编辑器要用到DesignEditors文件
      public
        function GetAttributes:TPropertyAttributes;override; //方法覆盖
        procedure Edit;override;                             //方法覆盖
      end;

    procedure Register;

    implementation

    procedure Register;
    begin
      //注册此组件到 Delisoft 组件页面
      RegisterComponents(''''Delisoft'''', [TdsWaitDialogEx]);
      //注册此属性编辑器
      RegisterPropertyEditor(TypeInfo(string), TdsAVISource, ''''FileName'''', TdsAVIFileNameProperty);
    end;

    { TdsAVIFileNameProperty }
    function TdsAVIFileNameProperty.GetAttributes:TPropertyAttributes;
    begin
      result:=[paDialog];
    end;

    procedure TdsAVIFileNameProperty.Edit;
    begin
      with TOpenDialog.Create(application) do
      try
        Filter:=''''AVI Files(*.avi)|*.avi|All Files(*.*)|*.*'''';
        if Execute then SetStrValue(FileName);
      finally
        free;
      end;
    end;

    end.

(四)组件文件dsDlgWaitEx.pas
    unit dsDlgWaitEx;
{定义本组件所有属性、方法;其中窗体TDlgWaitEx的属性BorderStyle为bsDialog,本例组件TdsDlgWaitEx用到窗体TDlgWaitEx;属性对象AVISource用到TdsAVISource,它是直接从TPersistent继承下来,另外用到枚举属性(DialogStyle、AVIPosition)和记录属性(Options)等。
}

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Gauges, ComCtrls;

    type
      TDialogStyle = (dlgNormal, dlgStayOnTop);
      TAVIPosition = (aviLeft, aviTop, aviBottom);
      TDlgOptions =  set of (showAVI,showCaption,showMessage1,showMessage2,showProgress,ShowProgressText);

      TDlgWaitEx = class(TForm)
        Animate1: TAnimate;
        Gauge1: TGauge;
        Label1: TLabel;
        Label2: TLabel;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private                                                        
        FCloseAfter: DWORD;
        FUserFormClose: TCloseEvent;
      public
        property UserFormClose: TCloseEvent read FUserFormClose write FUserFormClose;
        property CloseAfter: DWORD read FCloseAfter write FCloseAfter;
      end;

      TdsAVISource = class(TPersistent)
      private
        FCommonAVI: TCommonAVI;
        FFileName: string;
        procedure SetCommonAVI(const Value: TCommonAVI);
        procedure SetFileName(const Value: string);
      protected
      public
      published
        property CommonAVI: TCommonAVI read FCommonAVI write SetCommonAVI default aviNone;
        property FileName: string read FfileName write SetFileName ;
      end;

      TdsWaitDialogEx=class(TComponent)
      private
        //Form
        FDlgForm:TDlgWaitEx;
        FMessage1: string;
        FMessage2: string;
        FMessage1Font: TFont;
        FMessage2Font: TFont;
        FCaption: string;
        FDislogStyle:TDialogStyle ;
        FwordWrap:boolean;
        FOptions:TDlgOptions;
        FShowMessage1,FShowMessage2:boolean;

        //AVI
        FaviPosition: TAVIPosition ;
        FAviActive:boolean;
        FshowAVI:boolean;
        FAVISource : TdsAVISource;

        //progress
        FProgressMax:integer;
        FProgressMin:integer;
        FProgressPos:integer;
        FProgressStep:integer;
        FShowProgress: Boolean;
        FShowProgressText: Boolean;

[1] [2] [3] [4] [5] [6]  下一页

打印本文 打印本文 关闭窗口 关闭窗口