Solved

Saving TFont object in registry

Posted on 1998-06-20
2
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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