如何將Tfont的內容存入數據表中? (100分)

  • 主题发起人 主题发起人 kevin_m
  • 开始时间 开始时间
K

kevin_m

Unregistered / Unconfirmed
GUEST, unregistred user!
如何將Tfont的內容存入數據表中?
 
把TFont中最主要的属性 写到Stream中 或者是 String中 再写入 Table
需要时再从 Table中读出 赋给Font
 
下面是一个用INI文件存储字体信息的例子:

Question: How can I save/restore the TFont-object in INI and/or text file?
Answer: Sometimes you need to save/to load a font information in INI-file, Registry or some text file.

Now I desribe the some different methods.

1. very easy but not result isn't compact and effective (by data storage)
procedure SaveFont(FStream: TIniFile
Section: string
smFont: TFont);
begin //字体的主要信息在此
FStream.WriteString(Section, Ident + 'Name', smFont.Name);
FStream.WriteInteger(Section, Ident + 'CharSet', smFont.CharSet);
FStream.WriteInteger(Section, Ident + 'Color', smFont.Color);
FStream.WriteInteger(Section, Ident + 'Size', smFont.Size);
FStream.WriteInteger(Section, Ident + 'Style', Byte(smFont.Style));
end;

procedure LoadFont(FStream: TIniFile
Section: string
smFont: TFont);
begin
smFont.Name := FStream.ReadString(Section, Ident + 'Name', smFont.Name);
smFont.CharSet := TFontCharSet(FStream.ReadInteger(Section, Ident + 'CharSet', smFont.CharSet));
smFont.Color := TColor(FStream.ReadInteger(Section, Ident + 'Color', smFont.Color));
smFont.Size := FStream.ReadInteger(Section, Ident + 'Size', smFont.Size);
smFont.Style := TFontStyles(Byte(FStream.ReadInteger(Section, Ident + 'Style', Byte(smFont.Style))));
end;


2. more hardly than first method, but result is compact. I use this method in all own apps.
procedure SaveFont(FStream: TIniFile
Section: string
smFont: TFont);
begin
FStream.WriteString(Section, 'Font', smFont.Name + ',' +
IntToStr(smFont.CharSet) + ',' +
IntToStr(smFont.Color) + ',' +
IntToStr(smFont.Size) + ',' +
IntToStr(Byte(smFont.Style)));
end;

procedure LoadFont(FStream: TIniFile
Section: string
smFont: TFont);
var s, Data: string;
i: Integer;
begin
s := FStream.ReadString(Section, 'Font', ',,,,');
try
i := Pos(',', s);
if i > 0 then
begin
{Name}
Data := Trim(Copy(s, 1, i-1));
if Data <> '' then
smFont.Name := Data;
Delete(s, 1, i);
i := Pos(',', s);
if i > 0 then
begin
{CharSet}
Data := Trim(Copy(s, 1, i-1));
if Data <> '' then
smFont.Charset := TFontCharSet(StrToIntDef(Data, smFont.Charset));
Delete(s, 1, i);
i := Pos(',', s);
if i > 0 then
begin
{Color}
Data := Trim(Copy(s, 1, i-1));
if Data <> '' then
smFont.Color := TColor(StrToIntDef(Data, smFont.Color));
Delete(s, 1, i);
i := Pos(',', s);
if i > 0 then
begin
{Size}
Data := Trim(Copy(s, 1, i-1));
if Data <> '' then
smFont.Size := StrToIntDef(Data, smFont.Size);
Delete(s, 1, i);
{Style}
Data := Trim(s);
if Data <> '' then
smFont.Style := TFontStyles(Byte(StrToIntDef(Data,
Byte(smFont.Style))));
end
end
end
end;
except
end;
end;

3. as alternative for 1&amp;2 methods I can offer the third - you can create a temporary stream, save the
wished font component in this stream (Stream.SaveComponent) and then you can navigate the byte-by-byte
in stream, to convert each byte into hex (or some other radix) and save into your text file as string.
Each byte is a two symbols for hex radix. For font reading - on the contrary...
PS: personally I not used this method:)))


Uploader: Mike Shkolnik
 
creation-zy,是个好人!
 
尝试了下面这个例子,,你就举一反三了,方便,快捷,全面!!

procedure TForm1.Button1Click(Sender: TObject);
var
tt :tmemorystream;
tf :tfont;
begin
tt :=tmemorystream.Create;
tt.Write(Label1.Font,sizeof(tfont));
tt.SaveToFile('c:/123.dat');
tt.Clear;
tt.LoadFromFile('c:/123.dat');
tt.Read(tf,sizeof(tfont));
Label2.Font.Assign(tf)
[blue]lable2 的字体变成了 label1 d的一样了[/blue]
tt.free;
end


根据需要,你可以将 tt 这个STREAM 写到你的数据库 blod 字段里面去
 
多人接受答案了。
 
我发现一个很奇怪的问题
我修改字体后没有写进文件!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部