美文网首页
CodeTyphon 上位机开发入门系列-2.读写ini、jso

CodeTyphon 上位机开发入门系列-2.读写ini、jso

作者: 菜鸟笔记 | 来源:发表于2022-08-29 21:42 被阅读0次

    开发上位机经常会遇到要保存一些参数和数据到文件,其中ini、json、xml是最常用的格式。codetyphon有现成的控件可以,他们是IniPropStorage、JSONPropStorage、XMLPropStorage。通过简单的设置就可以实现配置文件的存储。

    IniPropStorage主要属性:

    1.property StoredValues: TStoredValues;
    存储的参数名和键值都在这里添加设置。
    2.property IniFileName: string; Name of the file where the property data is saved in INI format.
    要读取或存储的ini文件名称,支持相对路径。
    3.property IniSection: string; The section on the INI file where values are to be kept.
    设置设定参数所在的区域,理解就是参数的父节点名。
    4.property Active: Boolean;This determines if the object is active or not.
    这个建议设置成false,true运行发现有一些问题,原因不清楚。


    image.png

    程序设计

    1.新建application项目,拖拽控件,使界面如下:


    image.png

    2.设置主要属性:
    Active=false;
    IniFileName=config.ini;可以自己手动创建该文件,也可以让程序运行时自动创建;
    IniSection=Setup;
    StoredValues设置如下图:


    image.png

    程序代码

    unit main;
    
    {$mode objfpc}{$H+}
    
    interface
    
    uses
      Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
      IniPropStorage, ExtCtrls, Buttons, kedits;
    
    type
    
      { TForm1 }
    
      TForm1 = class(TForm)
        btnWriteINIFile: TBitBtn;
        btnExit: TButton;
        btnReadINIFile: TBitBtn;
        CheckBox1: TCheckBox;
        CheckBox2: TCheckBox;
        Edit2: TKNumberEdit;
        Edit3: TKNumberEdit;
        Edit4: TKNumberEdit;
        Edit6: TLabeledEdit;
        GroupBox1: TGroupBox;
        GroupBox2: TGroupBox;
        IniStorage1: TIniPropStorage;
        Edit1: TKNumberEdit;
        Edit5: TLabeledEdit;
        procedure btnExitClick(Sender: TObject);
        procedure btnReadINIFileClick(Sender: TObject);
        procedure btnWriteINIFileClick(Sender: TObject);
      private
    
      public
    
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.frm}
    
    { TForm1 }
    
    procedure TForm1.btnExitClick(Sender: TObject);
    begin
      Close;
    end;
    
    procedure TForm1.btnReadINIFileClick(Sender: TObject);
    var
      h:String;
    begin
      h:='';
      CheckBox2.Checked:=IniStorage1.ReadBoolean('AutoStart',False);
      Edit2.ValueAsInt:=IniStorage1.ReadInteger('Left',0);
      Edit4.Value:=IniStorage1.ReadString('Height','0.0').ToDouble;
      Edit6.Text:= IniStorage1.ReadString('Name','');
    end;
    
    procedure TForm1.btnWriteINIFileClick(Sender: TObject);
    begin
      IniStorage1.WriteBoolean('AutoStart',CheckBox1.Checked);
      IniStorage1.WriteInteger('Left',Edit1.ValueAsInt);
      IniStorage1.WriteString('Height',Edit3.ValueAsText);
      IniStorage1.WriteString('Name',Edit5.Text);
      //IniStorage1.Save;
    end;
    
    end.
    

    运行效果

    image.png

    JSON和XML文件操作与上面基本一样,不在细述。

    ···

    相关文章

      网友评论

          本文标题:CodeTyphon 上位机开发入门系列-2.读写ini、jso

          本文链接:https://www.haomeiwen.com/subject/ycefnrtx.html