如何在 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)。
最佳答案
是的,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;
编辑:在较新的 Delphi 版本中,EnumFontFamilies
函数被描述为返回 Integer
结果(根据 MSDN ),如 Andreas Rejbrand在评论中注意到,因此结果应视为:
if EnumFontFamilies(Canvas.Handle,
PChar('Courier New'),
@EnumFontsProc,0) <> 0 then
关于delphi - 如何在 Delphi 中检测等宽字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43861156/