如何保存和恢复表单

如何保存和恢复表单

本文介绍了如何保存和恢复表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个十几个控件的表单,有人想保存,然后恢复其内容和设置 - 选中哪个单选按钮,上/下的位置等。

So, I have a form with a few dozen controls and someone would like to save and later restore their contents and settings - which radio button was selected, what was the Position of that up/down, etc.

我还想在运行时将任何添加到列表框中的条目存储起来。

I would also like to store any entries added to a list box at run time.

最简单的方法是什么? DfmToString和反向?写/读一个.INI?其他的东西?

What's the simplest way to do it? DfmToString and reverse? Write/read a .INI? Something else?

推荐答案

PRUZ的解决方案是一个现成的解决方案; JVCL是开源的,使用JvFormStorage很简单。但是您也可以使用Delphi自己的流式传输机制,而不使用任何第三方组件。这是一个例子:

PRUZ's solution is a ready made solution; JVCL is open-source, and using JvFormStorage is simple. But you can also use Delphi's own streaming mechanism without using any third-party components. Here is an example:

procedure SaveComponentToFile(Component: TComponent; const FileName: TFileName);
var
  FileStream : TFileStream;
  MemStream : TMemoryStream;
begin
  MemStream := nil;

  if not Assigned(Component) then
    raise Exception.Create('Component is not assigned');

  FileStream := TFileStream.Create(FileName,fmCreate);
  try
    MemStream := TMemoryStream.Create;
    MemStream.WriteComponent(Component);
    MemStream.Position := 0;
    ObjectBinaryToText(MemStream, FileStream);
  finally
    MemStream.Free;
    FileStream.Free;
  end;
end;

SaveComponentToFile接受一个组件实例,加上一个文件名,并将组件流入文件,可读的文本。

SaveComponentToFile takes a component instance, plus a file name, and streams the component into the file, in a human-readable text.

要从文件加载组件,可以使用如下代码:

To load the component from file, you can use a code like this:

procedure LoadComponentFromFile(Component: TComponent; const FileName: TFileName);
var
  FileStream : TFileStream;
  MemStream : TMemoryStream;
  i: Integer;
begin
  MemStream := nil;

  if not Assigned(Component) then
    raise Exception.Create('Component is not assigned');

  if FileExists(FileName) then
  begin
    FileStream := TFileStream.Create(FileName,fmOpenRead);
    try
      for i := Component.ComponentCount - 1 downto 0 do
      begin
        if Component.Components[i] is TControl then
          TControl(Component.Components[i]).Parent := nil;
        Component.Components[i].Free;
      end;

      MemStream := TMemoryStream.Create;
      ObjectTextToBinary(FileStream, MemStream);
      MemStream.Position := 0;
      MemStream.ReadComponent(Component);
      Application.InsertComponent(Component);
    finally
      MemStream.Free;
      FileStream.Free;
    end;
  end;
end;

LoadComponentFromFile接受组件实例和文件名,然后将文件内容加载到组件实例中。为了避免命名冲突,我们可以在将文件数据加载到其中之前释放实例的所有现有组件。

LoadComponentFromFile takes a component instance, and a file name, then loads file content into the component instance. To avoid naming conflict, we are free all existing owned components of the instance, before loading file data into it.

现在可以使用上述代码将表单保存到一个文件:

Now you can use the above code for saving a form into a file:

  SaveComponentToFile(FSecondForm,ExtractFilePath(Application.ExeName)+ 'formdata.txt');

FSecondForm是一个表单实例,它将被保存在同一个formdata.txt文件中文件夹作为EXE文件。

FSecondForm is a form instance, and it will be saved into "formdata.txt" file inside the same folder as the EXE file.

要从formdata.txt文件加载FSecondForm,我们写下来:

And to load FSecondForm from "formdata.txt" file, we write this:

  if not Assigned(FSecondForm) then
    FSecondForm := TfrmSecond.Create(Application);
  LoadComponentFromFile(FSecondForm,ExtractFilePath(Application.ExeName)+ 'formdata.txt');
  FSecondForm.Show;

LoadComponentFromFile需要先创建实例,所以我们检查是否分配了FSecondForm,如果不是,创建它的一个实例(它是TfrmSecond类的一个实例),然后将文件数据加载到它中。最后,我们显示加载的表单。

LoadComponentFromFile needs the instance to be created first, so we check if FSecondForm is assigned, if not, we create an instance of it (it is an instance of TfrmSecond class), and then load file data into it. And eventually, we show the loaded form.

这篇关于如何保存和恢复表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:29