翻譯|使用教程|編輯:楊鵬連|2021-07-08 10:26:25.600|閱讀 239 次
概述:本文介紹了FastReport VCL如何為屬性創建編輯器。當你在設計器中選擇一個組件時,它的屬性會顯示在對象檢查器中。你可以為任何屬性創建你自己的編輯器。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
FastReport VCL是用于 Delphi、C++ Builder、RAD Studio 和 Lazarus 的報告和文檔創建 VCL 庫。它提供了可視化模板設計器,可以訪問 30 多種格式,并可以部署到云、網站、電子郵件和打印中。
當你在設計器中選擇一個組件時,它的屬性會顯示在對象檢查器中。你可以為任何屬性創建你自己的編輯器。"字體 "屬性的標準編輯器可以作為例子:如果這個屬性被選中,在行的右側出現...按鈕;通過點擊這個按鈕調用標準的 "字體屬性 "對話框。還有一個例子是 "顏色 "屬性編輯器。它在下拉列表中顯示標準顏色和顏色規格名稱。
所有屬性編輯器的基類在 "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)
在這個類中定義了幾個屬性。
下面的方法是服務性的。它們可以用來獲取或設置已編輯的屬性值。
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;
屬性分配是按以下方式實現的:
編輯方法在兩種情況下被調用:要么選擇屬性,要么通過雙擊它的值,或者(如果屬性有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);
只需要指定 "屬性類型 "參數。"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
還想要更多嗎?您可以點擊閱讀【FastReport報表2021最新資源盤點】,查找需要的教程資源。上是FastReport .NET慧正在網火熱銷售中!>>查看價格詳情
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: