Solved

save and load font properties

Posted on 2004-09-24
6
292 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

792 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