Save and restore Component Properties form a file!

Please discuss general Delphi programming topics here.

Save and restore Component Properties form a file!

Postby hadipardis » July 17th, 2004, 5:47 am

Hi
I was saved some components such as TEdit or TImage to a file by using ObjectBinaryToText function and get a file which contains
something such as follow:

object Edit1: TEdit
Left = 368
Top = 80
Width = 281
Height = 27
Cursor = crHandPoint
Hint = 'alireza'
BevelEdges = [beLeft, beRight]
BevelKind = bkTile
BorderStyle = bsNone
Color = 4784127
Font.Charset = ARABIC_CHARSET
Font.Color = clRed
Font.Height = -16
Font.Name = 'MS Serif'
Font.Style = [fsBold, fsItalic]
ParentFont = False
ParentShowHint = False
ShowHint = True
TabOrder = 1
Text = 'something'
end
Now, I want to reload this file in a new application and create relative components dynamically and then set the true
properties of each component.I write a program that parses this file line by line and sets the relative properties by some
instructions such as:
GetPropinfo ,SetOrdProp,SetEnumProp and etc.
*But there are some problems in my project.for example I read the value of cursor property (crHandPoint) and assign it but
It is not work properly .If I exchange crHandPoint by -21 it is works.The same problem is exists for Color property and
also I have problem in reading Picture Properties and Font properties.(see above).

Now ,Can anyone help me to overcome this problems?
Note that I was tried to use the ObjectTextToBinary function but it is not work properly.
I look forward to hearing form you.
Cheers
Hadi Hadizadeh
hadipardis
Junior Member
Junior Member
 
Posts: 33
Joined: July 17th, 2004, 5:45 am

Postby Kambiz » July 17th, 2004, 6:12 am

there was no need to write your own parser. In the same way you save a component in to a stream, you can load it from that.
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Reply to Save and read component properties!

Postby hadipardis » July 18th, 2004, 5:28 am

Thanks.But there is a problem with saving Images to a text file because
the size of it will become very big and also when we try to reload image form file by using TStream.ReadComponent we get an except ("Line 1 is too long!").Now can any one help me how to save Image properties to a textfile using TStream.WriteComponent or ObjectBinaryToText and vice versa Tstream.ReadComponent or ObjectTextToBinary?

And also if we want to save object properties to a RES file (note that some components must be saved) how we can do it?
hadipardis
Junior Member
Junior Member
 
Posts: 33
Joined: July 17th, 2004, 5:45 am

Postby Kambiz » July 18th, 2004, 7:30 am

Code: Select all
procedure SaveComponentToStream(Component: TComponent; Stream: TStream;
  AsText: Boolean);
var
  AuxStream: TStream;
begin
  if AsText then
  begin
    AuxStream := TMemoryStream.Create;
    try
      AuxStream.WriteComponent(Component);
      AuxStream.Seek(0, soFromBeginning);
      ObjectBinaryToText(AuxStream, Stream);
    finally
      AuxStream.Free;
    end;
  end
  else
    Stream.WriteComponent(Component);
end;

Code: Select all
procedure LoadComponentFromStream(Component: TComponent; Stream: TStream);
var
  AuxStream: TStream;
begin
  case TestStreamFormat(Stream) of
    sofText:
    begin
      AuxStream := TMemoryStream.Create;
      try
        ObjectTextToBinary(Stream, AuxStream);
        AuxStream.Seek(0, soFromBeginning);
        AuxStream.ReadComponent(Component);
      finally
        AuxStream.Free;
      end;
    end;
    sofBinary:
      Stream.ReadComponent(Component);
  end;
end;
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Postby hadipardis » July 18th, 2004, 10:59 am

Thanks.I have written procedures such as your procedures which you suggest.But when I try to restore the properties
of an image I get an EParser except.Now,Can you tell me how I can use your LoadComponentFromStream(Component: TComponent; Stream: TStream) procedure?

Note that the properties of objects have been written in a text file such as Bob.txt
Now how I can pass them to the Stream argument.I was tried to use TFileStream but It is not give a good result.

Please Help me.
Cheers
hadipardis
Junior Member
Junior Member
 
Posts: 33
Joined: July 17th, 2004, 5:45 am

Postby Kambiz » July 18th, 2004, 3:54 pm

I'm using the above procedures in a couple of my applications, and never had problem with images.

TStream is ancestor of the all stream objects. So, the TFileStream is also a valid parameter type to pass to the procedures.
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Postby hadipardis » July 19th, 2004, 5:12 am

Thank you.But can you give me an example of using LoadComponentFromStream Procedure?
cheers
hadipardis
Junior Member
Junior Member
 
Posts: 33
Joined: July 17th, 2004, 5:45 am

Postby hadipardis » July 19th, 2004, 5:50 am

Dear Kambiz
I have been written this code:
Code: Select all
save procedure:
***************
var st:TFileStream;
begin
st:=TFileStream.Create('c:\ali.txt',fmcreate);
SaveComponentToStream(image1,st,true);
SaveComponentToStream(button1,st,true);
st.Free;
end;
**************
load procedure
**************
var fp:TFilestream;
st:TStream;
ali:TComponent;
b:TMemoryStream;
begin
 b:=TmemoryStream.Create;
 ali:=TImage.Create(self);
 fp:=TFileStream.Create('c:\ali.txt',fmOpenRead);
 b.LoadFromStream(fp);
 LoadComponentFromStream(ali,b);
 fp.Free;
 b.Free;
end;
*****************************
and also I changed the LoadComponentFromStream procedure as bellow:
procedure TForm1.LoadComponentFromStream(Component: TComponent; Stream: TStream);
var
  AuxStream: TStream;
begin
  case TestStreamFormat(Stream) of
    sofText:
    begin
      AuxStream := TMemoryStream.Create;
      try
        ObjectTextToBinary(Stream, AuxStream);
        AuxStream.Seek(0, soFromBeginning);
        AuxStream.ReadComponent(Component);
      finally
        AuxStream.Free;
      end;
    end;
    sofBinary:
      Stream.ReadComponent(Component);
     
  end;//change is here:
  with component as TComponent do
  begin
    parent:=form1;
  end;
end;
***************************************
but by this code I can restore only one component!
Can you help me?
thanks
One
Code: Select all
hadipardis
Junior Member
Junior Member
 
Posts: 33
Joined: July 17th, 2004, 5:45 am

Postby Kambiz » July 19th, 2004, 8:07 am

Why you copy the file stream into a memory stream. Directly use the file stream.
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Postby Johnny_Bit » July 19th, 2004, 12:28 pm

One of my buddies from work is currently working on component to do such things lot easier. I don't have contact with him, but you can ask him: detox@xlan.pl, ask about class manager and tell that I told you about this.
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron