FastReport 报表工具如何为属性创建编辑器

FastReport VCL是用于 Delphi、C++ Builder、RAD Studio 和 Lazarus 的报告和文档创建 VCL 库。它提供了可视化模板设计器,可以访问 30 多种格式,并可以部署到云、网站、电子邮件和打印中。

【慧都网】下载FastReport VCL v6.9最新版

当你在设计器中选择一个组件时,它的属性会显示在对象检查器中。你可以为任何属性创建你自己的编辑器。"字体 "属性的标准编辑器可以作为例子:如果这个属性被选中,在行的右侧出现...按钮;通过点击这个按钮调用标准的 "字体属性 "对话框。还有一个例子是 "颜色 "属性编辑器。它在下拉列表中显示标准颜色和颜色规格名称。

所有属性编辑器的基类在 "frxDsgnIntf "单元中描述。

  TfrxPropertyEditor = class(TObject)
  protected
    procedure GetStrProc(const s: String);
    function GetFloatValue: Extended;
    function GetOrdValue: Integer;
    function GetStrValue: String;
    function GetVarValue: Variant;
    procedure SetFloatValue(Value: Extended);
    procedure SetOrdValue(Value: Integer);
    procedure SetStrValue(const Value: String);
    procedure SetVarValue(Value: Variant);
  public
    constructor Create(Designer: TfrxCustomDesigner); virtual;
    destructor Destroy; override;
    function Edit: Boolean; virtual;
    function GetAttributes: TfrxPropertyAttributes; virtual;
    function GetExtraLBSize: Integer; virtual;
    function GetValue: String; virtual;
    procedure GetValues; virtual;
    procedure SetValue(const Value: String); virtual;
    procedure OnDrawLBItem(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState); virtual;
    procedure OnDrawItem(Canvas: TCanvas; ARect: TRect); virtual;
    property Component: TPersistent readonly;
    property frComponent: TfrxComponent readonly;
    property Designer: TfrxCustomDesigner readonly;
    property ItemHeight: Integer;
    property PropInfo: PPropInfo readonly;
    property Value: String;
    property Values: TStrings readonly;
  end;

你也可以继承以下任何一个类,这些类本身实现了一些处理相应类型的属性的基本功能。

  TfrxIntegerProperty = class(TfrxPropertyEditor)
  TfrxFloatProperty = class(TfrxPropertyEditor)
  TfrxCharProperty = class(TfrxPropertyEditor)
  TfrxStringProperty = class(TfrxPropertyEditor)
  TfrxEnumProperty = class(TfrxPropertyEditor)
  TfrxClassProperty = class(TfrxPropertyEditor)
  TfrxComponentProperty = class(TfrxPropertyEditor)

在这个类中定义了几个属性。

  • Component - 链接到父级组件(不是属性本身!),给定的属性属于该组件。
  • frComponent - 相同的,但被投到TfrxComponent类型(为了在某些情况下方便)。
  • Designer - 通往报告设计器的链接。
  • ItemHeight - 项目高度,属性在其中显示。它在OnDrawXXX中很有用。
  • PropInfo - 链接到PPropInfo结构,它包含了关于已编辑属性的信息。
  • Value - 显示为字符串的属性值。
  • Values - 值的列表。如果定义了 "paValueList "属性(见下文),这个属性将被填入GetValue方法中。

下面的方法是服务性的。它们可以用来获取或设置已编辑的属性值。

    function GetFloatValue: Extended;
    function GetOrdValue: Integer;
    function GetStrValue: String;
    function GetVarValue: Variant;
    procedure SetFloatValue(Value: Extended);
    procedure SetOrdValue(Value: Integer);
    procedure SetStrValue(const Value: String);
    procedure SetVarValue(Value: Variant);

你应该使用与属性类型相对应的方法。因此,如果属性是 "Integer "类型的,就使用GetOrdValue和SetOrdValue方法。这些方法也用于处理TObject类型的属性,因为这种属性包含32位对象地址。在这种情况下,只要做以下类型的转换就可以了,比如说。

MyFont := TFont(GetOrdValue)

为了创建你自己的编辑器,有必要继承基本类,并重写定义在公共部分的一个或几个方法(这取决于你想实现的属性类型和功能)。你肯定要覆盖的方法之一是GetAttributes方法。这个方法是用来返回属性的集合。属性以下列方式定义。

TfrxPropertyAttribute = (paValueList, paSortList, paDialog, paMultiSelect, paSubProperties, paReadOnly, paOwnerDraw);
TfrxPropertyAttributes = set of TfrxPropertyAttribute;

属性分配是按以下方式实现的:

  • paValueList - 属性代表下拉式的数值列表。这个功能以 "颜色 "属性为例)。如果这个属性存在,GetValues方法应该被重写。
  • paSortList - 对列表元素进行排序。它与paValueList一起使用。
  • paDialog - 属性有编辑器。如果这个属性存在,...按钮将显示在编辑行的右边部分。通过点击它,编辑方法被调用。
  • paMultiSelect - 允许在同时选择的一些对象中编辑给定的属性。一些属性(如 "名称 "等)没有这个属性。
  • paSubProperties - 属性是TPersistent类型的对象,并且有它自己的属性,这些属性也应该被显示。这个功能在 "字体 "属性中得到了体现)。
  • paReadOnly - 不可能在编辑行中修改值。一些属性,作为 "类 "或 "设置 "类型,拥有这个属性。
  • paOwnerDraw - 属性值的绘制是通过OnDrawItem方法进行的。如果 "paValueList "属性被定义,那么下拉列表的绘制是通过OnDrawLBItem方法进行的。

编辑方法在两种情况下被调用:要么选择属性,要么通过双击它的值,或者(如果属性有paDialog属性)通过点击...按钮。如果属性值被修改,这个方法应该返回 "真"。

 

GetValue方法应该以字符串形式返回属性值(它将显示在对象检查器中)。如果你继承了TfrxPropertyEditor基本类,有必要覆盖这个方法。

SetValue方法是用来设置属性值转移为字符串。如果你继承自TfrxPropertyEditor基本类,就有必要覆盖这个方法。

如果你定义了 "paValueList "属性,GetValues方法应该被覆盖。这个方法应该用值来填充数值属性。
下面三个方法允许执行手动属性值绘制(颜色属性编辑器以同样的方式工作)。如果你定义了 "paOwnerDraw "属性,这些方法将被调用。
OnDrawItem方法在对象检查器中绘制属性值时被调用(当属性未被选中时;否则它的值将简单地显示在编辑行中)。例如,颜色属性编辑器会在属性值的左边绘制矩形,并根据颜色填充。
如果你定义了 "paValueList "属性,GetExtraLBSize方法将被调用。该方法返回像素数,"下拉列表 "的宽度应该被调整,以便为显示的图片找到空间。默认情况下,该方法返回对应于属性包络的单元格高度的值。如果你需要推导出宽度大于高度的图片,应该重写给定的方法。
OnDrawLBItem方法在下拉列表中绘制字符串时被调用,如果你定义了paValueList属性。事实上,这个方法是TListBox.OnDrawItem事件处理程序,并且有相同的参数集。
属性编辑器的注册是通过frxDsgnIntf文件中描述的程序进行的。

 

procedure frxPropertyEditors.Register(PropertyType: PTypeInfo; ComponentClass: TClass; const PropertyName: String; EditorClass: TfrxPropertyEditorClass);
  • PropertyType - 关于属性类型的信息,通过 "TypeInfo "系统函数传输,例如TypeInfo(String)。
  • ComponentClass - 组件名称,有你要编辑的属性(可以是nil)。
  • PropertyName - 你要编辑的属性的名称(可以是空白字符串)。
  • EditorClass - 属性编辑器名称

只需要指定 "属性类型 "参数。"ComponentClass "和/或 "PropertyName "参数可以是空白。这允许将编辑器注册到任何PropertyType类型的属性、任何具体的ComponentClass组件及其继承者的属性,或具体组件的PropertyName具体属性(或任何组件,如果ComponentClass参数为空)。
让我们来看看三个属性编辑器的例子。根据FastReport的要求,编辑器的代码可以放在一个文件中,该文件的名称与包含组件代码的文件相同,并添加编辑器的后缀。

{ TFont property editor displays editor button(...) }
{ inherit from ClassProperty }

type
 TfrxFontProperty = class(TfrxClassProperty)
  public
    function Edit: Boolean; override;
    function GetAttributes: TfrxPropertyAttributes; override;
  end;

function TfrxFontProperty.GetAttributes: TfrxPropertyAttributes;
begin
  {  property has nested properties and  editor. It cannot be edited manually }
  Result := [paMultiSelect, paDialog, paSubProperties, paReadOnly];
end;

function TfrxFontProperty.Edit: Boolean;
var
  FontDialog: TFontDialog;
begin
  { create standard dialogue }
  FontDialog := TFontDialog.Create(Application);
  try
    { take  property value }
    FontDialog.Font := TFont(GetOrdValue);
    FontDialog.Options := FontDialog.Options + [fdForceFontExist];
    { display dialogue }
    Result := FontDialog.Execute;
    { bind new value }
    if Result then
      SetOrdValue(Integer(FontDialog.Font));
  finally
    FontDialog.Free;
  end;
end;

{ registration }

frxPropertyEditors.Register(TypeInfo(TFont), nil, '', TfrxFontProperty);
{ TFont.Name property editor displays  drop-down list of available fonts }
{ inherit from StringProperty, as property is of string type }

type
  TfrxFontNameProperty = class(TfrxStringProperty)
  public
    function GetAttributes: TfrxPropertyAttributes; override;
    procedure GetValues; override;
  end;

function TfrxFontNameProperty.GetAttributes: TfrxPropertyAttributes;
begin
  Result := [paMultiSelect, paValueList];
end;

procedure TfrxFontNameProperty.GetValues;
begin
  Values.Assign(Screen.Fonts);
end;

{ registration }

frxPropertyEditors.Register(TypeInfo(String), TFont, 'Name', TfrxFontNameProperty);
{ TPen.Style property editor displays picture, which is pattern of selected style }

type
  TfrxPenStyleProperty = class(TfrxEnumProperty)
  public
    function GetAttributes: TfrxPropertyAttributes; override;
    function GetExtraLBSize: Integer; override;
    procedure OnDrawLBItem(Control: TWinControl; Index: Integer;
      ARect: TRect; State: TOwnerDrawState); override;
    procedure OnDrawItem(Canvas: TCanvas; ARect: TRect); override;
  end;

function TfrxPenStyleProperty.GetAttributes: TfrxPropertyAttributes;
begin
  Result := [paMultiSelect, paValueList, paOwnerDraw];
end;

{ method draws thick horizontal line with selected style }
procedure HLine(Canvas: TCanvas; X, Y, DX: Integer);
var
  i: Integer;
begin
  with Canvas do
  begin
    Pen.Color := clBlack;
    for i := 0 to 1 do
    begin
      MoveTo(X, Y - 1 + i);
      LineTo(X + DX, Y - 1 + i);
    end;
  end;
end;

{ drawing drop-down list }
procedure TfrxPenStyleProperty.OnDrawLBItem(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);
begin
  with TListBox(Control), TListBox(Control).Canvas do
  begin
    FillRect(ARect);
    TextOut(ARect.Left + 40, ARect.Top + 1, TListBox(Control).Items[Index]);
    Pen.Color := clGray;
    Brush.Color := clWhite;
    Rectangle(ARect.Left + 2, ARect.Top + 2, ARect.Left + 36, ARect.Bottom - 2);
    Pen.Style := TPenStyle(Index);
    HLine(TListBox(Control).Canvas, ARect.Left + 3, ARect.Top + (ARect.Bottom - ARect.Top) div 2, 32);
    Pen.Style := psSolid;
  end;
end;

{ drawing property value }
procedure TfrxPenStyleProperty.OnDrawItem(Canvas: TCanvas; ARect: TRect);
begin
  with Canvas do
  begin
    TextOut(ARect.Left + 38, ARect.Top, Value);
    Pen.Color := clGray;
    Brush.Color := clWhite;
    Rectangle(ARect.Left, ARect.Top + 1, ARect.Left + 34, ARect.Bottom - 4);
    Pen.Color := clBlack;
    Pen.Style := TPenStyle(GetOrdValue);
    HLine(Canvas, ARect.Left + 1, ARect.Top + (ARect.Bottom - ARect.Top) div 2 - 1, 32);
    Pen.Style := psSolid;
  end;
end;

{ return  picture width }
function TfrxPenStyleProperty.GetExtraLBSize: Integer;
begin
  Result := 36;
end;

{ registration }
frxPropertyEditors.Register(TypeInfo(TPenStyle), TPen, 'Style', TfrxPenStyleProperty);

 

如果您对 FastReport 感兴趣,欢迎加入 FastReport QQ 交流群:702295239

 

posted @ 2021-07-21 15:14  roffey  阅读(299)  评论(0编辑  收藏  举报