【问题标题】:How can I detect monospace fonts in Delphi?如何在 Delphi 中检测等宽字体?
【发布时间】:2017-05-09 04:29:21
【问题描述】:
如何在 Delphi 中检测等宽字体?
TFont.Pitch 应该是 fpFixed 我认为,但它不适用于 Delphi XE4:
var
Font: TFont;
begin
Font := TFont.Create;
Font.Name := 'Courier New';
if Font.Pitch = fpFixed then
ShowMessage('Monospace Font!');
...
Font.Pitch 基于 WinAPI 的 GetObject。它应该返回 lfPitchAndFamily FIXED_PITCH,但我总是得到所有字体的 DEFAULT_PITCH(也适用于 Arial)。
【问题讨论】:
标签:
delphi
winapi
delphi-xe4
【解决方案1】:
是的,GetObject 真的返回 DEFAULT_PITCH。但是您可以通过枚举具有所需名称的字体来获得真正的价值:
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;