How to get icon path from file?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
deama
Posts: 11
Joined: 05 Jan 2017, 17:08

How to get icon path from file?

Post by deama » 11 Jun 2020, 16:00

So I'm trying to display an icon onscreen using splash image. I figured out the splash image to display images, but I can't figure out a way to extract icons from files.

So far I got:

Code: Select all

FilePath = C:\Users\Public\Desktop\Dragon Age Origins.lnk
h_icon := DllCall("Shell32\ExtractAssociatedIcon" (A_IsUnicode ? "W" : "A"), ptr, DllCall("GetModuleHandle", ptr, 0, ptr), str, FilePath, "ushort*", lpiIcon, ptr)
Gui, Add, Text, w32 h32 Y0 X48 hwndmypic1 0x3
SendMessage, STM_SETICON := 0x0170, h_icon, 0,, Ahk_ID %mypic1%
Gui, Show
Which looks up at the dragon age origins shortcut I have, gets the icon, and displays it, however it does it for GUI instead of splash image, so I'm trying to figure out how to get it to work for splash image.
What I can't understand though is that apparentely h_icon doesn't return the icon path, instead it returns some integer, I guess that's an object reference or something? How do I get it to just return the icon path instead?

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to get icon path from file?

Post by teadrinker » 11 Jun 2020, 18:56

ExtractAssociatedIcon returns an icon handle, which can be passed to SplashImage like this:

Code: Select all

#Persistent
filePath := A_AhkPath
iconIndex := 0
hIcon := DllCall("Shell32\ExtractAssociatedIcon", "Ptr", 0, "Str", filePath, "ShortP", iconIndex, "Ptr")
SplashImage, HICON:%hIcon%
UPD: fixed the handle link.

deama
Posts: 11
Joined: 05 Jan 2017, 17:08

Re: How to get icon path from file?

Post by deama » 11 Jun 2020, 20:23

Ah, cheers, thanks.

hisrRB57
Posts: 64
Joined: 13 Jan 2019, 11:43

Re: How to get icon path from file?

Post by hisrRB57 » 26 Jan 2022, 11:03

SplashImage, HICON:%hIcon%[/code]

How would you get the icon into a listview?

I have this, but it produces all the same icons:

Code: Select all

Gui, +LastFound +OwnDialogs +Resize					; Create the ListView gui
Gui, Margin, 5, 5
Gui, Add, ListView, -Multi AltSubmit Count10 Grid r20 w1000 gMyListView vListView1, #|ID|Title|Program
ImageListID1 := IL_Create(20,2)		  											; Create an ImageList so that the ListView can display the icons:
LV_SetImageList(ImageListID1, 1)												; Attach the ImageLists to the ListView so that it can later display the icons:

WinGet windows, List
Loop %windows%
{
	id := windows%A_Index%
	WinGetTitle WinT, ahk_id %id%
	WinGet file, ProcessPath, ahk_id %id%
	PName := FileGetVersionInfo_AW(file, "FileDescription")
    iconIndex := 0
    hIcon := DllCall("Shell32\ExtractAssociatedIcon", "Ptr", 0, "Str", file, "ShortP", iconIndex, "Ptr")
    IconNumber := DllCall("ImageList_ReplaceIcon", UInt, ImageListID1, Int, -1, UInt, hIcon) + 1	; Add the HICON directly to the small-icon and large-icon lists.
    LV_Add("", "Icon" . IconNumber, id, Wint, Pname)
}
LV_ModifyCol()  			; Auto-size each column to fit its contents.
LV_ModifyCol(1, 22) 		; resize listview column to show only icon, not number
LV_ModifyCol(2, "Integer")  ; For sorting purposes, indicate that column 1 is an integer.
Gui, Show
return

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to get icon path from file?

Post by teadrinker » 26 Jan 2022, 11:44

Use IL_Add() instead of DllCall("ImageList_ReplaceIcon", .... In LV_Add() the "Icon" . IconNumber is a first parameter, not the second.

hisrRB57
Posts: 64
Joined: 13 Jan 2019, 11:43

Re: How to get icon path from file?

Post by hisrRB57 » 26 Jan 2022, 12:38

teadrinker wrote:
26 Jan 2022, 11:44
Use IL_Add() instead of DllCall("ImageList_ReplaceIcon", .... In LV_Add() the "Icon" . IconNumber is a first parameter, not the second.
Thx, this is what I made:

Code: Select all

WinGet windows, List
Loop %windows%
{
	id := windows%A_Index%
	WinGetTitle WinT, ahk_id %id%
	WinGet file, ProcessPath, ahk_id %id%
	PName := FileGetVersionInfo_AW(file, "FileDescription")
    iconIndex := 0
    hIcon := DllCall("Shell32\ExtractAssociatedIcon", "Ptr", 0, "Str", file, "ShortP", iconIndex, "Ptr")
	IL_Add(ImageListID1, HBITMAP:%hIcon% )
    LV_Add("Icon" . A_Index, id, Wint, Pname)
}
LV_ModifyCol()  			; Auto-size each column to fit its contents.
LV_ModifyCol(1, 22) 		; resize listview column to show only icon, not number
LV_ModifyCol(2, "Integer")  ; For sorting purposes, indicate that column 1 is an integer.
Gui, Show
return
but it errors on the HBITMAP: Missing ")" before ":"

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to get icon path from file?

Post by teadrinker » 26 Jan 2022, 13:34

hisrRB57 wrote:

Code: Select all

IL_Add(ImageListID1, HBITMAP:%hIcon% )
Expression syntax must be used in this case: strings in quotes, variables without percent sings. hIcon obviously is not HBITMAP.

Code: Select all

IconNumber := IL_Add(ImageListID1, "HICON:" . hIcon)

hisrRB57
Posts: 64
Joined: 13 Jan 2019, 11:43

Re: How to get icon path from file?

Post by hisrRB57 » 26 Jan 2022, 13:39

That's it. Thanks very much !!!!

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: How to get icon path from file?

Post by Spitzi » 27 Mar 2022, 02:27

Thank you all for this!! :dance:

Post Reply

Return to “Ask for Help (v1)”