☆PropertyGridを使ってみる。
PropertyGridを使って、アプリケーションでのオプション設定へ応用してみます。
応用などど大げさに書いていますが、MSDNの.NET Framework の PropertyGrid コントロールの高度な活用を参考にしています。
下記のプログラムで、きちんと動作しているようですが、C#と同じような属性部分の記述が今までのDelphiではなかっただけに、本当にこれでよいのか少し不安・・・。
次のようなオプション設定を前提にします。
<設定一覧>
[ウィンドウ]
Size - ウィンドウのサイズを設定します。
Color - ウィンドウ ウィンドウの色を設定します。
[エディタ]
Font - エディタのフォントを設定します。
[バージョン]
Version - MyAppのバージョンを表示します。
[一般設定]
RecentFiles - 最近使ったファイルの表示数
DefaultFileName - デフォルトファイル名
[計算オプション]
CalcOptions
AutoCalc - 自動計算機能
UserFunc - ユーザー関数機能
<プログラム>
フォームにPropertyGridを一つ配置しただけです。
unit WinForm; interface uses System.Drawing, System.Collections, System.ComponentModel, System.Windows.Forms, System.Data, System.Globalization; //System.Globalizationは自前で記述した。 type //ドロップダウン プロパティのサポートを提供します。 //DefaultFileName プロパティは文字列型であるため、StringConverter から継承します。 TFileNameConverter = class(StringConverter) public function GetStandardValuesSupported (Context: ITypeDescriptorContext): Boolean; override; function GetStandardValues(Context: ITypeDescriptorContext): TypeConverter.StandardValuesCollection; override; function GetStandardValuesExclusive (Context: ITypeDescriptorContext): Boolean; override; end; TCalcOptionsConverter = class(ExpandableObjectConverter) public function CanConvertTo(Context: ITypeDescriptorContext; DestinationType: System.Type): Boolean; override; function ConvertTo(Context: ITypeDescriptorContext; Culture: CultureInfo; Value: TObject; DestinationType: System.Type): TObject; override; function CanConvertFrom(Context: ITypeDescriptorContext; SourceType: System.Type): Boolean; override; function ConvertFrom(Context: ITypeDescriptorContext; Culture: CultureInfo; Value: TObject): TObject; override; end; TCalcOptions = class private FAutoCalc: Boolean; FUserFunc: Boolean; public constructor Create; published [DefaultValueAttribute(True)] property AutoCalc: Boolean read FAutoCalc write FAutoCalc; [DefaultValueAttribute(False)] property UserFunc: Boolean read FUserFunc write FUserFunc; end; [DefaultPropertyAttribute('RecentFiles')] TMyAppSettings = class private FWindowSize: Size; FWindowColor: Color; FEditorFont: Font; FVersion: String; FRecentFiles: Integer; FDefaultFileName: String; FCalcOptions: TCalcOptions; public constructor Create; published [CategoryAttribute('ウィンドウ'), DescriptionAttribute('アプリケーションのウィンドウサイズを設定します。')] property Size: Size read FWindowSize write FWindowSize; [CategoryAttribute('ウィンドウ'), DescriptionAttribute('アプリケーションのウィンドウカラーを設定します。')] property Color: Color read FWindowColor write FWindowColor; [CategoryAttribute('エディタ'), DescriptionAttribute('エディタのフォントを設定します。')] property Font: Font read FEditorFont write FEditorFont; //[BrowsableAttribute(False), DefaultValueAttribute(False)] [CategoryAttribute('バージョン'), DefaultValueAttribute('1.0'), ReadOnlyAttribute(True)] property Version: String read FVersion write FVersion; [CategoryAttribute('一般設定'), DefaultValueAttribute(5), DescriptionAttribute('最近使ったファイルの表示数を設定します。')] property RecentFiles: Integer read FRecentFiles write FRecentFiles; [TypeConverter(TypeOf(TFileNameConverter)), CategoryAttribute('一般設定'), DescriptionAttribute('新規作成時のファイル名を設定します。')] property DefaultFileName: String read FDefaultFileName write FDefaultFileName; [TypeConverterAttribute(TypeOf(TCalcOptionsConverter)), DescriptionAttribute('計算オプションを展開して表示します。')] [CategoryAttribute('計算オプション')] property CalcOptions: TCalcOptions read FCalcOptions write FCalcOptions; end; TWinForm = class(System.Windows.Forms.Form) (略) private MyAppSettings: TMyAppSettings; (略) end; (略) implementation (略) procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs); begin MyAppSettings := TMyAppSettings.Create; PropertyGrid1.SelectedObject := MyAppSettings; PropertyGrid1.ToolbarVisible := False; end; { TFileNameConverter } function TFileNameConverter.GetStandardValuesSupported( Context: ITypeDescriptorContext): Boolean; begin Result := True; end; function TFileNameConverter.GetStandardValues( Context: ITypeDescriptorContext): TypeConverter.StandardValuesCollection; var S: ArrayList; begin S := ArrayList.Create; S.Add('新しいファイル'); S.Add('報告書'); S.Add('計算書'); Result := StandardValuesCollection.Create(S); end; function TFileNameConverter.GetStandardValuesExclusive( Context: ITypeDescriptorContext): Boolean; begin Result := False; end; { TCalcOptionsConverter } function TCalcOptionsConverter.CanConvertTo( Context: ITypeDescriptorContext; DestinationType: System.Type): Boolean; begin if (DestinationType= TypeOf(TCalcOptions)) then Result := True else Result := inherited CanConvertTo(Context, DestinationType); end; function TCalcOptionsConverter.ConvertTo( Context: ITypeDescriptorContext; Culture: CultureInfo; Value: TObject; DestinationType: System.Type): TObject; var CO: TCalcOptions; begin if ((DestinationType = TypeOf(System.String)) and (Value is TCalcOptions)) then begin CO := TCalcOptions(Value); Result := '自動計算機能:' + Convert.ToString(CO.AutoCalc) + '、ユーザー関数機能: ' + Convert.ToString(CO.UserFunc); end else Result := inherited ConvertTo(Context, Culture, Value, DestinationType); end; function TCalcOptionsConverter.CanConvertFrom( Context: ITypeDescriptorContext; SourceType: System.Type): Boolean; begin if (SourceType = TypeOf(String)) then Result := True else Result := inherited CanConvertFrom(Context, SourceType); end; function TCalcOptionsConverter.ConvertFrom( Context: ITypeDescriptorContext; Culture: CultureInfo; Value: TObject): TObject; var AutoCalc, UserFunc: String; S: String; Colon, Comma: Integer; CO: TCalcOptions; begin if (Value = TypeOf(String)) then begin try S := Convert.ToString(Value); Colon := S.IndexOf(':'); Comma := S.IndexOf(','); if (Colon <> -1) and (Comma <> -1) then begin AutoCalc := S.Substring(Colon + 1 , (Comma - Colon - 1)); Colon := S.IndexOf(':', Comma + 1); Comma := S.IndexOf(',', Comma + 1); UserFunc := S.Substring(Colon + 1 , (Comma - Colon -1)); CO := TCalcOptions.Create; CO.AutoCalc :=System.Boolean.Parse(AutoCalc); CO.UserFunc := System.Boolean.Parse(UserFunc); Result := CO; Exit; end; except end; end; Result := inherited ConvertFrom(Context, Culture, Value); end; { TCalcOptions } constructor TCalcOptions.Create; begin inherited Create; AutoCalc := True; UserFunc := False; end; { TMyAppSettings } constructor TMyAppSettings.Create; begin inherited Create; Size := System.Drawing.Size.Create(100,100); Color := SystemColors.Control; Font := System.Drawing.Font.Create('MS UI Gothic', 9, FontStyle.Regular); Version := '1.0'; RecentFiles := 5; DefaultFileName := ''; CalcOptions := TCalcOptions.Create; end; end.
| 固定リンク