2006. június 10., szombat

How to get the font family when the user selects a font


Problem/Question/Abstract:

Windows organizes fonts by family and categorizes families with five family names. A sixth name ("Dontcare") allows an application to use the default font. These family names correspond to constants found in the WINGDI.H file: FF_DECORATIVE, FF_DONTCARE, FF_MODERN, FF_ROMAN, FF_SCRIPT, and FF_SWISS. An application uses these constants when it creates a font, selects a font, or retrieves information about a font. Fonts within a family are distinguished by size (10 point, 24 point, and so on) and style (regular, italic, and so on). However, it doesn't say how I can get that setting for any given font. Is there a way to use GetObject to do this?

Answer:

The following table describes the font-family names:

Font-family name: Description
Decorative: Specifies a novelty font. An example is Old English.
Dontcare: Specifies a generic family name. This name is used when information about a font does not exist or does not matter.
Modern: Specifies a monospace font with or without serifs. Monospace fonts are usually modern; examples include Pica, Elite, and Courier New.
Roman: Specifies a proportional font with serifs. An example is Times New Roman.
Script: Specifies a font that is designed to look like handwriting; examples include Script and Cursive.
Swiss: Specifies a proportional font without serifs. An example is Arial.

Here's a sample method, using the form's font:

procedure TForm1.Button1Click(Sender: TObject);
var
  LogFont: TLogFont;
  BytesReturned: integer;
  s: string;
begin
  BytesReturned := GetObject(Font.Handle, sizeof(LogFont), @LogFont);
  if BytesReturned = 0 then
  begin
    caption := 'Failed';
    exit;
  end;
  case LogFont.lfPitchAndFamily and $F0 of
    FF_DONTCARE: s := 'Don''t care, or don''t know.';
    FF_ROMAN: s := 'Variable stroke width, serifed.';
    FF_SWISS: s := 'Variable stroke width, sans-serifed.';
    FF_MODERN: s := 'Constant stroke width, serifed or sans-serifed.';
    FF_SCRIPT: s := 'Script; Cursive, etc.';
    FF_DECORATIVE: s := 'Decorative: Old English, etc.';
  end;
  caption := s;
end;

Nincsenek megjegyzések:

Megjegyzés küldése