如何获取桌面图标?

时间:2020-02-07 16:39:26

标签: delphi winapi

我想从某些系统目录中获取图标,例如“桌面”。但是,相反,我得到的是“默认文件”的图标。

function GetSpecialFolderPath(CSIDLFolder: Integer): string;
var  FilePath: array [0..MAX_PATH] of char;
begin
  SHGetFolderPath(0, CSIDLFolder, 0, 0, FilePath);
  Result := FilePath;
end;

function GetSystemFileIcon(FolderID: Integer): TIcon;
var
  AInfo: TSHFileInfo;
  AIcon: TIcon;
  Ext: PAnsiChar;
  Path: String;
begin
  Path := GetSpecialFolderPath(FolderID);

  if Path = '' then begin
    Result := nil;
    Exit;
  end;  

  if SHGetFileInfo(PChar(Path), FILE_ATTRIBUTE_NORMAL, AInfo, SizeOf( AInfo ),
    SHGFI_ICON or SHGFI_LARGEICON or SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES or SHGFI_OPENICON ) <> 0 then
  begin
    AIcon := TIcon.Create;
    try
      AIcon.Handle := AInfo.hIcon;
      Result := AIcon;
    except
      AIcon.Free;
      raise;
    end;
  end
  else
    Result := nil;
end;

var Ico: TIcon;
begin
  Ico :=  GetSystemFileIcon(CSIDL_DESKTOPDIRECTORY);

1 个答案:

答案 0 :(得分:1)

此属性需要删除:

SHGFI_USEFILEATTRIBUTES

然后就可以了。