Font中有Name,Size……,如果保存时需分别保存,而且有些变量也不能转换,请问有什么办法可以将怎样Font变量保存?谢谢

解决方案 »

  1.   

    uses inifiles;var myinifile:tinifile;tformcreate:
      myinifile:=tinifile.create('ini文件路径');
      label1.font.nema:=myinifile.readstring('font','fontname','宋体');
      label1.font.size:=myinifile.readinteger('font','fontsize',8);//第三个参数为默认值tformclose:
      myinifile.writestring('font','fontname',label1.font.nema);
      myinifile.writeinteger('font','fontsize',label1.font.size);
      myinifile.destroy;
      

  2.   

    fjswge 正是将Font中的Font.name,Font.size分开保存,能不能够将Font全部变量一下保存下来
      

  3.   

    fjswge 正是将Font中的Font.name,Font.size分开保存,能不能够将Font全部变量一下保存下来你写一个readfont,writefont的函数,然后再定义一个记录(record),这样不就等于实现了你的要求么?其实delphi的Font本身就是个记录类型啊。
      

  4.   

    type  TFontRecord = record
        CharSet:Byte;
        Color:Integer;
        Name:string;
        Size:Integer;
        Style:Byte;
        PixelsPerInch:Integer;
        Pitch:0..2;
        Height:Integer;
      end;procedure FontRecordToFont(FontRecord:TFontRecord;Font:TFont);
    begin
      with FontRecord do
      begin
        Font.Charset:=CharSet;
        Font.Color:=Color;
        Font.Name:=Name;
        Font.Size:=Size;
        case Pitch of
          0:Font.Pitch:=fpDefault;
          1:Font.Pitch:=fpVariable;
          2:Font.Pitch:=fpFixed;
        end;
        Font.Style:=IntToFontStyles(Style);
      end;
    end;function FontToFontRecord(Font:TFont):TFontRecord;
    begin
      with Font do
      begin
        Result.CharSet:=Charset;
        Result.Color:=Color;
        Result.Height:=Height;
        Result.Name:=Name;
        case Pitch of
          fpDefault:Result.Pitch:=0;
          fpVariable:Result.Pitch:=1;
          fpFixed:Result.Pitch:=2;
        end;
        Result.Size:=Size;
        Result.PixelsPerInch:=PixelsPerInch;
        Result.Style:=FontStylesToInt(Style);
      end;
    end;function FontStylesToInt(FontStyles:TFontStyles):Byte;
    var
      T:Integer;
    begin
      T:=0;
      if fsBold in FontStyles then
        T:=T+8;
      if fsItalic in FontStyles then
        T:=T+4;
      if fsUnderline in FontStyles then
        T:=T+2;
      if fsStrikeOut in FontStyles then
        T:=T+1;
      Result:=T;
    end;function IntToFontStyles(FontInteger:Byte):TFontStyles;
    var
      FontT:TFontStyles;
    begin
      if (FontInteger>15) then
      begin
        Result:=[];
        Exit;
      end;
      FontT:=[];
      if FontInteger>=8 then
      begin
        FontT:=FontT+[fsBold];
        FontInteger:=FontInteger-8;
      end;
      if FontInteger>=4 then
      begin
        FontT:=FontT+[fsItalic];
        FontInteger:=FontInteger-4;
      end;
      if FontInteger>=2 then
      begin
        FontT:=FontT+[fsUnderline];
        FontInteger:=FontInteger-2;
      end;
      if FontInteger>=1 then
        FontT:=FontT+[fsStrikeOut];
      Result:=FontT;
    end;
      

  5.   

    要保存的无法就是name、size、style、color这几个属性,单独保存也没什么麻烦的如果你执意要在ini文件里保存记录,是不是有点太钻牛角尖了?
      

  6.   

    觉得  mastersky(浪)  的方法不错!---------------------------------------------------
    http://kmok.cn
      

  7.   

    写:
    procedure WriteFontToIni(Ini: TIniFile; Session, Name: string; Font: TFont);
    var
      data: TLogFont;  function Buf2Str(const buf; Len: Integer): string;
      const
        Chars: string = '0123456789ABCDEF';
      var
        i: Integer;
        p: PByte;
      begin
        setlength(result, len*2);
        p := @buf;
        for i := 0 to len - 1 do
        begin
          result[i shl 1+1] := Chars[p^ shr 4];
          result[i shl 1+2] := Chars[p^ and $F];
          inc(p);
        end;
      end;begin
      getobject(font.handle, sizeof(data), @data);
      Ini.WriteString(Session, Name, Buf2Str(data));
    end;读:
    procedure ReadFontFromIni(Ini: TIniFile; Session, Name: string; Font: TFont);
    var
      data: TLogFont;
      
      procedure Str2Buf(S: string; var Buf);
      var
        i: Integer;
        p: PByte;
      begin
        p := @buf;
        for i := 1 to length(s) do
        begin
          case s[i] of
            '0'..'9': p^ := p^ shl 4 + byte(s[i]) - byte('0');
            'A'..'F': p^ := p^ shl 4 + byte(s[i]) - byte('A') + 10;
            'a'..'f': p^ := p^ shl 4 + byte(s[i]) - byte('a')+10;
          end;
          if i and 1 = 0 then
            inc(p);
        end;
      end;begin
      Str2Buf(Ini.ReadString(Session, Name, ''), data);
      Font.Handle := CreateFontIndirect(@data);
    end;
      

  8.   

    没有成功呀!
    Ini.WriteString(Session, Name, Buf2Str(data));
    Font.Handle := CreateFontIndirect(@data);
    都有问题!