Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

save and load font properties

Posted on 2004-09-24
6
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
ginsonic earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

721 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