FastReport中国社区FastReport联系电话 联系电话:023-68661681

FastReport VCL开发人员手册:如何为属性创建编辑器

来源:   发布时间:2021-07-20   浏览:次

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

立即点击下载FastReport VCL v6.9最新版

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

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

  TfrxPropertyEditor = class(TObject)
  受保护
    过程 GetStrProc(const s: String);
    函数 GetFloatValue:扩展;
    函数 GetOrdValue:整数;
    函数 GetStrValue:字符串;
    函数 GetVarValue:变体;
    过程 SetFloatValue(值:扩展);
    过程 SetOrdValue(值:整数);
    过程 SetStrValue(const Value: String);
    过程 SetVarValue(Value: Variant);
  上市
    构造函数创建(设计师:TfrxCustomDesigner);虚拟的;
    析构函数销毁;覆盖;
    功能编辑:布尔值;虚拟的;
    函数 GetAttributes:TfrxPropertyAttributes;虚拟的;
    函数 GetExtraLBSize:整数;虚拟的;
    函数 GetValue:字符串;虚拟的;
    过程 GetValues; 虚拟的;
    过程 SetValue(const Value: String); 虚拟的;
    过程 OnDrawLBItem(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState); 虚拟的;
    过程 OnDrawItem(Canvas: TCanvas; ARect: TRect); 虚拟的;
    属性组件:TPersistent 只读;
    属性 frComponent:TfrxComponent 只读;
    属性设计器:TfrxCustomDesigner 只读;
    属性项目高度:整数;
    属性 PropInfo:PPropInfo 只读;
    属性值:字符串;
    属性值:TStrings 只读;
  结尾;

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

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

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

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

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

    函数 GetFloatValue:扩展;
    函数 GetOrdValue:整数;
    函数 GetStrValue:字符串;
    函数 GetVarValue:变体;
    过程 SetFloatValue(值:扩展);
    过程 SetOrdValue(值:整数);
    过程 SetStrValue(const Value: String);
    过程 SetVarValue(Value: Variant);

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

MyFont := TFont(GetOrdValue)

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

TfrxPropertyAttribute = (paValueList, paSortList, paDialog, paMultiSelect, paSubProperties, paReadOnly, paOwnerDraw);
TfrxPropertyAttributes = 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​​文件中描述的程序进行的。

过程 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 属性编辑器显示编辑器按钮(...) }
{ 继承自 ClassProperty }

类型
 TfrxFontProperty = 类(TfrxClassProperty)
  上市
    功能编辑:布尔值;覆盖;
    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;
  结尾;
结尾;

{ 返回图片宽度 }
函数 TfrxPenStyleProperty.GetExtraLBSize:整数;
开始
  结果:= 36;
结尾;

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

如果您对 FastReport 动物,欢迎加入 FastReport QQ 交流群:702295239

还想要更多吗?您可以点击阅读【FastReport报表2021最新资源盘点】,查找需要的教程资源。上是FastReport .NET慧正在网火热销售中!>>查看价格详情

本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:FastReport控件中文网 [https://www.fastreportcn.com/]
本文地址:https://www.fastreportcn.com/post/3146.html

联系我们
  • 重庆总部 023-68661681
购买
  • sales@evget.com
合作
  • business@evget.com


在线
客服
微信
QQ 电话
023-68661681
返回
顶部