お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"Ini ファイルにコンポを読み書き"

この発言は
#00821 本田勝彦 さんのレジストリにコンポを読み書き
に対するコメントです

本田勝彦 さんこんにちは。 Ini ファイルにも読み書きできまっせ〜遅いけど(^^;) TComponent を丸ごと Ini ファイルに読み書き出来ます。 #00821 にも該当することなのですが、コンポがぺたぺたされたフォームとか パネルなど、子コントロールを持っているコンポーネントの保存・復帰は 出来ません。 <TIniFile>> //---------------------------------------------------------------------- uses IniFiles; procedure StreamToStrings(Stream: TStream; Strings: TStrings); const LineByte = 64; var S: String; B: Byte; begin Strings.BeginUpdate; try Strings.Clear; S := ''; while Stream.Read(B, SizeOf(B)) > 0 do begin S := S + IntToHex(B, 2); if Length(S) >= LineByte then begin Strings.Add(S); S := ''; end; end; if S <> '' then Strings.Add(S); finally Strings.EndUpdate; end; end; procedure StringsToStream(Strings: TStrings; Stream: TStream); var I: Integer; S: String; B: Byte; begin for I := 0 to Strings.Count - 1 do begin S := Strings[I]; while Length(S) > 0 do begin B := StrToIntDef('$' + Copy(S, 1, 2), 0); Stream.Write(B, SizeOf(B)); Delete(S, 1, 2); end; end; end; procedure ErrorMessage; var Form: TForm; begin Form := CreateMessageDialog( '子コントロールを所有するコンポーネントの保存・復帰出来ません。', mtError, [mbOk]); try Form.Caption := 'hogehoge'; Form.ShowModal; Abort; finally Form.Free; end; end; procedure IniWriteComponent(const IniFileName, Section, Ident: String; Component: TComponent); var Ini: TIniFile; List: TStringList; Ms: TMemoryStream; IdentName: String; I: Integer; begin if (Component = nil) or ((Component is TWinControl) and (TWinControl(Component).ControlCount > 0)) then ErrorMessage; Ini := TIniFile.Create(IniFileName); try List := TStringList.Create; try Ms := TMemoryStream.Create; try Ms.WriteComponent(Component); Ms.Position := 0; StreamToStrings(Ms, List); finally Ms.Free; end; IdentName := Ident + '_c'; Ini.WriteInteger(Section, IdentName, List.Count); for I := 0 to List.Count - 1 do begin IdentName := Ident + '_d' + IntToStr(I); Ini.WriteString(Section, IdentName, List[I]); end; finally List.Free; end; finally Ini.Free; end; end; procedure IniReadComponent(const IniFileName, Section, Ident: String; Component: TComponent); var Ini: TIniFile; List: TStringList; Ms: TMemoryStream; S, IdentName: String; I, C: Integer; begin if (Component = nil) or ((Component is TWinControl) and (TWinControl(Component).ControlCount > 0)) then ErrorMessage; Ini := TIniFile.Create(IniFileName); try List := TStringList.Create; try IdentName := Ident + '_c'; C := Ini.ReadInteger(Section, IdentName, 0); if C > 0 then begin for I := 0 to C - 1 do begin IdentName := Ident + '_d' + IntToStr(I); S := Ini.ReadString(Section, IdentName, ''); List.Add(S); end; Ms := TMemoryStream.Create; try StringsToStream(List, Ms); Ms.Position := 0; Ms.ReadComponent(Component); finally Ms.Free; end; end; finally List.Free; end; finally Ini.Free; end; end; //---------------------------------------------------------------------- // 使用例 const SectionName = 'Memo1'; IdentName = 'prop'; procedure TForm1.Button1Click(Sender: TObject); var S: String; Memo: TMemo; begin S := ChangeFileExt(Application.ExeName, '.ini'); Memo := TMemo.Create(Self); try Memo.Parent := Self; Memo.Top := 0; Memo.Left := 0; Memo.Color := clRed; Memo.Font.Name := 'MS 明朝'; Memo.Font.Size := 20; Memo.ScrollBars := ssBoth; Memo.Lines.Add('Ini ファイルに書き込まれた TMemo'); IniWriteComponent(S, SectionName, IdentName, Memo); finally Memo.Free; end; end; procedure TForm1.Button2Click(Sender: TObject); var S: String; Memo: TMemo; begin S := ChangeFileExt(Application.ExeName, '.ini'); Memo := TMemo.Create(Self); Memo.Parent := Self; IniReadComponent(S, SectionName, IdentName, Memo); end; 本田勝彦 Original document by 本田勝彦 氏 ID:(VYR01647)



ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum