记得一年前软件里面用到字体设置时,想把字体信息保存到配置文件里面都是把相应的属性分开来保存一下,这样一直使用,最近想delphi自己的dfm文件也同样会保存这些信息,它又是怎样保存的呢。。
用记事本打开dfm文件会看到类似这样的文本:
object Label1: TLabel Left = 24 Top = 32 Width = 140 Height = 46 Caption = #20445#23384#23454#20363 Font.Charset = GB2312_CHARSET Font.Color = clBlue Font.Height = -35 Font.Name = #24494#36719#38597#40657 Font.Style = [fsBold, fsUnderline, fsStrikeOut] ParentFont = False end
自己写了个直接保存成文件文件的,记录一下
unit Font2String; interface uses Classes, SysUtils, Dialogs, Graphics; function Font2File(AFont: TFont; APath: string): Boolean; function File2Font(APath: string; AFont: TFont): Boolean; implementation function Font2File(AFont: TFont; APath: string): Boolean; var FD: TFontDialog; begin Result := False; FD := TFontDialog.Create(nil); try FD.Font.Assign(AFont); WriteComponentResFile(APath, FD); Result := True; finally FD.Free; end; end; function File2Font(APath: string; AFont: TFont): Boolean; var FD: TFontDialog; begin Result := False; FD := TFontDialog.Create(nil); try ReadComponentResFile(APath, FD);//如果此外为nil,下面的注册去掉 AFont.Assign(FD.Font); Result := True; finally FD.Free; end; end; //initialization //RegisterClass(TFontDialog); end.
例子:
procedure TForm1.Button1Click(Sender: TObject); begin Font2File(Label1.Font, 'd:\Font.fnt');//把字体保存成文件 end; procedure TForm1.Button2Click(Sender: TObject); begin File2Font( 'd:\Font.fnt', Label2.Font);//从文件里读取字体 end;