3

我想枚举所有文件C:\Windows\Fonts\

首先我FindFirst&FindNext用来获取所有文件

代码:

Path := 'C:\Windows\Fonts';
  if FindFirst(Path + '\*', faNormal, FileRec) = 0 then
    repeat

      Memo1.Lines.Add(FileRec.Name);

    until FindNext(FileRec) <> 0;
  FindClose(FileRec);

它得到一些这样的名称tahoma.ttf,显示 Tahoma regular在 Windows 字体文件夹中。

但我怎么能得到呢?

其次我为什么不能通过C:\Windows\Fonts\shell枚举文件

代码 :

var
  psfDeskTop : IShellFolder;
  psfFont : IShellFolder;
  pidFont : PITEMIDLIST;
  pidChild : PITEMIDLIST;
  pidAbsolute : PItemIdList;
  FileInfo : SHFILEINFOW;
  pEnumList : IEnumIDList;
  celtFetched : ULONG;
begin
  OleCheck(SHGetDesktopFolder(psfDeskTop));
  //Font folder path
  OleCheck(SHGetSpecialFolderLocation(0, CSIDL_FONTS, pidFont));
  OleCheck(psfDeskTop.BindToObject(pidFont, nil, IID_IShellFolder, psfFont));
  OleCheck(psfFont.EnumObjects(0, SHCONTF_NONFOLDERS or SHCONTF_INCLUDEHIDDEN
    or SHCONTF_FOLDERS, pEnumList));
  while pEnumList.Next(0, pidChild, celtFetched ) = 0 do
  begin
   //break in here
    pidAbsolute := ILCombine(pidFont, pidChild);
    SHGetFileInfo(LPCTSTR(pidAbsolute), 0, FileInfo, SizeOf(FileInfo),
    SHGFI_PIDL or SHGFI_DISPLAYNAME );
    Memo1.Lines.Add(FileInfo.szDisplayName);
  end;
end;

我知道 useScreen.Fonts可以获取字体列表,但它的显示不同于C:\Windows\Fonts\;

4

3 回答 3

7

记录的函数可以从字体文件中获取字体的名称。GetFontResourceInfo

试试这个样本

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Windows,
  SysUtils;


function GetFontResourceInfo(lpszFilename: PChar; var cbBuffer: DWORD; lpBuffer: PChar; dwQueryType: DWORD): DWORD; stdcall; external 'gdi32.dll' name 'GetFontResourceInfoW';

procedure ListFonts;
const
  QFR_DESCRIPTION  =1;
var
  FileRec : TSearchRec;
  cbBuffer : DWORD;
  lpBuffer: array[0..MAX_PATH-1] of Char;
begin
  if FindFirst('C:\Windows\Fonts\*.*', faNormal, FileRec) = 0 then
  try
    repeat
      cbBuffer:=SizeOf(lpBuffer);
      GetFontResourceInfo(PWideChar('C:\Windows\Fonts\'+FileRec.Name), cbBuffer, lpBuffer, QFR_DESCRIPTION);
      Writeln(Format('%s - %s',[FileRec.Name ,lpBuffer]));
    until FindNext(FileRec) <> 0;
  finally
    FindClose(FileRec);
  end;
end;


begin
  try
   ListFonts;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end. 

关于你的第二个问题替换这一行

  while pEnumList.Next(0, pidChild, b) = 0 do 

  while pEnumList.Next(0, pidChild, celtFetched) = 0 do
于 2012-11-14T16:05:53.493 回答
3

我从德国德尔福论坛得到这个。它适用于 Delphi 7 Enterprise。

function GetFontNameFromFile(FontFile: WideString): string;
type
  TGetFontResourceInfoW = function(Name: PWideChar; var BufSize: Cardinal;
    Buffer: Pointer; InfoType: Cardinal): LongBool; stdcall;
var
  GFRI: TGetFontResourceInfoW;
  AddFontRes, I: Integer;
  LogFont: array of TLogFontW;
  lfsz: Cardinal;
  hFnt: HFONT;
begin
  GFRI := GetProcAddress(GetModuleHandle('gdi32.dll'), 'GetFontResourceInfoW');
  if @GFRI = nil then
    raise Exception.Create('GetFontResourceInfoW in gdi32.dll not found.');

  if LowerCase(ExtractFileExt(FontFile)) = '.pfm' then
    FontFile := FontFile + '|' + ChangeFileExt(FontFile, '.pfb');

  AddFontRes := AddFontResourceW(PWideChar(FontFile));
  try
    if AddFontRes > 0 then
      begin
        SetLength(LogFont, AddFontRes);
        lfsz := AddFontRes * SizeOf(TLogFontW);
        if not GFRI(PWideChar(FontFile), lfsz, @LogFont[0], 2) then
          raise Exception.Create('GetFontResourceInfoW failed.');

        AddFontRes := lfsz div SizeOf(TLogFont);
        for I := 0 to AddFontRes - 1 do
          begin
            hFnt := CreateFontIndirectW(LogFont[I]);
            try
              Result := LogFont[I].lfFaceName;
            finally
              DeleteObject(hFnt);
            end;
          end; // for I := 0 to AddFontRes - 1
      end; // if AddFontRes > 0
  finally
    RemoveFontResourceW(PWideChar(FontFile));
  end;
end;

procedure TMainForm.btnFontInfoClick(Sender: TObject);
begin
  if OpenDialog1.Execute then
    MessageDlg(Format('The font name of %s is'#13#10'%s.', [OpenDialog1.FileName,
      GetFontNameFromFile(OpenDialog1.FileName)]), mtInformation, [mbOK], 0);
end;
于 2013-05-03T14:01:38.327 回答
1

这是对 RRUZ 答案的改编,其好处是您可以枚举并查找任何目录中的字体名称,而不必只是 C:\Windows 中已安装的字体。诀窍是在使用 GetFontResourceInfoW 处理每个字体文件之前(和之后)调用 AddFontResource:

program font_enum;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  Windows,
  System.SysUtils;

const
  QFR_DESCRIPTION = 1;

var
  p: String;
  F: TSearchRec;
  cbBuffer: DWORD;
  lpBuffer: array [0 .. MAX_PATH - 1] of Char;

function GetFontResourceInfo(lpszFilename: PChar; var cbBuffer: DWORD; lpBuffer: PChar; dwQueryType: DWORD): DWORD;
  stdcall; external 'gdi32.dll' name 'GetFontResourceInfoW';

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }

    p := ParamStr(1);

    if (p = EmptyStr) then
      p := ExtractFilePath(ParamStr(0))
    else if (not DirectoryExists(p)) then
    begin
      Writeln('Directory specified is not valid.');
      Exit;
    end;

    p := IncludeTrailingPathDelimiter(p);

    if (FindFirst(p + '*.ttf', faAnyFile - faDirectory, F) = 0) then
    begin

      repeat
        AddFontResource(PWideChar(p + F.Name));

        cbBuffer := SizeOf(lpBuffer);
        GetFontResourceInfo(PWideChar(p + F.Name), cbBuffer, lpBuffer, QFR_DESCRIPTION);
        Writeln(Format('%s = %s', [F.Name, lpBuffer]));

        RemoveFontResource(PWideChar(p + F.Name));

      until (FindNext(F) <> 0);

    end;

    FindClose(F);

    if (FindFirst(p + '*.fon', faAnyFile - faDirectory, F) = 0) then
    begin

      repeat
        AddFontResource(PWideChar(p + F.Name));

        cbBuffer := SizeOf(lpBuffer);
        GetFontResourceInfo(PWideChar(p + F.Name), cbBuffer, lpBuffer, QFR_DESCRIPTION);
        Writeln(Format('%s = %s', [F.Name, lpBuffer]));

        RemoveFontResource(PWideChar(p + F.Name));

      until (FindNext(F) <> 0);

    end;

    FindClose(F);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;

end.
于 2014-08-11T02:53:08.280 回答