如何在 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
基于GetObject
WinAPI 的。它应该返回lfPitchAndFamily
FIXED_PITCH
,但我总是得到DEFAULT_PITCH
适用于所有字体(也适用于 Arial)。
Yes, 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;
Edit:在较新的 Delphi 版本中EnumFontFamilies
函数被描述为返回Integer
结果(根据MSDN https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-enumfontfamiliesa),正如 Andreas Rejbrand 在评论中注意到的那样,因此结果应视为:
if EnumFontFamilies(Canvas.Handle,
PChar('Courier New'),
@EnumFontsProc,0) <> 0 then
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)