![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
запись в один .ini состояния MainForm и Form2
Garacio © (2007-01-14 10:30:00)Я сохраняю содержимое 2-х Edit на главной форме в save.ini. Как мне дополнить код, чтобы в этом-же save.ini сохранялись состояния CheckBox-ов на Form2? (и, соответственно, восстанавливались при запуске приложения)
Garacio (2007-01-14 11:12:00) [1]Благодарю, FAskerQ! Это тот ответ, который был нужен!
FAskerQ (2007-01-14 11:11:00) [2]
> [3] Garacio © (14.01.07 10:49)
То же не конца наверно понял
FAskerQ (2007-01-14 11:08:00) [3]Uses
Unit2;
procedure TForm2.FormCreate(Sender: TObject);
begin
Ini:= TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'save.ini');
//Текс в Edit1 на Form1
Form1.Edit1.Text := Ini .ReadString('tools', 'edit1', 'text');
//CheckBox1 на Form2
CheckBox1.Checked := Ini F.ReadBool('tools', 'check', true);
Ini .Destroy;
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
Ini := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'save.ini');
//Текс в Edit1 на Form1
Ini .WriteString('tools', 'edit1', Form1.Edit1.Text);
//CheckBox1 на Form2
Ini .WriteBool('tools', 'check', CheckBox1.Checked);
Ini .Free;
end;
Джо (2007-01-14 10:57:00) [4]> [3] Garacio © (14.01.07 10:49)
Не понял вопроса.
Garacio (2007-01-14 10:49:00) [5]Вот, за второй ответ, спасибо! Пригодится!
Я только немного уточню свой вопрос:
В Интернете гора примеров создания .ini (автосохранения), но почти все сохраняют положение окна приложения на рабочем столе. Я немного разобрался, теперь моя программа сохраняет
> содержимое 2-х Edit на главной форме в save.ini
но:
1. Как выглядят строки (сохранение/запись) для CheckBox-ов;
2. Как выглядят строки (сохранение/запись), допустим, содержимое тех-же Edit только с Form2?
Джо (2007-01-14 10:37:00) [6]Вот, когда-то для форума писал. Можешь доработать и пользоваться.
// Расширение класса TIniFile для записи/чтения объектов.
type
TExtendedIniFile = class (TIniFile)
public
procedure WriteObject (AObject: TObject; const Section: string);
procedure ReadObject (AObject: TObject; const Section: string);
end;
implementation
{$R *.dfm}
uses TypInfo;
type
TPropInfo = record
Name: string;
Kind: TTypeKind;
end;
TPropInfoArray = array of TPropInfo;
function GetPropertyNames (AClass: TClass): TPropInfoArray;
var
I,Cnt: Integer;
Pti: PTypeInfo;
PPropLst: PPropList;
begin
Pti := PTypeInfo(AClass.ClassInfo);
GetMem (PPropLst,High(TPropList));
try
Cnt := GetPropList(
Pti,
[tkInteger, tkChar, tkEnumeration, tkFloat, tkString,
tkSet, tkWChar, tkLString, tkWString,tkInt64],
PPropLst
);
SetLength(Result,Cnt);
for I := 0 to Cnt-1 do
begin
Result[I].Name := PPropLst^[I].Name;
Result[I].Kind := PPropLst^[I].PropType^.Kind;
end;
finally
FreeMem (PPropLst);
end;
end;
{ TExtendedIniFile }
procedure TExtendedIniFile.ReadObject(AObject: TObject; const Section: string);
var
I: Integer;
Props: TPropInfoArray;
V: Variant;
begin
Props := GetPropertyNames(AObject.ClassType);
for I := 0 to High(Props) do
begin
case Props[I].Kind of
tkInteger,tkInt64:
SetOrdProp(AObject,Props[I].Name,ReadInteger(Section,Props[I].Name,0));
tkString,tkLString,tkWString,tkChar,tkWChar:
SetStrProp(AObject,Props[I].Name,ReadString(Section,Props[I].Name,''));
tkFloat:
SetFloatProp(AObject,Props[I].Name,ReadFloat(Section,Props[I].Name,0));
tkEnumeration:
SetEnumProp(AObject,Props[I].Name,ReadString(Section,Props[I].Name,''));
tkSet:
SetSetProp(
AObject,
Props[I].Name,
ReadString (Section,Props[I].Name,'')
);
end;
end;
end;
procedure TExtendedIniFile.WriteObject(AObject: TObject; const Section: string);
var
I: Integer;
Props: TPropInfoArray;
V: Variant;
begin
Props := GetPropertyNames(AObject.ClassType);
for I := 0 to High(Props) do
begin
V := GetPropValue(AObject,Props[I].Name);
case Props[I].Kind of
tkInteger,tkInt64:
WriteInteger (Section,Props[I].Name,V);
tkString,tkLString,tkWString,tkChar,tkWChar:
WriteString (Section,Props[I].Name,V);
tkFloat:
WriteFloat (Section,Props[I].Name,V);
tkEnumeration:
WriteString (Section,Props[I].Name,
GetEnumProp(AObject,Props[I].Name));
tkSet:
begin
WriteString(
Section,
Props[I].Name,
GetSetProp(AObject,Props[I].Name,True)
);
end;
else
begin
// Этого не должно случиться
// ибо в GetPropertyNames мы фильтруем
// только нужные нам типы свойств
Assert (False,'Unsupported property type');
end;
end;
end;
end;
Использование.
// запись объекта
var
Ini: TExtendedIniFile;
begin
Ini := TExtendedIniFile.Create('d:\form.ini');
try
// Сохраняем, что угодно
Ini.WriteObject(Self,'Form');
Ini.WriteObject(Font,'Font');
finally
Ini.Free;
end;
end;
// чтение объекта
var
Ini: TExtendedIniFile;
begin
Ini := TExtendedIniFile.Create('d:\form.ini');
try
Ini.ReadObject(Font,'Font');
Ini.ReadObject(Self,'Form');
finally
Ini.Free;
end;
end;
Джо (2007-01-14 10:31:00) [7]Ну, пиши в разные секции.