网页素材: 导航菜单选项卡滑动门焦点图图片切换下拉菜单弹出层Tabs链接提示

您的当前位置: 源码爱好者 » 网页制作教程(编程教程) » Delphi技术教程

窗体文字从INI文件中读取 Delphi操作INI

  • 标签:ini文件 读写ini  更新时间:2014-07-17
  • Delphi操作INI文件读写功能,窗体上的文字是从INI文件中读取出来的,INI文件常被用作配置文件,若你是Delphi新手,对此不太了解的话,这个例子比较简单,适合学习参考:

    unit IniForm;
    interface
    uses
      SysUtils, Windows, Messages, Classes, Graphics, Controls,
      Forms, Dialogs, IniFiles, StdCtrls;
    type
      TForm1 = class(TForm)
        Label1: TLabel;
        Button1: TButton;
        Button2: TButton;
        procedure FormCreate(Sender: TObject);
        procedure FormClose(Sender: TObject;
          var Action: TCloseAction);
        procedure Button2Click(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        IniFile: TIniFile;
      public
        { Public declarations }
      end;
    var
      Form1: TForm1;
    implementation
    {$R *.DFM}
    procedure TForm1.FormCreate(Sender: TObject);
    var
      Status: Integer;
    begin
      IniFile := TIniFile.Create (ExtractFilePath (
        Application.ExeName) + 'ini_one.ini');
      //设定ini文件对象
      Status := IniFile.ReadInteger ('MainForm', 'Status', 0);
      if Status <> 0 then
      begin
      //读取ini文件中的内容
        Top := IniFile.ReadInteger ('MainForm', 'Top', Top);
        Left := IniFile.ReadInteger ('MainForm', 'Left', Left);
        Width := IniFile.ReadInteger ('MainForm', 'Width', Width);
        Height := IniFile.ReadInteger ('MainForm', 'Height', Height);
        label1.Caption:=IniFile.ReadString('label1','caption',label1.Caption);
        button1.Visible:=IniFile.ReadBool('button1','visible',button1.visible);
        button2.Visible:=IniFile.ReadBool('button2','visible',button2.visible);
        case Status of
          1: WindowState := wsNormal;
          2: WindowState := wsMinimized;
          3: WindowState := wsMaximized;
        end;
      end;
    end;
    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    var
      Status: Integer;
    begin
      if MessageDlg ('保存当前的设置吗',
        mtConfirmation, [mbYes, mbNo], 0) = IdYes then
      begin
        Status := 1; // default
        case WindowState of
          wsNormal: begin
          //向ini文件中写入内容
            {save position and size only if the state is normal}
            IniFile.WriteInteger ('MainForm', 'Top', Top);
            IniFile.WriteInteger ('MainForm', 'Left', Left);
            IniFile.WriteInteger ('MainForm', 'Width', Width);
            IniFile.WriteInteger ('MainForm', 'Height', Height);
            IniFile.WriteString('label1','caption',label1.Caption);
            IniFile.WriteBool('Button1','Visible',Button1.Visible);
            IniFile.WriteBool('Button2','Visible',Button2.Visible);
          end;
          wsMinimized: Status := 2;
          wsMaximized: Status := 3;
        end;
        if not Active then
          Status := 2;
        IniFile.WriteInteger ('MainForm', 'Status', Status);
      end;
      IniFile.Free;
    end;
    procedure TForm1.Button2Click(Sender: TObject);
    //中文显示
    begin
      label1.Caption:='欢迎来到delphi世界';
      button2.Visible:=false;
      button1.Visible:=true;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    //英文显示
    begin
      label1.Caption:='Welcome to delphi''s word';
      button1.Visible:=false;
      button2.visible:=true;
    end;
    end.

     

  • 已读: 次 收藏本文关闭本文打印本文复制链接
处理 SSI 文件时出错