标签:
本例程在 Delphi XE8 版本下运行
Delphi群:59129236
把Delphi做为工作的辅助技能,走向幸福人生!
晓不得2013 QQ:26562729
SimpleParamDemo 功能:
把ini文件的参数与控件绑定起来,以达到方便使用的目的。
本例程共有2个单元
uSimpleParam->TSimpleParam;//本功能
uSimpleList->TSimpleList;用泛型实现的TList,更实用一点
源码下载:http://files.cnblogs.com/files/lackey/SimpleParamDemo.zip
用法:
const
csEditSimpleParam = ‘EditSimpleParam‘;
csCheckBoxParam = ‘CheckBoxParam‘;
FIniFile := TIniFile.Create(‘.\SimpleParams.ini‘);
FSimpleParam := TSimpelParam.Create;
FSimpleParam.IniFile := FIniFile; // 指定 IniFile
// 绑定控件,并设置默认参数
FSimpleParam.Binding(edtSimpleParam, csEditSimpleParam).SetInteger(101);
FSimpleParam.Binding(chkSimpleParam, csCheckBoxParam).SetBoolean(true);
// 加载参数
FSimpleParam.LoadParams;
FSimpleParam.SaveParams; // 保存参数
//获取参数
nSimpleParam := FSimpleParam[csEditSimpleParam].AsInteger;
bSimleParam := FSimpleParam[csCheckBoxParam].AsBoolean;
//设置参数
FSimpleParam[csEditSimpleParam].SetInteger(105);
FSimpleParam[csCheckBoxParam].SetBoolean(false);
unit uSimpleParam; interface uses uSimpleList, Generics.Collections, Vcl.StdCtrls, Vcl.Controls, IniFiles; type TDefaultType = (dtInteger, dtString, dtBoolean); TBaseParam<T: TWinControl> = class private FCtrl: T; FIndent: string; FDefaultType: TDefaultType; FDefaultInteger: integer; FDefaultString: string; FDefaultBoolean: boolean; public function AsInteger: integer; virtual; function AsString: string; virtual; function AsBoolean: boolean; virtual; procedure SetInteger(val: integer); virtual; procedure SetString(val: string); virtual; procedure SetBoolean(val: boolean); virtual; procedure SetDefaultInteger(val: integer); procedure SetDefaultString(val: string); procedure SetDefaultBoolean(val: boolean); procedure Binding(ACtrl: T; AIndent: string); virtual; end; TEditParam = class(TBaseParam<TEdit>) public function AsInteger: integer; override; function AsString: string; override; procedure SetInteger(val: integer); override; procedure SetString(val: string); override; procedure Binding(ACtrl: TEdit; AIndent: string); override; end; TCheckBoxParam = class(TBaseParam<TCheckBox>) public function AsBoolean: boolean; override; procedure SetBoolean(val: boolean); override; procedure Binding(ACtrl: TCheckBox; AIndent: string); override; end; TBaseParamList = class(TClassSimpleList < TBaseParam < TWinControl >> ) public function Binding(ACtrl: TWinControl; AIndent: string): TBaseParam<TWinControl>; end; TSimpelParam = class FParamList: TBaseParamList; private FIniFile: TIniFile; FSectionIndent: String; procedure SetIniFile(val: TIniFile); procedure SetSectionIndent(val: String); function Get(AIndent: string): TBaseParam<TWinControl>; public constructor Create; destructor Destroy; override; property IniFile: TIniFile read FIniFile write SetIniFile; property SectionIndent: String read FSectionIndent write SetSectionIndent; property Items[AIndent: string]: TBaseParam<TWinControl> Read Get; default; function Binding(ACtrl: TWinControl; AIndent: string): TBaseParam<TWinControl>; procedure LoadParams; procedure SaveParams; end; implementation uses SysUtils; function TEditParam.AsInteger: integer; begin Result := StrToIntDef(FCtrl.Text, 0); end; function TEditParam.AsString: string; begin Result := FCtrl.Text; end; procedure TEditParam.Binding(ACtrl: TEdit; AIndent: string); begin inherited; end; procedure TEditParam.SetInteger(val: integer); begin inherited; if Assigned(FCtrl) then FCtrl.Text := IntToStr(val); end; procedure TEditParam.SetString(val: string); begin inherited; if Assigned(FCtrl) then FCtrl.Text := val; end; function TCheckBoxParam.AsBoolean: boolean; begin Result := FCtrl.Checked; end; procedure TCheckBoxParam.Binding(ACtrl: TCheckBox; AIndent: string); begin inherited; end; procedure TCheckBoxParam.SetBoolean(val: boolean); begin inherited; if Assigned(FCtrl) then FCtrl.Checked := val; end; { TBaseParam<T> } function TBaseParam<T>.AsBoolean: boolean; begin end; function TBaseParam<T>.AsInteger: integer; begin end; function TBaseParam<T>.AsString: string; begin end; procedure TBaseParam<T>.Binding(ACtrl: T; AIndent: string); begin FCtrl := ACtrl; FIndent := AIndent; end; function TBaseParamList.Binding(ACtrl: TWinControl; AIndent: string): TBaseParam<TWinControl>; begin Result := nil; if ACtrl is TEdit then Result := TBaseParam<TWinControl>(TEditParam.Create) else if ACtrl is TCheckBox then Result := TBaseParam<TWinControl>(TCheckBoxParam.Create); if Assigned(Result) then begin Result.Binding(ACtrl, AIndent); Add(Result) end; end; procedure TBaseParam<T>.SetBoolean(val: boolean); begin end; procedure TBaseParam<T>.SetDefaultBoolean(val: boolean); begin FDefaultBoolean := val; FDefaultType := dtBoolean; end; procedure TBaseParam<T>.SetDefaultInteger(val: integer); begin FDefaultInteger := val; FDefaultType := dtInteger; end; procedure TBaseParam<T>.SetDefaultString(val: string); begin FDefaultString := val; FDefaultType := dtString; end; procedure TBaseParam<T>.SetInteger(val: integer); begin end; procedure TBaseParam<T>.SetString(val: string); begin end; { TSimpelParam } function TSimpelParam.Binding(ACtrl: TWinControl; AIndent: string): TBaseParam<TWinControl>; begin Result := FParamList.Binding(ACtrl, AIndent); end; constructor TSimpelParam.Create; begin inherited; FParamList := TBaseParamList.Create; FSectionIndent := ‘Main‘; end; destructor TSimpelParam.Destroy; begin FParamList.Free; inherited; end; function TSimpelParam.Get(AIndent: string): TBaseParam<TWinControl>; var i: integer; begin Result := nil; for i := 0 to FParamList.Count - 1 do begin if SameText(AIndent, FParamList[i].FIndent) then begin Result := FParamList[i]; exit; end; end; end; procedure TSimpelParam.LoadParams; var B: TBaseParam<TWinControl>; begin if Assigned(FIniFile) then begin for B in FParamList do begin if B.ClassName = ‘TEditParam‘ then begin if B.FDefaultType = dtString then begin TEdit(B.FCtrl).Text := IniFile.ReadString(FSectionIndent, B.FIndent, B.FDefaultString); end else if B.FDefaultType = dtInteger then begin TEdit(B.FCtrl).Text := IntToStr(IniFile.ReadInteger(FSectionIndent, B.FIndent, B.FDefaultInteger)); end; end else if B.ClassName = ‘TCheckBoxParam‘ then begin TCheckBox(B.FCtrl).Checked := IniFile.ReadBool(FSectionIndent, B.FIndent, B.FDefaultBoolean); end; end; end; end; procedure TSimpelParam.SaveParams; var B: TBaseParam<TWinControl>; begin if Assigned(FIniFile) then begin for B in FParamList do begin if B.ClassName = ‘TEditParam‘ then begin FIniFile.WriteString(FSectionIndent, B.FIndent, TEdit(B.FCtrl).Text); end else if B.ClassName = ‘TCheckBoxParam‘ then begin FIniFile.WriteBool(FSectionIndent, B.FIndent, TCheckBox(B.FCtrl).Checked); end; end; end; end; procedure TSimpelParam.SetIniFile(val: TIniFile); begin FIniFile := val; end; procedure TSimpelParam.SetSectionIndent(val: String); begin FSectionIndent := val; end; end.
unit uSimpleList; interface uses Generics.Collections; type TSimpleList<T> = class(TList<T>) private FCurIndexPos: integer; function DoPopByIndex(Index: integer): T; procedure FreeAllItems; procedure SetCurIndexPos(const Value: integer); protected FNeedFreeItem: boolean; procedure FreeItem(Item: T); virtual; public constructor Create; destructor Destroy; override; procedure Lock; procedure Unlock; function PopFirst: T; function PopLast: T; function PopByIndex(Index: integer): T; procedure ClearAndFreeAllItems; property CurIndexPos: integer read FCurIndexPos write SetCurIndexPos; end; TClassSimpleList<T: Class, Constructor> = class(TSimpleList<T>) protected procedure FreeItem(Item: T); override; function AddNewOne: T; end; implementation procedure TSimpleList<T>.ClearAndFreeAllItems; begin FreeAllItems; clear; end; constructor TSimpleList<T>.Create; begin inherited; FNeedFreeItem := true; FCurIndexPos := -1; end; destructor TSimpleList<T>.Destroy; begin FreeAllItems; inherited; end; function TSimpleList<T>.DoPopByIndex(Index: integer): T; begin if (index >= 0) and (index <= count - 1) then begin result := items[index]; Delete(index); Exit; end; result := T(nil); end; procedure TSimpleList<T>.FreeAllItems; var Item: T; begin if FNeedFreeItem then begin FCurIndexPos := -1; for Item in self do FreeItem(Item); end; end; procedure TSimpleList<T>.FreeItem(Item: T); begin // 假设 T 是 PMyRec =^TMyRec TMyRec=record; // 这个写法对吗? // if GetTypeKind(T) = tkPointer then // begin // Dispose(Pointer(Pointer(@Item)^)); // end; end; procedure TSimpleList<T>.Lock; begin system.TMonitor.Enter(self); end; procedure TSimpleList<T>.Unlock; begin system.TMonitor.Exit(self); end; function TSimpleList<T>.PopByIndex(Index: integer): T; begin result := DoPopByIndex(index); end; function TSimpleList<T>.PopFirst: T; begin result := DoPopByIndex(0); end; function TSimpleList<T>.PopLast: T; begin result := DoPopByIndex(count - 1); end; procedure TSimpleList<T>.SetCurIndexPos(const Value: integer); begin FCurIndexPos := Value; end; { TThreadClassList<T> } function TClassSimpleList<T>.AddNewOne: T; begin result := T.Create(); Add(result); end; procedure TClassSimpleList<T>.FreeItem(Item: T); begin Item.Free; end; end.
Delphi 实现Ini文件参数与TEdit和TCheckBox绑定(TSimpleParam)
标签:
原文地址:http://www.cnblogs.com/lackey/p/5367617.html