原創|其它|編輯:郝浩|2012-11-30 11:40:20.000|閱讀 483 次
概述:當你在設計器中選擇一個組件時,組件屬性便顯示在對象檢查器中。你可以為屬性創建你自己的編輯器。本文主要介紹如何編寫屬性編輯器。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
當你在設計器中選擇一個組件時,組件屬性便顯示在對象檢查器中。你可以為屬性創建你自己的編輯器。例如,“Font”屬性也擁有一個編輯器:如果此屬性選擇了“...”按鈕,通過點擊此按鈕打開標準的 "font properties"對話框。
“TfrxPropertyEditor”是所有屬性編輯器的基類,并在“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)
多個屬性被定義在TfrxPropertyEditor類中:
Component:鏈接到父組件
frComponent:鏈接到父組件,但方便在某些情況下,轉換到TfrxComponent類型。
Designer:鏈接到報表設計器
ItemHeight:item高度
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”方法。由于這些屬性包含了32-bit對象的地址,因此,這些方法也可用于“TObject" 類型的屬性,如MyFont := TFont(GetOrdValue)。
你可以從基類中繼承并重寫公共部分聲明的一些方法,從而創建你自己的編輯器。“GetAttributes”是其中一個需要被重寫的方法。通過以下方式定義其屬性:
TfrxPropertyAttribute = (paValueList, paSortList, paDialog, paMultiSelect, paSubProperties, paReadOnly, paOwnerDraw); TfrxPropertyAttributes = set of TfrxPropertyAttribute
屬性編輯器注冊由frxDsgnIntf 文件中定義的程序執行:
procedure frxPropertyEditors.Register(PropertyType: PTypeInfo; ComponentClass: TClass; const PropertyName: String; EditorClass: TfrxPropertyEditorClass);
只有“PropertyType”參數需要進行指定。 “ComponentClass” 和/或 “PropertyName”參數可能為空。
讓我們來看看三個屬性編輯器的例子。FastReport VCL報表要求Editor code 存放在與component code所在文件夾同名的文件夾中,其后綴名為'Editor'。
{ 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 a 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 a picture, which is an example of the 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);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網