whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

172 Visitors Online


 
...save a font to the registry/a stream?
Autor: Mike Orriss
Homepage: http://www.teamb.com
[ Print tip ]  

Tip Rating (4):  
     


type
  
FontRec = packed record
    
Color: TColor;
    LogFont: TLogFont;
  end;

// Save a font to the registry
// Eine Schrift in die Registry speichern
procedure SaveFontToReg(reg: TRegistry; const key, id: string; Font: TFont);
var
  
fRec: FontRec;
begin
  if 
Windows.GetObject(Font.Handle, SizeOf(fRec.LogFont), @fRec.LogFont) > 0 then
  begin
    if 
reg.OpenKey(key, True) then
      try
        
fRec.Color := Font.Color;
        reg.WriteBinaryData(id, fRec, SizeOf(fRec));
      finally
        
reg.CloseKey;
      end;
  end;
end;

// Load a font from the registry
// Eine Schrift von der Registry laden
procedure LoadFont(reg: TRegistry; const key, id: string; Font: TFont);
var
  
fRec: FontRec;
begin
  if 
reg.OpenKey(key, False) then
    try
      if 
reg.ReadBinaryData(id, frec, SizeOf(fRec)) = SizeOf(fRec) then
        
Font.Handle := CreateFontIndirect(fRec.LogFont);
      Font.Color := fRec.Color;
    finally
      
reg.CloseKey;
    end;
end;

// Save a font to a stream
// Eine Schrift in einen Stream speichern
procedure WriteFontToStream(s: TStream; Font: TFont);
var
  
fRec: FontRec;
  sz: integer;
begin
  
sz := SizeOf(fRec.LogFont);
  if Windows.GetObject(Font.Handle, sz, @fRec.LogFont) > 0 then
  begin
    
s.Write(sz, SizeOf(Integer));
    fRec.Color := Font.Color;
    s.Write(fRec, SizeOf(fRec));
  end
  else
  begin
    
sz := 0;
    s.Write(sz, SizeOf(Integer));
  end;
end;

// Read a font from a stream
// Eine Schrift von einem Stream laden
procedure ReadFont(s: TStream; Font: TFont);
var
  
fRec: FontRec;
  sz: integer;
begin
  
s.read(sz, SizeOf(Integer));
  if sz = SizeOf(fRec.LogFont) then
  begin
    
s.read(fRec, SizeOf(fRec));
    Font.Handle := CreateFontIndirect(fRec.LogFont);
    Font.Color  := fRec.Color;
  end;
end;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners