How can I detect monospace fonts in Delphi?

How can I detect monospace fonts in Delphi?

TFont.Pitch should be fpFixed I think, but it does not work for me with Delphi XE4:

 var Font: TFont; begin Font := TFont.Create; Font.Name := 'Courier New'; if Font.Pitch = fpFixed then ShowMessage('Monospace Font!'); ... 

Font.Pitch based on GetObject for WinAPI. It should return to lfPitchAndFamily FIXED_PITCH , but I always get DEFAULT_PITCH for all fonts (also for Arial).

+5
source share
1 answer

Yes, GetObject does return DEFAULT_PITCH . But you can get the true value by listing the fonts with the desired name:

 function EnumFontsProc(var elf: TEnumLogFont; var tm: TNewTextMetric; FontType: Integer; Data: LPARAM): Integer; stdcall; begin; Result := Integer(FIXED_PITCH = (elf.elfLogFont.lfPitchAndFamily and FIXED_PITCH)); end; procedure TForm1.Button13Click(Sender: TObject); begin; if EnumFontFamilies(Canvas.Handle, PChar('Courier New'), @EnumFontsProc,0) then Caption := 'Fixed' else Caption := 'Variable'; end; 
+5
source

All Articles