Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to save and retrieve font information to / from a TIniFile Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
12-Oct-02
Category
Graphic
Language
Delphi 2.x
Views
139
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How to save and retrieve font information to / from a TIniFile

Answer:

Solve 1:

1   uses
2     Inifiles;
3   
4   procedure SaveFont(FName: string; Section: string; smFont: TFont);
5   var
6     FStream: TIniFile;
7   begin
8     FStream := TIniFile.Create(FName);
9     try
10      FStream.WriteString(Section, 'Name', smFont.Name);
11      FStream.WriteInteger(Section, 'CharSet', smFont.CharSet);
12      FStream.WriteInteger(Section, 'Color', smFont.Color);
13      FStream.WriteInteger(Section, 'Size', smFont.Size);
14      FStream.WriteInteger(Section, 'Style', Byte(smFont.Style));
15    finally
16      FStream.free;
17    end;
18  end;
19  
20  procedure LoadFont(FName: string; Section: string; smFont: TFont);
21  var
22    FStream: TIniFile;
23  begin
24    FStream := TIniFile.Create(Fname);
25    try
26      smFont.Name := FStream.ReadString(Section, 'Name', smFont.Name);
27      smFont.CharSet := TFontCharSet(FStream.ReadInteger(Section, 'CharSet',
28        smFont.CharSet));
29      smFont.Color := TColor(FStream.ReadInteger(Section, 'Color', smFont.Color));
30      smFont.Size := FStream.ReadInteger(Section, 'Size', smFont.Size);
31      smFont.Style := TFontStyles(Byte(FStream.ReadInteger(Section, 'Style',
32        Byte(smFont.Style))));
33    finally
34      FStream.free;
35    end;
36  end;
37  
38  //Here 's how to use the procedures:
39  
40  {Save Font}
41  
42  procedure TForm1.Button1Click(Sender: TObject);
43  begin
44    SaveFont('font.ini', 'label', Label1.Font);
45  end;
46  
47  procedure TForm1.Label1DblClick(Sender: TObject);
48  begin
49    if FontDialog1.Execute then
50      Label1.Font := FontDialog1.Font
51  end;
52  
53  {Load Font}
54  
55  procedure TForm1.Button2Click(Sender: TObject);
56  begin
57    LoadFont('font.ini', 'label', Label1.Font);
58  end;



Solve 2:

This code converts an instance of TFont to/ from a string. Just store that string 
whereever you want:

59  function GetAnyFontAsStr(Font: TFont): string;
60  begin
61    Result := 'DEFAULT';
62    if Assigned(Font) then
63    begin
64      Result := QuotedStr('Name=' + Font.Name);
65      Result := Result + ';
66        Size = 'nt.Size);
67        Result := Result + ';
68        Color = 'g(Font.Color);
69        Result := Result + ';
70        Pitch = 'TypeInfo(TFontPitch), Ord(Font.Pitch));
71        Result := Result + ';
72        Style = 'e(Font.Style));
73    end;
74  end;
75  
76  procedure SetAnyFontAsStr(var Font: TFont; aFontStr: string);
77  var
78    FontStrList: TStringList;
79  begin
80    if Assigned(Font) then
81    begin
82      FontStrList := TStringList.Create;
83      FontStrList.QuoteChar := '''';
84      FontStrList.Delimiter := ';';
85      FontStrList.DelimitedText := aFontStr;
86      Font.Name := FontStrList.Values['Name'];
87      Font.Size := StrToInt(FontStrList.Values['Size']);
88      Font.Color := StringToColor(FontStrList.Values['Color']);
89      Font.Pitch := TFontPitch(GetEnumValue(TypeInfo(TFontPitch),
90        FontStrList.Values['Pitch']));
91      Font.Style := TFontStyles(Byte(StrToIntDef(FontStrList.Values['Style'], 0)));
92      FontStrList.Free;
93    end;
94  end;



Solve 3:

95  uses
96    IniFiles;
97  
98  procedure SaveInfo;
99  var
100   t: TIniFile;
101   s: string;
102 begin
103   s := ExtractFilePath(ParamStr(0)) + 'fontinfo.ini';
104   t := TIniFile.Create(s);
105   {load previous values into Font dialog if there are any}
106   if FileExists(s) then
107     with FontDialog1.Font do
108     begin
109       Name := t.ReadString('Font', 'Name', Name);
110       Size := t.ReadInteger('Font', 'Size', Size);
111       Color := t.ReadInteger('Font', 'Color', Color);
112       if t.ReadBool('Font', 'Bold', False) then
113         Style := Style + [fsBold];
114       if t.ReadBool('Font', 'Italic', False) then
115         Style := Style + [fsItalic];
116       if t.ReadBool('Font', 'Underline', False) then
117         Style := Style + [fsUnderline];
118       if t.ReadBool('Font', 'Strikeout', False) then
119         Style := Style + [fsStrikeOut];
120     end;
121   if FontDialog1.Execute then
122   begin
123     {write new values to INI file for next time}
124     with FontDialog1.Font do
125     begin
126       t.WriteString('Font', 'Name', Name);
127       t.WriteInteger('Font', 'Size', Size);
128       t.WriteInteger('Font', 'Color', Color);
129       t.WriteBool('Font', 'Bold', fsBold in Style);
130       t.WriteBool('Font', 'Italic', fsItalic in Style);
131       t.WriteBool('Font', 'Underline', fsUnderline in Style);
132       t.WriteBool('Font', 'Strikeout', fsStrikeout in Style);
133     end;
134   end;
135   t.Free;
136 end;


As you can see, the Color property is stored as an integer. Therefore, if you need to also store the ColorDialog.Color property, use the ReadInteger and WriteInteger methods.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC