Mega Code Archive

 
Categories / Delphi / Examples
 

Automatically save - load user settings

private procedure SaveConfig(fn: string); procedure LoadConfig(fn: string); end; implementation {....} procedure TForm1.SaveConfig(fn: string); // fn: Filename where to save the stream { Saves all TWinControl-descendants of all Pages of a PageControl in a stream can easy be used to save userdefined Options Place a new control to the PageControl and it will be saved to the stream, without any additional codeline Speichert alle Abkömmlinge von TWinControl auf allen Seiten eines PageControls in ein File Erleichtert das Speichern von benutzerdefinierten Optionen Einfach ein neues Control auf das PageControl setzen und es wird ohne zusätzlichen Code gespeichert } var Stream: TFileStream; i, j: Integer; ObjName: string[255]; begin Stream := TFileStream.Create(fn, fmCreate); try Stream.Position := 0; // Walk throug every Control on every Page of a PageControl // Alle Controls auf allen Pages eines PageControls durchlaufen for j := 0 to PageControl.PageCount - 1 do begin for i := 0 to PageControl.Pages[j].ControlCount - 1 do begin if PageControl.Pages[j].Controls[i] is TWinControl then begin // If the control is a descendant of TWinControl, then save it in the // stream incl. name and length // Wenn das Control von TWinControl abstammt, dann mit Namen und Länge // in den Stream speichern ObjName := PageControl.Pages[j].Controls[i].Name; Stream.WriteBuffer(ObjName, Length(ObjName) + 1); Stream.WriteComponent(PageControl.Pages[j].Controls[i]); end; end; end; finally Stream.Free; end; end; procedure TForm1.LoadConfig(fn: string); // fn: Filename from where to load the stream // Loads the controls back from the stream // Lädt aus dem gespeicherten Stream den Zustand der Controls zurück var Stream: TFileStream; ObjName: string[255]; ObjNameLen: Byte; i, j: Integer; begin Stream := TFileStream.Create(fn, fmOpenRead or fmShareDenyNone); try Stream.Position := 0; // walk through the stream // durch den ganzen Stream gehen while Stream.Position do begin try // get objectname // Objektname holen Stream.Read(ObjName[0], 1); ObjNameLen := Byte(ObjName[0]); Stream.Read(ObjName[1], ObjNameLen); finally end; // Search the control on the PageControl // Das Control auf dem PageControl suchen for j := 0 to PageControl.PageCount - 1 do begin for i := 0 to PageControl.Pages[j].ControlCount - 1 do begin if PageControl.Pages[j].Controls[i] is TWinControl then begin if ObjName = PageControl.Pages[j].Controls[i].Name then begin try if PageControl.Pages[j].Controls[i] is TCheckbox then // TCheckbox has to be set to False // TCheckbox muss zuerst auf False gesetzt werden (PageControl.Pages[j].Controls[i] as TCheckbox).Checked := False; // load control // Control laden Stream.ReadComponent(PageControl.Pages[j].Controls[i]); finally end; end; end; end; end; end; finally Stream.Free; end; end; { Save} procedure TForm1.Button2Click(Sender: TObject); begin SaveConfig('pagecontrol.dat'); end; { Load} procedure TForm1.Button1Click(Sender: TObject); begin LoadConfig('pagecontrol.dat'); end;