Get icon of a folderitem

Hi there!

I’m looking for a way to get the icon of a folderitem as used by the operating system. Unfortunately, I found no cross-platform method (it should work on OS X and Windows) that does not involve some kind of commercial plugins.

I’m not familiar with system operating calls (“declares”), but maybe someone has written a solution and is willing to share it. :slight_smile:

Thanks and have a nice day!
Jimmy

Monkeybread has methods for that. Plugins are a whole lot easier to use than declares :slight_smile:

Thanks; but I can live with declares, since I also want to learn something about programming not how to use black boxes (not meant offending, please excuse).

BTW. I must confess that I find MBS quite overpriced, but that may be my personal opinion only.

I came in here to also say MBS does a great job of solving this.

And now I also want to say that considering what is available in MBS, the fact that you can buy it as a whole or buy individual pieces, and that it’s endlessly updated, it’s more than worth the money. Not starting a flame war, just supporting a harding working community member with my personal opinion. :slight_smile:

Good luck.

To not start a fight here I’ll going to ignore any further replies asking to use commercial plugins. That’s just not my idea of programming nor do I think will it help people to really work productive. I had much trouble in the past as I relied upon others (closed-source) work.

In the meantime I found on macoslib two methods that are doing the job (for all interested in: see MacOSFolderItemExtension in the package on https://github.com/macoslib/macoslib.git . I will now try to understand what these methods do and then use them. Learned something new today. :slight_smile:

However, I’m still looking for a method that works on Windows. I’m currently searching the Windows Functionality Suite on github, but can’t find something. Any help would be appreciated.

[quote=284997:@Jim Fanthers]

I’m looking for a way to get the icon of a folderitem as used by the operating system. Unfortunately, I found no cross-platform method (it should work on OS X and Windows) that does not involve some kind of commercial plugins.

I’m not familiar with system operating calls (“declares”), but maybe someone has written a solution and is willing to share it. :slight_smile:

Thanks and have a nice day!
Jimmy[/quote]

Searching the forums for previous requests like this is one way
https://forum.xojo.com/32412-icon-from-extension
https://forum.xojo.com/30285-retrieve-icon-image-of-files

There is a difference between suggesting and asking. In the end, you do as you please.

You were the one asking for a solution. I merely suggested.

Good luck in your discovery.

For Windows, only executable such as EXE and DLL have icons in them. There is very little available about how to get the icon used by the system for each filetype.

FolderItem.IconMBS is fast and cross platform (Mac+Win)

This should do it for Windows:

Function DefaultIconForFile(File As FolderItem, Size As Integer = 32) As Picture
  #If TargetWin32 Then
    Declare Function SHGetFileInfoW Lib "Shell32" (FilePath As WString, Attribs As Integer, Info As Ptr, InfoSize As Integer, Flags As Integer) As Boolean
    Declare Function DrawIconEx Lib "User32" (hDC As Integer, xLeft As Integer, yTop As Integer, hIcon As Integer, cxWidth As Integer, _
    cyWidth As Integer, StepIfAniCur As Integer, FlickerFreeDraw As Integer, Flags As Integer) As Boolean
    Declare Function DestroyIcon Lib "User32" (hIcon As Integer) As Integer
    Const SHGFI_ICON = &h000000100
    
    Dim info As New MemoryBlock(352) ' a SHFILEINFO structure
    If Not SHGetFileInfoW(File.Name, 0, info, info.Size, SHGFI_ICON) Then Return Nil
    
    Dim ico As New Picture(size, size, 32)
    ico.Transparent = 1
    Dim hIcon As Integer = info.Int32Value(0)
    Call DrawIconEx(ico.Graphics.Handle(1), 0, 0, hIcon, size, size, 0, 0, &h3)
    Call DestroyIcon(hIcon)
    Return ico
  #endif
End Function

no. That’s not doing it.

You need to grab mask and image part separate and recombine them.
Your icons will not have clear edges.

Andrew, I tried your code and found that I needed to make one change. I changed the line

If Not SHGetFileInfoW(File.Name, 0, info, info.Size, SHGFI_ICON) Then Return Nil
to

If Not SHGetFileInfoW(File.NativePath, 0, info, info.Size, SHGFI_ICON) Then Return Nil

so that I could pass in a folderitem loaded anywhere on my system.

SHGetFileInfo only looks at the file extension. Using the FolderItem is just for convenience.

e.g. this gets the default icon for HTML documents:

If Not SHGetFileInfoW("FAKE.html", 0, info, info.Size, SHGFI_ICON) Then Return Nil