2010. március 13., szombat

How to get the font settings as defined in the Windows display properties


Problem/Question/Abstract:

I'm using a TMenuBar control and I would like my menus to be the same size as defined in the Windows display properties. The same goes for the rest of my application. How can I find out what the windows settings for fonts and sizes for menus are?

Answer:

function GetCaptionFont: TFont;
var
  ncMetrics: TNonClientMetrics;
begin
  ncMetrics.cbSize := sizeof(TNonClientMetrics);
  SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(TNonClientMetrics), @ncMetrics,
    0);
  Result := TFont.Create;
  Result.Handle := CreateFontIndirect(ncMetrics.lfCaptionFont);
end;

In Windows XP, applications seem to have a concept of a system font. That is, labels and captions seem to appear in another font other than MS Sans Serif. I was wondering if it is possible to detect this font and use it in Delphi applications. Currently, everything seems hard-coded to MS Sans Serif.

procedure GetCaptionFont(afont: TFont);
var
  ncMetrics: TNonClientMetrics;
begin
  assert(assigned(afont));
  ncMetrics.cbSize := sizeof(TNonClientMetrics);
  SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
    sizeof(TNonClientMetrics), @ncMetrics, 0);
  afont.Handle := CreateFontIndirect(ncMetrics.lfCaptionFont);
end;

The TNonClientMetrics structure also contains information on other fonts used in the non-client area:

lfCaptionFont: Font used in regular captions
lfSmCaptionFont: Font used in small captions
lfMenuFont: Font used in menus
lfStatusFont: Font used in status bars
lfMessageFont: Font used in message boxes

The problem with changing the forms font (and with it all controls that have Parentfont = true) is that it will likely change the size of some controls that autosize depending on fonts, and that can screw up your layout.

Nincsenek megjegyzések:

Megjegyzés küldése