순수하게 설정정보만 편하게 저장하고 싶을때 쓰면좋다..
델마당에서 얻었어요~^ㅁ^
================================================================
어플리케이션에서 사용하는 특정한 내용들은 보통 .INI 파일이나 레지스트리에 저장하는데
하다보면 무진장 귀찮습니다(귀차니즘 모드).
특히, 정수, 문자 등이 아닌 열거형이나 집합형을 읽고 쓰려면 더욱 귀찮지요.
그래서 델파이가 컴포넌트의 정보를 폼(DFM)에 저장되는 방식을 빌어서 좀 간편하게 사용할 수 있도록
클래스를 한번 만들어 봤습니다.
::아래 코드::
//
// Unit : dxConfigManager.pas
// Description : INI를 대신하는 유닛
// 누구든 자유롭게 사용하고 고쳐쓸 수 있습니다.
// Author : 안영제(civilian@korea.com)
// History
// 1.0 2005.07.11 처음 만듦
//
unit dxConfigManager;
interface
uses
Windows, SysUtils, Classes;
type
TCustomConfigManager = class(TComponent)
private
FFileName: String;
public
constructor Create(AOwner: TComponent); override;
procedure LoadFromFile;
procedure SaveToFile;
property FileName: String read FFileName write FFileName;
end;
implementation
{ TCustomConfigManager }
constructor TCustomConfigManager.Create(AOwner: TComponent);
begin
inherited;
FFileName := '';
end;
//
// 파일에서 읽어오기
//
procedure TCustomConfigManager.LoadFromFile;
var
Stream: TFileStream;
BinStream: TMemoryStream;
begin
if FileExists(FFileName) then
begin
Stream := TFileStream.Create(FFileName, fmOpenRead);
BinStream := TMemoryStream.Create;
try
ObjectTextToBinary(Stream, BinStream);
BinStream.Seek(0, soFromBeginning);
BinStream.ReadComponent(Self);
finally
Stream.Free;
BinStream.Free;
end;
end;
end;
//
// 파일에 저장
//
procedure TCustomConfigManager.SaveToFile;
var
Stream: TFileStream;
BinStream: TMemoryStream;
begin
Stream := TFileStream.Create(FFileName, fmCreate);
BinStream := TMemoryStream.Create;
try
BinStream.WriteComponent(Self);
BinStream.Seek(0, soFromBeginning);
ObjectBinaryToText(BinStream, Stream);
finally
Stream.Free;
BinStream.Free;
end;
end;
end.
::사용방법::
위 클래스를 어플리케이션에서 사용하려면 우선 저녀석으로 부터 상속받은 클래스를 하나 만들어야 합니다.
type
TMyConfig = class(TCustomConfigManager)
private
FServerIP: String;
FServerPort: String;
published
//
// 파일에 기록되는 것은 published 절에 있는 프로퍼티만 저장됩니다.
//
property ServerIP: String read FServerIP write FServerIP;
property ServerPort: String read FServerPort write FServerPort;
end;
자 이제 클래스를 만들었으니 사용하면 되겠지요.
::저장할 때::
MyConfig := TMyConfig.Create;
MyConfig.FileName := 'cL\temp\MyConfig.dat';
MyConfig.ServerIP := '10.0.0.1';
MyConfig.ServerPort := '8080';
MyConfig.SaveToFile;
MyConfig.Free;
::읽어올 때::
MyConfig := TMyConfig.Create;
MyConfig.FileName := 'cL\temp\MyConfig.dat';
MyConfig.LoadFromFile;
MyConfig.Free;
보통 유닛(Config.pas)에 넣어놓고 쓰는 것이 편리할 겁니다.
::저장된 파일의 구조::
델파이의 DFM이 TEXT 포맷으로 저장된 것과 동일합니다.
아래 샘플은 제가 만드는 어플리케이션의 설정값을 저장한 것입니다.
object TMyConfig
AudioOptions.Auto = True
AudioOptions.Device = -1
AudioOptions.Format = -1
FixedHeight = 0
FixedWidth = 0
CursorOptions.ActualCursor = False
CursorOptions.HighliteColor = clYellow
CursorOptions.HighliteCursor = False
CursorOptions.HighliteShape = hsCircle
CursorOptions.HighliteSize = 0
CursorOptions.LeftClickUse = False
CursorOptions.LeftClickShape = csRing
CursorOptions.LeftClickSize = 0
CursorOptions.RightClickUse = False
CursorOptions.RightClickShape = csRing
CursorOptions.RightClickSize = 0
NameOption = foAsk
ProgramOptions = []
RecordOptions = [ro4Times]
SoundOptions.MouseSoundUse = False
SoundOptions.MouseVolume = 0
SoundOptions.KeySoundUse = False
SoundOptions.KeyVolume = 0
VideoOptions.Auto = True
VideoOptions.Frame = 10
VideoOptions.KeyFrame = 1
end
================================================================