Author: Tomas Rutkauskas
How can I save the font settings of a control to registry? Saving name , size, etc.
as string/int doesn't seem the best way ... (as far as I remember , I can't even
save all font options this way)
Answer:
You can create a little component (needs not be installed on the palette) that
allows you to stream a fonts properties to a stream. The stream contents could then
be saved to a binary key in the registry.
1 { ... }2 type3 TFontWrapper = class(TComponent)
4 private5 FFont: TFont;
6 constructor Create(aOwner: TComponent); override;
7 destructor Destroy; override;
8 procedure SetFont(value: TFont);
9 published10 property Font: TFont read FFont write SetFont;
11 end;
12 13 { TFontWrapper }14 15 constructor TFontWrapper.Create(aOwner: TComponent);
16 begin17 inherited;
18 FFont := TFont.Create;
19 end;
20 21 destructor TFontWrapper.Destroy;
22 begin23 FFont.Free;
24 inherited;
25 end;
26 27 procedure TFontWrapper.SetFont(value: TFont);
28 begin29 FFont.Assign(value);
30 end;
31 32 { ms is a field of the form }33 34 procedure TForm1.Button1Click(Sender: TObject);
35 var36 helper: TFontWrapper;
37 begin38 ifnot Assigned(ms) then39 ms := TMemoryStream.Create
40 else41 ms.Clear;
42 helper := TFontWrapper.Create(nil);
43 try44 helper.font := label1.font;
45 ms.WriteComponent(helper);
46 finally47 helper.free;
48 end;
49 label1.font.size := label1.font.size + 2;
50 end;
51 52 procedure TForm1.Button2Click(Sender: TObject);
53 var54 helper: TFontWrapper;
55 begin56 ifnot Assigned(ms) then57 Exit;
58 ms.Position := 0;
59 helper := TFontWrapper.Create(nil);
60 try61 ms.ReadComponent(helper);
62 label1.font := helper.font;
63 finally64 helper.free;
65 end;
66 end;
67 68 69 //If reg is a TRegistry instance already with key open a70 71 72 reg.WriteBinaryData(valuename, ms.Memory^, ms.Size);
73 74 75 //would save the streamed data to the registry,76 77 78 ms.size := reg.GetDatasize(valuename);
79 reg.ReadBinaryData(valuename, ms.Memory^, ms.Size);
would read it back. Mind the caret!