DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

FontExists

Checks if a named font exists on the sytem.

function FontExists(const FontName: string): Boolean;
begin
  Result := Forms.Screen.Fonts.IndexOf(FontName) >= 0;
end;

IsTrueTypeFont (TFont overload)

Checks if a font is a true type font.

function IsTrueTypeFont(const Font: Graphics.TFont): Boolean; overload;
var
  DC: Windows.HDC;          // device context in which font is selected
  TM: Windows.TTextMetric;  // text metrics for font in DC
begin
  DC := CreateDisplayDC;
  try
    Windows.SelectObject(DC, Font.Handle);
    Windows.GetTextMetrics(DC, TM);
    Result := IsFlagSet(TM.tmPitchAndFamily, Windows.TMPF_TRUETYPE);
  finally
    Windows.DeleteDC(DC);
  end;
end;

IsTrueTypeFont (string overload)

Checks if a named font is a true type font.

function IsTrueTypeFont(const FontName: string): Boolean; overload;
var
  Font: Graphics.TFont;
begin
  Font := Graphics.TFont.Create;
  try
    Font.Name := FontName;
    Result := IsTrueTypeFont(Font);
  finally
    Font.Free;
  end;
end;

SetDefaultFont

Sets the given TFont object to the default GUI font for the operating system.

procedure SetDefaultFont(const AFont: Graphics.TFont);
begin
  AFont.Handle := Windows.GetStockObject(Windows.DEFAULT_GUI_FONT);
end;

SetDesktopIconFont

Sets the given TFont object to the font used for desktop icons on the operating system. If the required font is not supported by the OS then the system default GUI font is used instead.

procedure SetDesktopIconFont(const AFont: Graphics.TFont);
var
  LogFont: Windows.TLogFont;  // structure containing desktop icon font info
begin
  if Windows.SystemParametersInfo(
    Windows.SPI_GETICONTITLELOGFONT, SizeOf(LogFont), @LogFont, 0) then
    AFont.Handle := Windows.CreateFontIndirect(LogFont)
  else
    SetDefaultFont(AFont);
end;

SetVistaContentFont

Sets the given TFont object to the Vista content font and adjusts the font's size accordingly. Does nothing if (1) the operating system is not at least Vista, (2) the Vista content font is not available or (3) the font is already the required font.

procedure SetVistaContentFont(const AFont: Graphics.TFont);
const
  VistaContentFont = 'Calibri'; // name of Vista content font
begin
  if (SysUtils.Win32MajorVersion >= 6)
    and not SysUtils.SameText(AFont.Name, VistaContentFont)
    and FontExists(VistaContentFont) then
  begin
    AFont.Size := AFont.Size + 2;
    AFont.Name := VistaContentFont;
  end;
end;

SetVistaFont

Sets the given TFont object to the Vista default font and adjusts the font's size accordingly. Does nothing if (1) the operating system is not at least Vista, (2) default Vista font is not available or (3) the font is already the Vista default font.

procedure SetVistaFont(const AFont: Graphics.TFont);
const
  VistaFont = 'Segoe UI'; // name of main Vista font
begin
  if (SysUtils.Win32MajorVersion >= 6)
    and not SysUtils.SameText(AFont.Name, VistaFont)
    and FontExists(VistaFont) then
  begin
    AFont.Size := AFont.Size + 1;
    AFont.Name := VistaFont;
  end;
end;

View the whole database.

Go to the DelphiDabbler website.