| View previous topic :: View next topic |
| Author |
Message |
hadipardis Member
Joined: 17 Jul 2004 Posts: 12
|
Posted: 17/07/04 05:47 Post subject: Save and restore Component Properties form a file! |
|
|
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 |
|
 |
Kambiz Administrator

Joined: 07 Mar 2003 Posts: 409 Location: Tehran, Iran
|
Posted: 17/07/04 06:12 Post subject: |
|
|
| 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 |
|
 |
hadipardis Member
Joined: 17 Jul 2004 Posts: 12
|
Posted: 18/07/04 05:28 Post subject: Reply to Save and read component properties! |
|
|
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 |
|
 |
Kambiz Administrator

Joined: 07 Mar 2003 Posts: 409 Location: Tehran, Iran
|
Posted: 18/07/04 07:30 Post subject: |
|
|
| 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 |
|
 |
hadipardis Member
Joined: 17 Jul 2004 Posts: 12
|
Posted: 18/07/04 10:59 Post subject: |
|
|
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 |
|
 |
Kambiz Administrator

Joined: 07 Mar 2003 Posts: 409 Location: Tehran, Iran
|
Posted: 18/07/04 15:54 Post subject: |
|
|
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 |
|
 |
hadipardis Member
Joined: 17 Jul 2004 Posts: 12
|
Posted: 19/07/04 05:12 Post subject: |
|
|
Thank you.But can you give me an example of using LoadComponentFromStream Procedure?
cheers |
|
| Back to top |
|
 |
hadipardis Member
Joined: 17 Jul 2004 Posts: 12
|
Posted: 19/07/04 05:50 Post subject: |
|
|
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 |
|
| Back to top |
|
 |
Kambiz Administrator

Joined: 07 Mar 2003 Posts: 409 Location: Tehran, Iran
|
Posted: 19/07/04 08:07 Post subject: |
|
|
| Why you copy the file stream into a memory stream. Directly use the file stream. |
|
| Back to top |
|
 |
Johnny_Bit Moderator
Joined: 15 Jun 2003 Posts: 154
|
Posted: 19/07/04 12:28 Post subject: |
|
|
| 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 |
|
 |
|