Ir al contenido


Foto

[TRUCO DELPHI] Salvar una Fuente/Font al registro o a un Stream.


  • Por favor identifícate para responder
No hay respuestas en este tema

#1 sir.dev.a.lot

sir.dev.a.lot

    Advanced Member

  • Miembros
  • PipPipPip
  • 545 mensajes
  • Location127.0.0.1

Escrito 29 agosto 2016 - 08:00

[TRUCO DELPHI] Salvar una Fuente/Font al registro o a un Stream.


delphi
  1. type
  2. FontRec = packed record
  3. Color: TColor;
  4. LogFont: TLogFont;
  5. end;
  6.  
  7. // Salva la fuente al Registro
  8. procedure SaveFontToReg(reg: TRegistry; const key, id: string; Font: TFont);
  9. var
  10. fRec: FontRec;
  11. begin
  12. if Windows.GetObject(Font.Handle, SizeOf(fRec.LogFont), @fRec.LogFont) > 0
  13. then
  14. begin
  15. if reg.OpenKey(key, True) then
  16. try
  17. fRec.Color := Font.Color;
  18. reg.WriteBinaryData(id, fRec, SizeOf(fRec));
  19. finally
  20. reg.CloseKey;
  21. end;
  22. end;
  23. end;
  24.  
  25. // Carga la fuente desde el Registro
  26. procedure LoadFont(reg: TRegistry; const key, id: string; Font: TFont);
  27. var
  28. fRec: FontRec;
  29. begin
  30. if reg.OpenKey(key, False) then
  31. try
  32. if reg.ReadBinaryData(id, frec, SizeOf(fRec)) = SizeOf(fRec) then
  33. Font.Handle := CreateFontIndirect(fRec.LogFont);
  34. Font.Color := fRec.Color;
  35. finally
  36. reg.CloseKey;
  37. end;
  38. end;
  39.  
  40. // Guarda la fuente a un Stream
  41. procedure WriteFontToStream(s: TStream; Font: TFont);
  42. var
  43. fRec: FontRec;
  44. sz: integer;
  45. begin
  46. sz := SizeOf(fRec.LogFont);
  47. if Windows.GetObject(Font.Handle, sz, @fRec.LogFont) > 0 then
  48. begin
  49. s.Write(sz, SizeOf(Integer));
  50. fRec.Color := Font.Color;
  51. s.Write(fRec, SizeOf(fRec));
  52. end
  53. else
  54. begin
  55. sz := 0;
  56. s.Write(sz, SizeOf(Integer));
  57. end;
  58. end;
  59.  
  60. // Carga la Fuente desde un Stream
  61. procedure ReadFont(s: TStream; Font: TFont);
  62. var
  63. fRec: FontRec;
  64. sz: integer;
  65. begin
  66. s.read(sz, SizeOf(Integer));

Codigo restante.


delphi
  1. if sz = SizeOf(fRec.LogFont) then
  2.  
  3. begin
  4.  
  5. s.read(fRec, SizeOf(fRec));
  6.  
  7. Font.Handle := CreateFontIndirect(fRec.LogFont);
  8.  
  9. Font.Color := fRec.Color;
  10.  
  11. end;
  12.  
  13. end;

Saludos!


  • 2




IP.Board spam blocked by CleanTalk.