Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Font-Attribute in INI-Datei speichern (https://www.delphipraxis.net/73434-font-attribute-ini-datei-speichern.html)

PeterPanino 18. Jul 2006 01:09


Font-Attribute in INI-Datei speichern
 
Hallo,

ich muss alle Attribute eines Font (Name, Size, Color usw.) in einer INI-Datei abspeichern. Gibt es dafür eine bequeme Komplettroutine oder muss ich jedes Font-Attribut einzeln abspeichern?

Vielen Dank im Voraus!

semo 18. Jul 2006 06:02

Re: Font-Attribute in INI-Datei speichern
 
ich denke, dass du die alle einzeln abspeichern musst.
um nen bissl tipparbeit kommt man nun mal als programmierer nicht herum - und mal ehrlich - so viel arbeit ist das doch gar nicht.

v2afrank 18. Jul 2006 06:10

Re: Font-Attribute in INI-Datei speichern
 
Ich bin mir nicht mehr sicher wo ich die Routinen gefunden habe. Funktionieren aber
Delphi-Quellcode:
function FontToString(Font: TFont): String;
begin
  // name, size, bold, italic, underline, strikethrough, colour
  Result := Format('%s,%d,%d%d%d%d,%s', [Font.Name, Font.Size,
    Integer(fsBold in Font.Style), Integer(fsItalic in Font.Style),
      Integer(fsUnderline in Font.Style), Integer(fsStrikeOut in Font.Style),
      ColorToString(Font.Color)]);
end;

procedure StringToFont(Str: String; Font: TFont);
const
  SEP = ',';
  EXCEPT_MSG = 'Invalid string to font conversion';
var
  i: Integer;
begin
  {any exception/error we encounter will ultimately
  result in an EConvertError being raised}
  // name
  i := Pos(SEP, Str);
  if i = 0 then
    raise EConvertError.Create(EXCEPT_MSG);
  Font.Name := Copy(Str, 1, i - 1);
  Delete(Str, 1, i);

  // size
  i := Pos(SEP, Str);
  if i = 0 then
    raise EConvertError.Create(EXCEPT_MSG);
  Font.Size := StrToInt(Copy(Str, 1, i - 1));
  Delete(Str, 1, i);

  // bold, italic, underline, strikethrough
  if Pos(SEP, Str) <> 5 then
    raise EConvertError.Create(EXCEPT_MSG);
  Font.Style := [];
  if Str[1] = '1' then
    Font.Style := Font.Style + [fsBold];
  if Str[2] = '1' then
    Font.Style := Font.Style + [fsItalic];
  if Str[3] = '1' then
    Font.Style := Font.Style + [fsUnderline];
  if Str[4] = '1' then
    Font.Style := Font.Style + [fsStrikeOut];

  Delete(Str, 1, 5);

  // colour
  Font.Color := StringToColor(Str);
end;

marabu 18. Jul 2006 06:46

Re: Font-Attribute in INI-Datei speichern
 
Hier noch eine Alternative - nicht voll ausgebaut und nur um das Prinzip zu demonstrieren:

Delphi-Quellcode:
uses
  TypInfo,
  IniFiles;

procedure SaveToIniFile(o: TObject; ini: TMemIniFile; sectionName: String);
var
  i, iProps: integer;
  ppl: PPropList;
  ppi: PPropInfo;
begin
  ini.EraseSection(sectionName);
  iProps := GetPropList(o, ppl);
  for i := 0 to Pred(iProps) do
  begin
    ppi := ppl[i];
    case ppi.PropType^.Kind of
      tkString,
      tkLString:
        ini.WriteString(sectionName, ppi.Name, GetStrProp(o, ppi));
      tkInteger:
        ini.WriteString(sectionName, ppi.Name, IntToStr(GetOrdProp(o, ppi)));
      tkEnumeration:
        ini.WriteString(sectionName, ppi.Name, GetEnumProp(o, ppi));
      tkSet:
        ini.WriteString(sectionName, ppi.Name, GetSetProp(o, ppi));
      else
        ini.WriteString(sectionName, ppi.Name, '???');
    end;
  end;
end;

procedure TDemoForm.SaveButtonClick(Sender: TObject);
var
  fn: TFileName;
  ini: TMemIniFile;
begin
  fn := ChangeFileExt(ParamStr(0), '.ini');
  ini := TMemIniFile.Create(fn);
  SaveToIniFile(Font, ini, 'Font');
  ini.ReadSectionValues('Font', ValueListEditor.Strings);
  ini.UpdateFile;
  ini.Free;
end;
Grüße vom marabu

PeterPanino 19. Jul 2006 02:21

Re: Font-Attribute in INI-Datei speichern
 
Danke für die Hinweise - haben mir sehr geholfen!


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:55 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz