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

本例程在 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.

  

时间: 2024-07-29 12:54:53

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

Delphi&#160;操作Ini文件

Delphi提供了一个TInifile类,使我们可以非常灵活的处理INI文件 一.INI文件的结构[小节名]ini文件       关键字1=值1       关键子2=值2INI文件允许有多个小节,每个小节又允许有多个关键字,“=”后面是该关键字的值.值的类型有三种:字符串.整型数值和布尔值.其中字符串存贮在INI文件中时没有引号,布尔真值用1表示,布尔假值用0表示. 二.定义 1.           在Interface的Uses节增加IniFiles:2.           在Var变

Delphi 对ini文件的操作

界面如图: 代码如下: 1 unit Unit1; 2 3 interface 4 5 uses 6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,IniFiles; //添加库 IniFiles 8 9 type 10 TForm1 = cla

INI文件读写

我们在编写程序的过程中,有很多要用到出始化,但有很多都写进了注册表,而我们在想修改或者查看的时候必须到注册表里去修改,而不是直接到一个程序目录下的ini文件里查看或者修改,怎么使可以直接生成的ini文件能写进Debug 呢?而不写进注册表. 1 //初始化ini文件参数 2 typedef struct _INITFILE 3 { 4 int flag; //界面切换提示标志,开机自启动无需提示,但是启动后切换的话需要提示 5 CString pathName;//ini文件保存路径 6 CSt

封装 INI 文件读写函数

delphi读写ini文件实例 //--两个过程,主要实现:窗体关闭的时候,文件保存界面信息:窗体创建的时候,程序读取文件文件保存的信息. //--首先要uses IniFiles(单元) //--窗体创建的时候,读取ini文件信息 procedure TfrmAFN04H_F9.FormCreate(Sender: TObject); var vFIni: TIniFile; sFileName: string; begin sFileName := ExtractFileDir(Applic

Delphi 在写Ini文件时报错,Access violation at address 774D6EC8 in module &#39;ntdll.dll&#39; write of address 004044CD

检查代码发现读写Ini文件函数ReadString参数错误导致:如下 节点不能为空. Delphi 在写Ini文件时报错,Access violation at address 774D6EC8 in module 'ntdll.dll' write of address 004044CD

读取INI文件 - Delphi篇

程序经常需要读取一些用户设定值,怎么完成这个过程? B/S程序一般使用XML文件,而C/S程序则使用INI文件. 前篇<C#迁移之callXBFLibrary - 2(调用非托管DLL)>是C#读取INI的示例. 本篇介绍使用Delphi完成这个过程. 首先,引用单元. uses Windows, SysUtils, Classes, DB, ADODB, StrUtils, Forms, IniFiles; 其中"IniFiles"即是我们要引用的单元. 然后,定义类变量

WEKA运行参数修改(RunWeka.ini文件)

一般使用weka进行数据挖掘的时候会碰到两个问题,一是内存不够,二是libsvm使用不了,这时就需要重新配置RunWeka.ini文件,解决上述问题.查看RunWeka.ini原文如下: 1 # Contains the commands for running Weka either with a command prompt 2 # ("cmd_console") or without the command prompt ("cmd_default"). 3

Delphi INI 文件读写

delphi中,配置文件的相关操作. (1) INI文件的结构: ;这是关于INI文件的注释部分 [节点] 关键字=值 ... INI文件允许有多个节点,每个节点又允许有多个关键字, “=”后面是该关键字的值(类型有三种:字符串.整型数值和布尔值.其中字符串存贮在INI文件中时没有引号,布尔真值用1表示,布尔假值用0表示).注释以分号“;”开头. (2) INI文件的操作 1. 在Interface的Uses节增加IniFiles: 2. 在Var变量定义部分增加一行:inifile:Tinif

Delphi INI文件保存与读取

//需要引用IniFiles uses system.IniFiles; //保存INI配置文件 procedure TForm1.btnSaveClick(Sender: TObject); var myIniFile: TIniFile; filepath: string; begin filepath := ExtractFilePath(Application.Exename) + 'DBConfig.ini'; //取得ini文件的路径 myIniFile := TIniFile.Cr