Delphi 实现Ini文件参数与TEdit和TCheckBox绑定(TSimpleParam)

在实际工作中,常遇到对Ini参数的修改,显示与保存问题。

如果手工写代码,会有很大的工作量。

本例把ini文件的参数与控件绑定起来,以达到方便使用的目的。

本例程共有2个单元

uSimpleParam->TSimpleParam;//本功能

uSimpleList->TSimpleList;用泛型实现的TList,更实用一点

源码下载

 1 //用法:
 2 
 3 const
 4 
 5   csEditSimpleParam = 'EditSimpleParam';
 6   csCheckBoxParam = 'CheckBoxParam';
 7 
 8 
 9 FIniFile := TIniFile.Create('.\SimpleParams.ini');
10 FSimpleParam := TSimpelParam.Create;
11 
12 FSimpleParam.IniFile := FIniFile; // 指定 IniFile
13 
14 // 绑定控件,并设置默认参数
15 FSimpleParam.Binding(edtSimpleParam, csEditSimpleParam).SetInteger(101);
16 FSimpleParam.Binding(chkSimpleParam, csCheckBoxParam).SetBoolean(true);
17 
18 // 加载参数
19 FSimpleParam.LoadParams;
20 
21 FSimpleParam.SaveParams; // 保存参数
22 
23 //获取参数
24 nSimpleParam := FSimpleParam[csEditSimpleParam].AsInteger;
25 bSimleParam := FSimpleParam[csCheckBoxParam].AsBoolean;
26 
27 //设置参数
28 FSimpleParam[csEditSimpleParam].SetInteger(105);
29 FSimpleParam[csCheckBoxParam].SetBoolean(false);
  1 unit uSimpleParam;
  2 
  3 interface
  4 
  5 uses
  6   uSimpleList, Generics.Collections, Vcl.StdCtrls, Vcl.Controls, IniFiles;
  7 
  8 type
  9 
 10   TDefaultType = (dtInteger, dtString, dtBoolean);
 11 
 12   TBaseParam<T: TWinControl> = class
 13   private
 14     FCtrl: T;
 15     FIndent: string;
 16     FDefaultType: TDefaultType;
 17     FDefaultInteger: integer;
 18     FDefaultString: string;
 19     FDefaultBoolean: boolean;
 20 
 21   public
 22 
 23     function AsInteger: integer; virtual;
 24     function AsString: string; virtual;
 25     function AsBoolean: boolean; virtual;
 26 
 27     procedure SetInteger(val: integer); virtual;
 28     procedure SetString(val: string); virtual;
 29     procedure SetBoolean(val: boolean); virtual;
 30 
 31     procedure SetDefaultInteger(val: integer);
 32     procedure SetDefaultString(val: string);
 33     procedure SetDefaultBoolean(val: boolean);
 34 
 35     procedure Binding(ACtrl: T; AIndent: string); virtual;
 36 
 37   end;
 38 
 39   TEditParam = class(TBaseParam<TEdit>)
 40   public
 41     function AsInteger: integer; override;
 42     function AsString: string; override;
 43     procedure SetInteger(val: integer); override;
 44     procedure SetString(val: string); override;
 45     procedure Binding(ACtrl: TEdit; AIndent: string); override;
 46   end;
 47 
 48   TCheckBoxParam = class(TBaseParam<TCheckBox>)
 49   public
 50     function AsBoolean: boolean; override;
 51     procedure SetBoolean(val: boolean); override;
 52     procedure Binding(ACtrl: TCheckBox; AIndent: string); override;
 53   end;
 54 
 55   TBaseParamList = class(TClassSimpleList < TBaseParam < TWinControl >> )
 56   public
 57     function Binding(ACtrl: TWinControl; AIndent: string): TBaseParam<TWinControl>;
 58   end;
 59 
 60   TSimpelParam = class
 61     FParamList: TBaseParamList;
 62   private
 63     FIniFile: TIniFile;
 64     FSectionIndent: String;
 65 
 66     procedure SetIniFile(val: TIniFile);
 67     procedure SetSectionIndent(val: String);
 68     function Get(AIndent: string): TBaseParam<TWinControl>;
 69 
 70   public
 71 
 72     constructor Create;
 73     destructor Destroy; override;
 74 
 75     property IniFile: TIniFile read FIniFile write SetIniFile;
 76     property SectionIndent: String read FSectionIndent write SetSectionIndent;
 77     property Items[AIndent: string]: TBaseParam<TWinControl> Read Get; default;
 78 
 79     function Binding(ACtrl: TWinControl; AIndent: string): TBaseParam<TWinControl>;
 80 
 81     procedure LoadParams;
 82     procedure SaveParams;
 83 
 84   end;
 85 
 86 implementation
 87 
 88 uses
 89   SysUtils;
 90 
 91 function TEditParam.AsInteger: integer;
 92 begin
 93   Result := StrToIntDef(FCtrl.Text, 0);
 94 end;
 95 
 96 function TEditParam.AsString: string;
 97 begin
 98   Result := FCtrl.Text;
 99 end;
100 
101 procedure TEditParam.Binding(ACtrl: TEdit; AIndent: string);
102 begin
103   inherited;
104 end;
105 
106 procedure TEditParam.SetInteger(val: integer);
107 begin
108   inherited;
109   if Assigned(FCtrl) then
110     FCtrl.Text := IntToStr(val);
111 end;
112 
113 procedure TEditParam.SetString(val: string);
114 begin
115   inherited;
116   if Assigned(FCtrl) then
117     FCtrl.Text := val;
118 end;
119 
120 function TCheckBoxParam.AsBoolean: boolean;
121 begin
122   Result := FCtrl.Checked;
123 end;
124 
125 procedure TCheckBoxParam.Binding(ACtrl: TCheckBox; AIndent: string);
126 begin
127   inherited;
128 end;
129 
130 procedure TCheckBoxParam.SetBoolean(val: boolean);
131 begin
132   inherited;
133   if Assigned(FCtrl) then
134     FCtrl.Checked := val;
135 end;
136 
137 { TBaseParam<T> }
138 
139 function TBaseParam<T>.AsBoolean: boolean;
140 begin
141 
142 end;
143 
144 function TBaseParam<T>.AsInteger: integer;
145 begin
146 
147 end;
148 
149 function TBaseParam<T>.AsString: string;
150 begin
151 
152 end;
153 
154 procedure TBaseParam<T>.Binding(ACtrl: T; AIndent: string);
155 begin
156   FCtrl := ACtrl;
157   FIndent := AIndent;
158 end;
159 
160 function TBaseParamList.Binding(ACtrl: TWinControl; AIndent: string): TBaseParam<TWinControl>;
161 begin
162 
163   Result := nil;
164 
165   if ACtrl is TEdit then
166     Result := TBaseParam<TWinControl>(TEditParam.Create)
167   else if ACtrl is TCheckBox then
168     Result := TBaseParam<TWinControl>(TCheckBoxParam.Create);
169 
170   if Assigned(Result) then
171   begin
172     Result.Binding(ACtrl, AIndent);
173     Add(Result)
174   end;
175 
176 end;
177 
178 procedure TBaseParam<T>.SetBoolean(val: boolean);
179 begin
180 
181 end;
182 
183 procedure TBaseParam<T>.SetDefaultBoolean(val: boolean);
184 begin
185   FDefaultBoolean := val;
186   FDefaultType := dtBoolean;
187 end;
188 
189 procedure TBaseParam<T>.SetDefaultInteger(val: integer);
190 begin
191   FDefaultInteger := val;
192   FDefaultType := dtInteger;
193 end;
194 
195 procedure TBaseParam<T>.SetDefaultString(val: string);
196 begin
197   FDefaultString := val;
198   FDefaultType := dtString;
199 end;
200 
201 procedure TBaseParam<T>.SetInteger(val: integer);
202 begin
203 end;
204 
205 procedure TBaseParam<T>.SetString(val: string);
206 begin
207 
208 end;
209 
210 { TSimpelParam }
211 
212 function TSimpelParam.Binding(ACtrl: TWinControl; AIndent: string): TBaseParam<TWinControl>;
213 begin
214   Result := FParamList.Binding(ACtrl, AIndent);
215 end;
216 
217 constructor TSimpelParam.Create;
218 begin
219   inherited;
220   FParamList := TBaseParamList.Create;
221   FSectionIndent := 'Main';
222 end;
223 
224 destructor TSimpelParam.Destroy;
225 begin
226   FParamList.Free;
227   inherited;
228 end;
229 
230 function TSimpelParam.Get(AIndent: string): TBaseParam<TWinControl>;
231 var
232   i: integer;
233 begin
234   Result := nil;
235   for i := 0 to FParamList.Count - 1 do
236   begin
237     if SameText(AIndent, FParamList[i].FIndent) then
238     begin
239       Result := FParamList[i];
240       exit;
241     end;
242   end;
243 end;
244 
245 procedure TSimpelParam.LoadParams;
246 var
247   B: TBaseParam<TWinControl>;
248 begin
249   if Assigned(FIniFile) then
250   begin
251     for B in FParamList do
252     begin
253       if B.ClassName = 'TEditParam' then
254       begin
255 
256         if B.FDefaultType = dtString then
257         begin
258           TEdit(B.FCtrl).Text := IniFile.ReadString(FSectionIndent, B.FIndent, B.FDefaultString);
259         end
260         else if B.FDefaultType = dtInteger then
261         begin
262           TEdit(B.FCtrl).Text := IntToStr(IniFile.ReadInteger(FSectionIndent, B.FIndent,
263             B.FDefaultInteger));
264         end;
265 
266       end
267       else if B.ClassName = 'TCheckBoxParam' then
268       begin
269         TCheckBox(B.FCtrl).Checked := IniFile.ReadBool(FSectionIndent, B.FIndent,
270           B.FDefaultBoolean);
271       end;
272 
273     end;
274   end;
275 end;
276 
277 procedure TSimpelParam.SaveParams;
278 var
279   B: TBaseParam<TWinControl>;
280 begin
281   if Assigned(FIniFile) then
282   begin
283     for B in FParamList do
284     begin
285       if B.ClassName = 'TEditParam' then
286       begin
287         FIniFile.WriteString(FSectionIndent, B.FIndent, TEdit(B.FCtrl).Text);
288       end
289       else if B.ClassName = 'TCheckBoxParam' then
290       begin
291         FIniFile.WriteBool(FSectionIndent, B.FIndent, TCheckBox(B.FCtrl).Checked);
292       end;
293     end;
294   end;
295 end;
296 
297 procedure TSimpelParam.SetIniFile(val: TIniFile);
298 begin
299   FIniFile := val;
300 end;
301 
302 procedure TSimpelParam.SetSectionIndent(val: String);
303 begin
304   FSectionIndent := val;
305 end;
306 
307 end.
uSimpleParam.pas

代码略长,请耐心看。可以当成面向对象的基础用法来学习。

附:delphi 进阶基础技能说明

posted on 2016-04-08 13:05  晓不得2013  阅读(1208)  评论(0编辑  收藏  举报

导航