Solved

Saving TFont object in registry

Posted on 1998-06-20
2
360 Views
Last Modified: 2011-09-20
I want to save a TFont object as binary data in registry. I don't want to separately store its style, name, etc. Instead, if its inheritance from TPersistent can help in some way through TMemoryStream, perhaps, it would be great. This TFont object is changed based on user's selection and hence I want to remember the user's preference.

Any idea how to do it in a clean way?

0
Comment
Question by:skanade
2 Comments
 
LVL 3

Accepted Solution

by:
vladika earned 50 total points
ID: 1354610
Create wrapper class as
type
  TWrapper = class(TComponent)
  private
    FItem: TPersistent;
  published
    property Item: TPersistent read FItem write FItem;
  end;

Now you can store any object inherited from TPersistent.
For example
  Wrapper.Item := Form.Font;
  Stream.WriteComponent(Wrapper);
or
  Wrapper.Item := Form.Font;
  Stream.ReadComponent(Wrapper);

Here is full example
type
  TForm1 = class(TForm)
    WriteFontBtn: TButton;
    ReadFontBtn: TButton;
    procedure WriteFontBtnClick(Sender: TObject);
    procedure ReadFontBtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TWrapper = class(TComponent)
  private
    FItem: TPersistent;
  published
    property Item: TPersistent read FItem write FItem;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const RegPath = 'Software\Test\Properties';

procedure TForm1.WriteFontBtnClick(Sender: TObject);
var Stream: TMemoryStream;
    Wrapper: TWrapper;
    Reg: TRegistry;
    Memory: Pointer;
begin
  Wrapper := TWrapper.Create(nil);
  try
    Stream := TMemoryStream.Create;
    try
      Wrapper.Item := Font;  // set reference on Form1.Font
      Stream.WriteComponent(Wrapper); // write Form1.Font into Stream
      Reg := TRegistry.Create;
      try
        Reg.OpenKey(RegPath, True);
        Memory := Stream.Memory;
        Reg.WriteBinaryData('Font', Memory^, Stream.Size);  // write Form1.Font into registry
      finally
        Reg.Free;
      end;
    finally
      Stream.Free;
    end;
  finally
    Wrapper.Free;
  end;
end;

procedure TForm1.ReadFontBtnClick(Sender: TObject);
var Stream: TMemoryStream;
    Wrapper: TWrapper;
    Reg: TRegistry;
    Memory: Pointer;
begin
  Wrapper := TWrapper.Create(nil);
  try
    Stream := TMemoryStream.Create;
    try
      Reg := TRegistry.Create;
      try
        Reg.OpenKey(RegPath, True);
        Stream.Size := Reg.GetDataSize('Font');  // get binary data size
        Memory := Stream.Memory;
        Reg.ReadBinaryData('Font', Memory^, Stream.Size); // read binary data into Stream
      finally
        Reg.Free;
      end;
      Wrapper.Item := Font;  // set reference on Form1.Font
      Stream.ReadComponent(Wrapper); // read Form1.Font
    finally
      Stream.Free;
    end;
  finally
    Wrapper.Free;
  end;
end;


But WHY do you not want to separately store font properties?
0
 

Author Comment

by:skanade
ID: 1354611
>>But WHY do you not want to separately store font properties?

My application allows the user to select a font for printing out plain text file reports. I want to remember the font in the registry. I am already remembering many user preferences in the registry and have avoided the use of an INI file as per the new standards.

Thanks!
Sanjay
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now