关于流文件及控件的创建!!! (100分)

  • 主题发起人 主题发起人 lcq2000
  • 开始时间 开始时间
L

lcq2000

Unregistered / Unconfirmed
GUEST, unregistred user!
想创建一些控件,然后放入流文件中,然后用时在取出!!!
 
什么意思,讲清处点儿。
 
请看:
type
Tmyobj = class(Tedit)
var
myobj:tmyobj;
procedure Tform1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
myobj:=classref.Create(self);
myobj.Parent:=self;

myobj.Left:=x;
myobj.Top:=y;
myobj.visible:=true;
end;

代码是这样的,当程序运行时,可创建edit的很多实例,但程序退出时
实例丢失,我想用流来存,在下次程序运行时,添加流文件,也就是以
前的实例。请诸位帮忙!!!
 
用配置文件(.txt类型)保存也可以达到同样的目的,而且方法简单一些。你觉得呢?
 
请mat举一例子,thank you!!!
 
如果你读过李维先生的书,应该可以找到这个例子:www.inprises.com 上有这本书的源码。
安装以后默认在:C:/Program Files/MDelphi4(2)/Chap09/DynamicCheckBox

unit fDynamicCheckBoxMain;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons;

type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
btnCreateCheckBoxes: TBitBtn;
btnSaveCheckBoxes: TBitBtn;
btnloadCheckBoxes: TBitBtn;
btnOK: TBitBtn;
procedure btnOKClick(Sender: TObject);
procedure btnCreateCheckBoxesClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnSaveCheckBoxesClick(Sender: TObject);
procedure btnloadCheckBoxesClick(Sender: TObject);
private
{ Private declarations }
aList : TList;

procedure DeleteCheckBoxes;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.btnOKClick(Sender: TObject);
begin
Close;
end;

procedure TForm1.btnCreateCheckBoxesClick(Sender: TObject);
const
OFFSET : Integer = 40;
var
iCount, yPos : Integer;
aCheckBox : TCheckBox;
begin
yPos := 40;
for iCount := 1 to 5 do
begin
aCheckBox := TCheckBox.Create(Self);
aCheckBox.Parent := GroupBox1;
aCheckBox.Left := 9;
aCheckBox.Top := yPos;
aCheckBox.Width := 300;
aCheckBox.Caption := 'CheckBox北?じン' + IntToStr(iCount);
yPos := yPos + OFFSET;
aList.Add(aCheckBox);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
aList := Tlist.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
DeleteCheckBoxes;
aList.Free;
end;

procedure TForm1.btnSaveCheckBoxesClick(Sender: TObject);
var
fSaveStream : TMemoryStream;
iCount : Integer;
aCheckBox : TCheckBox;
begin
try
fSaveStream := TMemoryStream.Create;
for iCount := 0 to aList.Count - 1 do
begin
aCheckBox := aList.Items[iCount];
fSaveStream.WriteComponent(aCheckBox);
end;
fSaveStream.SaveToFile('sCksbo9.cps');
finally
fSaveStream.Free;
end;
end;

procedure TForm1.btnloadCheckBoxesClick(Sender: TObject);
var
fSaveStream : TMemoryStream;
iCount : Integer;
aCheckBox : TCheckBox;
begin
DeleteCheckBoxes;

Classes.RegisterClasses([TCheckBox]);
try
fSaveStream := TMemoryStream.Create;
fSaveStream.LoadFromFile('sCksbo9.cps');

for iCount := 0 to 4 do
begin
aCheckBox := fSaveStream.ReadComponent(nil) as TCheckBox;
aCheckBox.Parent := GroupBox1;
aCheckBox.Left := 90;
aCheckBox.Font.Color := clRed;
aList.Add(aCheckBox);
end;
finally
fSaveStream.Free;
end;
end;

procedure TForm1.DeleteCheckBoxes;
var
iCount : Integer;
aCheckBox : TCheckBox;
begin
for iCount := aList.Count - 1 downto 0 do
begin
aCheckBox := aList.Items[iCount];
aCheckBox.Free;
aList.Delete(iCount);
end;
end;

end.
 
不如放配置文件,下次都可以用
 
lcq2000:如果你还要继续讨论请定期提前你的帖子,如果不想继续讨论请结束帖子。
 
多人接受答案了。
 
后退
顶部