DELPHI AREA
MESSAGE BOARD
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Save and restore Component Properties form a file!

 
   Reply to topic    DELPHI AREA Forum Index -> Delphi Programming
View previous topic :: View next topic  
Author Message
hadipardis
Member


Joined: 17 Jul 2004
Posts: 11

PostPosted: 17/07/04 05:47    Post subject: Save and restore Component Properties form a file! Reply with quote

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
Back to top
View user's profile
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 386
Location: Tehran, Iran

PostPosted: 17/07/04 06:12    Post subject: Reply with quote

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.
Back to top
View user's profile Send e-mail Visit poster's website
hadipardis
Member


Joined: 17 Jul 2004
Posts: 11

PostPosted: 18/07/04 05:28    Post subject: Reply to Save and read component properties! Reply with quote

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?
Back to top
View user's profile
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 386
Location: Tehran, Iran

PostPosted: 18/07/04 07:30    Post subject: Reply with quote

Code:
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:
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;
Back to top
View user's profile Send e-mail Visit poster's website
hadipardis
Member


Joined: 17 Jul 2004
Posts: 11

PostPosted: 18/07/04 10:59    Post subject: Reply with quote

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
Back to top
View user's profile
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 386
Location: Tehran, Iran

PostPosted: 18/07/04 15:54    Post subject: Reply with quote

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.
Back to top
View user's profile Send e-mail Visit poster's website
hadipardis
Member


Joined: 17 Jul 2004
Posts: 11

PostPosted: 19/07/04 05:12    Post subject: Reply with quote

Thank you.But can you give me an example of using LoadComponentFromStream Procedure?
cheers
Back to top
View user's profile
hadipardis
Member


Joined: 17 Jul 2004
Posts: 11

PostPosted: 19/07/04 05:50    Post subject: Reply with quote

Dear Kambiz
I have been written this code:
Code:
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:
Back to top
View user's profile
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 386
Location: Tehran, Iran

PostPosted: 19/07/04 08:07    Post subject: Reply with quote

Why you copy the file stream into a memory stream. Directly use the file stream.
Back to top
View user's profile Send e-mail Visit poster's website
Johnny_Bit
Moderator


Joined: 15 Jun 2003
Posts: 145

PostPosted: 19/07/04 12:28    Post subject: Reply with quote

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.
Back to top
View user's profile
Display posts from previous:   
   Reply to topic    DELPHI AREA Forum Index -> Delphi Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB 2.0.6 © 2001, 2002 phpBB Group