Get System Font

How Can I get System Font in Windows (eg.: the one that is used on the taskbar (size, font name, ...)) ?
rafaelriAsked:
Who is Participating?
 
Slick812Commented:
hello rafaelri, I don't think you have the correct term for the font you want. It isn't the "System font", but the active Caption Font. Anyway, here's how to get the handle for the windows System Font,

hSysFont := GetStockObject(SYSTEM_FONT);

the font used in the Taskbar is the active Caption Font and you can get the FontLog for that font from the NonClientMetrics returned with the SystemParametersInfo function. Here is a procedure I have used to get a few of the windows fonts

procedure GetSystemFonts;
var
NonClMetrics: TNonClientMetrics;
TempLogF: TLogFont;
CaptionFont, MenuFont, MessFont, IconFont, StatusFont: THandle;
CapFontHeight: Integer;
CapFontName: String;
begin
NonClMetrics.cbSize := SizeOf(NonClMetrics);
SystemParametersInfo(
    SPI_GETNONCLIENTMETRICS,     // system parameter to query or set
    0,
    @NonClMetrics, // address of NonClientMetrics
    0
   );
{The SystemParametersInfo function queries or sets systemwide parameters
with  SPI_GETNONCLIENTMETRICS  you get the metrics associated with the
standard nonclient area of windows.}

CaptionFont := CreateFontIndirect(NonClMetrics.lfCaptionFont);
CapFontName := String(NonClMetrics.lfCaptionFont.lfFaceName);
CapFontHeight := NonClMetrics.lfCaptionFont.lfHeight;
MenuFont := CreateFontIndirect(NonClMetrics.lfMenuFont);
StatusFont := CreateFontIndirect(NonClMetrics.lfStatusFont);
MessFont := CreateFontIndirect(NonClMetrics.lfMessageFont);

{see API help for index NONCLIENTMETRICS, to see other info in NonClMetrics}
SystemParametersInfo(SPI_GETICONTITLELOGFONT,SizeOf(TempLogF),@TempLogF,0);
{this gets the font used under icons}
IconFont := CreateFontIndirect(TempLogF);
end;

if you want to use these font handles as delphi fonts you can do a handle assignment

Edit1.Font.Handle := CaptionFont;

or
Form1.Canvas.Font.Handle := CaptionFont;

you should DeleteObject when you are finished with the fonts

DeleteObject(CaptionFont);
DeleteObject(MenuFont);


ask questions if it's unclear
0
 
AloneCommented:
Try SystemParametersInfo. See help for SPI_GETICONTITLELOGFONT, SPI_GETNONCLIENTMETRICS and NONCLIENTMETRICS structure.
0
 
ginsonicCommented:
This procedure enum all system fonts.

procedure TMainForm.GetFontNames;
var
  DC: HDC;
begin
  DC := GetDC(0);
  EnumFonts(DC, nil, @EnumFontsProc, Pointer(FontName.Items));
  ReleaseDC(0, DC);
  FontName.Sorted := True;
end;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.