翻譯|使用教程|編輯:楊鵬連|2021-07-08 10:26:25.600|閱讀 239 次
概述:本文介紹了FastReport VCL如何為屬性創(chuàng)建編輯器。當(dāng)你在設(shè)計(jì)器中選擇一個(gè)組件時(shí),它的屬性會(huì)顯示在對(duì)象檢查器中。你可以為任何屬性創(chuàng)建你自己的編輯器。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
FastReport VCL是用于 Delphi、C++ Builder、RAD Studio 和 Lazarus 的報(bào)告和文檔創(chuàng)建 VCL 庫(kù)。它提供了可視化模板設(shè)計(jì)器,可以訪問(wèn) 30 多種格式,并可以部署到云、網(wǎng)站、電子郵件和打印中。
立即點(diǎn)擊下載FastReport VCL v6.9最新版
當(dāng)你在設(shè)計(jì)器中選擇一個(gè)組件時(shí),它的屬性會(huì)顯示在對(duì)象檢查器中。你可以為任何屬性創(chuàng)建你自己的編輯器。"字體 "屬性的標(biāo)準(zhǔn)編輯器可以作為例子:如果這個(gè)屬性被選中,在行的右側(cè)出現(xiàn)...按鈕;通過(guò)點(diǎn)擊這個(gè)按鈕調(diào)用標(biāo)準(zhǔn)的 "字體屬性 "對(duì)話框。還有一個(gè)例子是 "顏色 "屬性編輯器。它在下拉列表中顯示標(biāo)準(zhǔn)顏色和顏色規(guī)格名稱。
所有屬性編輯器的基類在 "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;
你也可以繼承以下任何一個(gè)類,這些類本身實(shí)現(xiàn)了一些處理相應(yīng)類型的屬性的基本功能。
TfrxIntegerProperty = class(TfrxPropertyEditor)
TfrxFloatProperty = class(TfrxPropertyEditor)
TfrxCharProperty = class(TfrxPropertyEditor)
TfrxStringProperty = class(TfrxPropertyEditor)
TfrxEnumProperty = class(TfrxPropertyEditor)
TfrxClassProperty = class(TfrxPropertyEditor)
TfrxComponentProperty = class(TfrxPropertyEditor)
在這個(gè)類中定義了幾個(gè)屬性。
下面的方法是服務(wù)性的。它們可以用來(lái)獲取或設(shè)置已編輯的屬性值。
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);
你應(yīng)該使用與屬性類型相對(duì)應(yīng)的方法。因此,如果屬性是 "Integer "類型的,就使用GetOrdValue和SetOrdValue方法。這些方法也用于處理TObject類型的屬性,因?yàn)檫@種屬性包含32位對(duì)象地址。在這種情況下,只要做以下類型的轉(zhuǎn)換就可以了,比如說(shuō)。
MyFont := TFont(GetOrdValue)
為了創(chuàng)建你自己的編輯器,有必要繼承基本類,并重寫定義在公共部分的一個(gè)或幾個(gè)方法(這取決于你想實(shí)現(xiàn)的屬性類型和功能)。你肯定要覆蓋的方法之一是GetAttributes方法。這個(gè)方法是用來(lái)返回屬性的集合。屬性以下列方式定義。
TfrxPropertyAttribute = (paValueList, paSortList, paDialog, paMultiSelect, paSubProperties, paReadOnly, paOwnerDraw);
TfrxPropertyAttributes = set of TfrxPropertyAttribute;
屬性分配是按以下方式實(shí)現(xiàn)的:
編輯方法在兩種情況下被調(diào)用:要么選擇屬性,要么通過(guò)雙擊它的值,或者(如果屬性有paDialog屬性)通過(guò)點(diǎn)擊...按鈕。如果屬性值被修改,這個(gè)方法應(yīng)該返回 "真"。
GetValue方法應(yīng)該以字符串形式返回屬性值(它將顯示在對(duì)象檢查器中)。如果你繼承了TfrxPropertyEditor基本類,有必要覆蓋這個(gè)方法。
SetValue方法是用來(lái)設(shè)置屬性值轉(zhuǎn)移為字符串。如果你繼承自TfrxPropertyEditor基本類,就有必要覆蓋這個(gè)方法。
如果你定義了 "paValueList "屬性,GetValues方法應(yīng)該被覆蓋。這個(gè)方法應(yīng)該用值來(lái)填充數(shù)值屬性。
下面三個(gè)方法允許執(zhí)行手動(dòng)屬性值繪制(顏色屬性編輯器以同樣的方式工作)。如果你定義了 "paOwnerDraw "屬性,這些方法將被調(diào)用。
OnDrawItem方法在對(duì)象檢查器中繪制屬性值時(shí)被調(diào)用(當(dāng)屬性未被選中時(shí);否則它的值將簡(jiǎn)單地顯示在編輯行中)。例如,顏色屬性編輯器會(huì)在屬性值的左邊繪制矩形,并根據(jù)顏色填充。
如果你定義了 "paValueList "屬性,GetExtraLBSize方法將被調(diào)用。該方法返回像素?cái)?shù),"下拉列表 "的寬度應(yīng)該被調(diào)整,以便為顯示的圖片找到空間。默認(rèn)情況下,該方法返回對(duì)應(yīng)于屬性包絡(luò)的單元格高度的值。如果你需要推導(dǎo)出寬度大于高度的圖片,應(yīng)該重寫給定的方法。
OnDrawLBItem方法在下拉列表中繪制字符串時(shí)被調(diào)用,如果你定義了paValueList屬性。事實(shí)上,這個(gè)方法是TListBox.OnDrawItem事件處理程序,并且有相同的參數(shù)集。
屬性編輯器的注冊(cè)是通過(guò)frxDsgnIntf文件中描述的程序進(jìn)行的。
procedure frxPropertyEditors.Register(PropertyType: PTypeInfo; ComponentClass: TClass; const PropertyName: String; EditorClass: TfrxPropertyEditorClass);
只需要指定 "屬性類型 "參數(shù)。"ComponentClass "和/或 "PropertyName "參數(shù)可以是空白。這允許將編輯器注冊(cè)到任何PropertyType類型的屬性、任何具體的ComponentClass組件及其繼承者的屬性,或具體組件的PropertyName具體屬性(或任何組件,如果ComponentClass參數(shù)為空)。
讓我們來(lái)看看三個(gè)屬性編輯器的例子。根據(jù)FastReport的要求,編輯器的代碼可以放在一個(gè)文件中,該文件的名稱與包含組件代碼的文件相同,并添加編輯器的后綴。
{ 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);
如果您對(duì) FastReport 感興趣,歡迎加入 FastReport QQ 交流群:702295239
還想要更多嗎?您可以點(diǎn)擊閱讀【FastReport報(bào)表2021最新資源盤點(diǎn)】,查找需要的教程資源。上是FastReport .NET慧正在網(wǎng)火熱銷售中!>>查看價(jià)格詳情
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: