this slowpoke moves

Save all Components

Dieses Beispiel speichert und ladet alle in einem PageControl befindlichen Komponenten. Das kann langwierige Ini-Dateien, die zum Speichern von Programmdaten benutzt werden, ersparen.
uses ComCtrls, Spin

private
    { Private declarations }
    procedure SaveConfig(fn: string);
    procedure LoadConfig(fn: string);
    
//

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
}
    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
        for j := 0 to PageControl1.PageCount - 1 do
        begin
          for i := 0 to PageControl1.Pages[j].ControlCount - 1 do
          begin
            if PageControl1.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
              ObjName := PageControl1.Pages[j].Controls[i].Name;
              Stream.WriteBuffer(ObjName, Length(ObjName) + 1);
              Stream.WriteComponent(PageControl1.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

    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
        while Stream.Position >= 0  do
        begin
          try
            // get objectname
            Stream.Read(ObjName[0], 1);
            ObjNameLen := Byte(ObjName[0]);
            Stream.Read(ObjName[1], ObjNameLen);
          finally
          end;
          // Search the control on the PageControl
          for j := 0 to PageControl1.PageCount - 1 do
          begin
            for i := 0 to PageControl1.Pages[j].ControlCount - 1 do
            begin
              if PageControl1.Pages[j].Controls[i] is TWinControl then
              begin
                if ObjName = PageControl1.Pages[j].Controls[i].Name then
                begin
                  try
                    if PageControl1.Pages[j].Controls[i] is TCheckbox then
                      // TCheckbox has to be set to False
                      (PageControl1.Pages[j].Controls[i] as TCheckbox).Checked := False;
                    // load control
                    Stream.ReadComponent(PageControl1.Pages[j].Controls[i]);
                  finally
                  end;
                end;
              end;
            end;
          end;
        end;
      except
        Stream.Free;
      end;
    end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SaveConfig('pagecontrol.dat');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  LoadConfig('pagecontrol.dat');
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate