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
of the WinAPI. It should return in lfPitchAndFamily
FIXED_PITCH
, but I always get DEFAULT_PITCH
for all fonts (also for Arial).
Yes, GetObject
really returns DEFAULT_PITCH
. But you can get true value through enumeration of fonts with needed 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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With