Link to home
Start Free TrialLog in
Avatar of kwang080897
kwang080897

asked on

Saving a TFont

How do i save a Tfont to a file and load it again

i'm trying with
  Stream.Write(Font,Sizeof(font));

Why does Size return 4 ??? (Some kind of pointer ??)


Avatar of kwang080897
kwang080897

ASKER

I need to save and load a lot of different components - so if there is any easy way to do this I would like to know
Adjusted points to 100
Avatar of simonet
What exactly do you want to achieve, kwang?

Alex
hi kwang,

you can do it with the internal delphi streaming methods

a sample how to save and load a font

unit stream_font_u;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    FontDialog1: TFontDialog;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  If FontDialog1.Execute then
    Memo1.Font.Assign(FontDialog1.Font);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  FS : TFileStream;
begin
  if opendialog1.Execute then
  begin
    FS := TFilestream.Create(Opendialog1.Filename,fmOpenread);
    try
      TComponent(FontDialog1) := FS.ReadComponent(FontDialog1);
      Memo1.Font.Assign(FontDialog1.Font);
    finally
      FS.Free;
    end;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  FS : TFileStream;
begin
  if savedialog1.Execute then
  begin
    FS := TFilestream.Create(Savedialog1.Filename,fmCreate);
    try
      FontDialog1.Font.Assign(Memo1.Font);
      FS.WriteComponent(FontDialog1);
    finally
      FS.Free;
    end;
  end;
end;

end.


the streaming methods can only applied to Tcomponents. Tfont is no tcomponentclass, therefore i save and load here the component tfontdialog.

this streaming methods can applied to all components.

meikl
I'm doing a Print Project......

and i would like to save my objects whith all the properties included

For eks. I have a Property :

PrinterFont : Tfont

It seem's like i can save it ok - but i Get an error when i try to load it
(this case with SaveComponentRes and LoadComponent)

So what i'm really looking for is a Method to Enumerate all components in my TPrintDoc object and save them...... and be able to load them again




Hi,
I suggest you get the rxLib and use the formstorage placement
component. It will do all the work for you.
The only way I found to save the fonts state is to save everything in the
registry or elsewhere, but by hand. This means you have
to save every property one after another, the size, the color, the name, the style
and even the style is to handle by hand, bold, underlined, etc...
So use the rxlib. Anyway, with that component it's a matter of two clicks.
Get it here : http://rx.demo.ru/
if you have problems with the install let me know. If you prefer I send you
a code sample of how I store and restore my font propertys, let me know too.

Oh, Maybe I'm wrong and I did not understand your question, you want
to save the font in a file, or the font propertys ?
Hi  meikl !!

Thanks.......

I will keep this open, because i would like to take this question a little futher..

A will post a seperate question to you - hope it's ok (please look at my other comment)


Kim
hi kim,

well its ok.

the rx-library, suggested by jeurk, has a Component (TPlacement i guess), with which you can store some propertys into a ini-file or in the registry.

you should take a look to this great library.

meikl ;-)
Yup, TFormStorage.
Not just what i'm looking for.

In the end i should have some kind of file format, so i wont have the data in the registry, and i dont care about the placement of the form

Well maybe it is ......

How do i add Stored Props at runtime ??
Found out........

Think i can use it

 Jeurk !!! - Thanks - please submit an answer

There are many diffrent ways to save components, but you have always to save their properties and not the whole object instance. You can save it as stream, as text, as XML or how you want to. In the load procedure you have just to set the properties to the value they had before.

Cheers, ptm.
ASKER CERTIFIED SOLUTION
Avatar of jeurk
jeurk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial