Tom
Tom

Reputation: 3042

How to get icon of a Desktop?

I want to get icons from some system directories, like "Desktop". However instead I am getting an icon for "default file".

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);

Upvotes: 1

Views: 196

Answers (1)

Tom
Tom

Reputation: 3042

This attribute needs to be removed:

SHGFI_USEFILEATTRIBUTES

then it works just fine.

Upvotes: 1

Related Questions