Solved

save and load font properties

Posted on 2004-09-24
6
289 Views
Last Modified: 2013-12-03
hi,

i would like to save my edit object font properties to database and load from database.

thanks...
0
Comment
Question by:komputer
  • 5
6 Comments
 
LVL 9

Expert Comment

by:ginsonic
ID: 12143221
Use this code to get as string all properties:

procedure ListComponentProperties(Component: TComponent; Strings: TStrings);
var
  Count, Size, I: Integer;
  List: PPropList;
  PropInfo: PPropInfo;
  PropOrEvent, PropValue: string;
begin
  Count := GetPropList(Component.ClassInfo, tkAny, nil);
  Size  := Count * SizeOf(Pointer);
  GetMem(List, Size);
  try
    Count := GetPropList(Component.ClassInfo, tkAny, List);
    for I := 0 to Count - 1 do
    begin
      PropInfo := List^[I];
      if PropInfo^.PropType^.Kind in tkMethods then
        PropOrEvent := 'Event'
      else
        PropOrEvent := 'Property';
      PropValue := VarToStr(GetPropValue(Component, PropInfo^.Name));
      Strings.Add(Format('[%s] %s: %s = %s', [PropOrEvent, PropInfo^.Name,
        PropInfo^.PropType^.Name, PropValue]));
    end;
  finally
    FreeMem(List);
  end;
end;
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12143229
Add
....
uses
  Typinfo;
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12143527
To save:

uses
  Typinfo;

function GetFontProp( anObj: TObject) : TFont;
var
  PInfo: PPropInfo;
begin
  PInfo := GetPropInfo( anObj.ClassInfo, 'font' );
  Result := Nil;
  if PInfo <> Nil then
    if (PInfo^.Proptype^.Kind = tkClass) and
             GetTypeData(PInfo^.Proptype^)^.ClassType.InheritsFrom(TFont) then
      Result := TFont(GetOrdProp( anObj, PInfo ));
end;

function StyleToString( styles: TFontStyles ): String;
var
  style: TFontStyle;
begin
  Result := '[';
  for style := Low(style) to High(style) do
  begin
    if style IN styles then
    begin
      if Length(result) > 1 then
        result := result + ',';
      result := result + GetEnumname( typeInfo(TFontStyle), Ord(style));
    end;
  end;
  Result := Result + ']';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  font: TFont;
begin
  font := GetFontProp( Form1 ); // or work component - if already have an TFont use it with font.Assign(MyFont)
  if not Assigned( font ) then
    Exit;
  Table1.Insert;
  Table1.FieldByName('Charset').AsInteger:=font.charset;
  Table1.FieldByName('Name').AsString:=font.Name;
  Table1.FieldByName('Size').AsInteger:=font.size;
  Table1.FieldByName('Color').AsString:='$' + IntToHex(font.color,8);
  Table1.FieldByName('Style').AsString:=StyleToString( font.Style );
  Table1.Post;
end;


0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 9

Accepted Solution

by:
ginsonic earned 50 total points
ID: 12143602
to load use:

function StringToStyle( S: String ): TFontStyles;
var
  sl: TStringlist;
  style: TFontStyle;
  i: Integer;
begin
  Result := [];
  if Length(S) < 2 then
    Exit;
  if S[1] = '[' then
    Delete(S, 1, 1);
  if S[Length(S)] = ']' then
    Delete(S, Length(S), 1);
  if Length(S) = 0 then
    Exit;
  sl := TStringlist.Create;
  try
    sl.commatext := S;
    for i := 0 to sl.Count - 1 do
    begin
      try
        style := TFontStyle( GetEnumValue( Typeinfo(TFontStyle), sl[i] ));
        Include( Result, style );
      except
      end;
    end;
  finally
    sl.free
  end;
end;

.......
var
  font: TFont;
begin
  font := GetFontProp( Form1 ); // or your component
  if not Assigned( font ) then
    Exit;
  font.Charset := Table1.FieldByName('Charset').AsInteger;
  font.Name := Table1.FieldByName('Name').AsString;
  font.Size := Table1.FieldByName('Size').AsInteger;
  font.Color := TColor( StrToInt(Table1.FieldByName('Name').AsString));
  font.Style := StringToStyle(Table1.FieldByName('Name').AsString);
end;
0
 

Author Comment

by:komputer
ID: 12150042
thanks ginsonic...
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12150271
Welcome! :)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question