Get icon file - problem with SHGetFileInfo

Windows specific forum
User avatar
Le Soldat Inconnu
Enthusiast
Enthusiast
Posts: 306
Joined: Wed Jul 09, 2003 11:33 am
Location: France

Get icon file - problem with SHGetFileInfo

Post by Le Soldat Inconnu »

Hello,

I need a function to get icon 16*16 or 32*32 associated to a file

But the function SHGetFileInfo don't work with all icon on XP
And it's the only function I know

The problem is the function SHgetFileInfo have a problem with the transparency level of the color.

See this example :

I create an icon 32*32 in a file. this icon is a red box this a custom transparency level.
And I display this icon with the function SHGetfileInfo

you can change the transparency level with the trackbar

If the transparency level is >= 128, no problem
But if the transparency level is < 128, the function SHGetFileInfo don't work correctly

I display my icon also with the function ExtractIconEx and I have no problem with this function but it's only work with *.ico, *.exe or *.dll file

Image

Code: Select all

Procedure CreateIcon(Transparency.l)
  ; This procedure create an icon
  If CreateFile(0, "test" + Str(Transparency) + ".ico")
    WriteLong(65536)
    WriteWord(1)
    WriteByte(32)
    WriteByte(32)
    WriteLong(65536)
    WriteWord(32)
    WriteLong(4264)
    WriteLong(22)
    WriteLong(40)
    WriteLong(32)
    WriteLong(64)
    WriteWord(1)
    WriteWord(32)
    WriteLong(0)
    WriteLong(4224)
    WriteLong(0)
    WriteLong(0)
    WriteLong(0)
    WriteLong(0)
    
    For n = 1 To 32
      For nn = 1 To 32
        
        WriteByte(0)
        WriteByte(0)
        WriteByte(255)
        WriteByte(Transparency)
        
      Next
    Next
    
    For n = 1 To 32
      WriteLong(0)
    Next
    
    CloseFile(0)
  EndIf
EndProcedure


; Création de la fenêtre et de la GadgetList
If OpenWindow(0, 0, 0, 500, 300, #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget, "Test") = 0 Or CreateGadgetList(WindowID()) = 0
  End
EndIf

Transparency = 128
CreateIcon(Transparency)

IconPath.s = "test" + Str(Transparency) + ".ico"
SHGetFileInfo_(IconPath, 0, @InfosFile.SHFILEINFO, SizeOf(SHFILEINFO), #SHGFI_ICON)
Icone1 = InfosFile\hIcon
ImageGadget(0, 10, 10, 32, 32, Icone1)
TextGadget(#PB_Any, 10, 50, 100, 50, "SHGetFileInfo")

ExtractIconEx_(IconPath, IconIndex, @Icone2, 0, 1)
ImageGadget(1, 110, 10, 32, 32, Icone2)
TextGadget(#PB_Any, 110, 50, 100, 50, "ExtractIconEx" + Chr(10) + "But there only works with *.ico, *.exe or *.dll")

TrackBarGadget(10, 10, 200, 480, 20, 1, 255)
SetGadgetState(10, Transparency)
TextGadget(11, 10, 230, 290, 15, "icon transparency = " + Str(Transparency))

Repeat
  Event = WaitWindowEvent()
  
  If Event = #PB_EventGadget
    Select EventGadgetID()
      Case 10
        DestroyIcon_(Icone1)
        DestroyIcon_(Icone2)
        DeleteFile("test" + Str(Transparency) + ".ico")
        
        Transparency = GetGadgetState(10)
        TextGadget(11, 10, 230, 290, 15, "icon transparency = " + Str(Transparency))
        
        CreateIcon(Transparency)
        IconPath.s = "test" + Str(Transparency) + ".ico"
        
        SHGetFileInfo_(IconPath, 0, @InfosFile.SHFILEINFO, SizeOf(SHFILEINFO), #SHGFI_ICON)
        Icone1 = InfosFile\hIcon
        SetGadgetState(0, Icone1)
        
        ExtractIconEx_(IconPath, IconIndex, @Icone2, 0, 1)
        SetGadgetState(1, Icone2)
        
        InvalidateRect_(WindowID(), 0, 1)
        UpdateWindow_(WindowID())
        
    EndSelect
  EndIf
  
Until Event = #PB_EventCloseWindow

DestroyIcon_(Icone1)
DestroyIcon_(Icone2)
DeleteFile("test" + Str(Transparency) + ".ico")

End
The window explorer or GadgetExplorer have no problem with my icon.

How can i do the same ? Coule you help me.

thanks for your help (and sorry for mystakes)
LSI
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post by newbie »

I'm not sure to be able to help, but all I know is that with SHGetFileInfo_()
you can specify these two constants to retrieve the 16x16 icon :

Code: Select all

#SHGFI_ICON | #SHGFI_SMALLICON
If nothing works, you can take a look to PrivateExtractIcons_() :
http://msdn.microsoft.com/library/defau ... ticons.asp


The way I personally solved all my icons troubles while displaying them on a ListIconGadget was to create an ImageList, set with a mask of 32x32 in size and 32bits, with a transparent background, and to import the icons in it with SHGetFileInfo_().
- Registered PB user -

Using PB 4.00
User avatar
Le Soldat Inconnu
Enthusiast
Enthusiast
Posts: 306
Joined: Wed Jul 09, 2003 11:33 am
Location: France

Post by Le Soldat Inconnu »

PrivateExtractIcons function wrote:This function extracts from executable (.exe), dynamic-link library (DLL) (.dll), icon (.ico), cursor (.cur), animated cursor (.ani), and bitmap (.bmp) files. Extractions from Microsoft Windows 3.x 16-bit executables (.exe or .dll) are also supported
So it don't work with all file :wink:

I need this function to do create menu with file and icon. And the file can be exe, lnk, doc, etc ...
So I just draw Icon on an image

Image

The icon is a white circle. And the border of the circle have transparency color on XP.
But SHGetFileInfo don't work and return an icon with bug (see the example of the first post)
And I obtain black points on the border of the circle.

How the window explorer do to extract icon associated to a file or How gadgetexplorer do do to extract icon associated to a file ? this two functions work fine with my icon.
LSI
User avatar
Le Soldat Inconnu
Enthusiast
Enthusiast
Posts: 306
Joined: Wed Jul 09, 2003 11:33 am
Location: France

Post by Le Soldat Inconnu »

I find a solution

I have no problem with my icon if I draw my icon directly from the ImageList returned by SHGetFileInfo (I use the function ImageList_Draw_() )
LSI
Post Reply