Get Icon For File *updated*

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Get Icon For File *updated*

14 Oct 2018, 16:24

I was looking how to add icons to my listviews and found this old forum post: https://autohotkey.com/board/topic/3335 ... -for-file/

it didn't work, but Cerpin pointed out that Str should be AStr, so here's code that works :) (for V1 and V2):

Code: Select all

Loop %A_Programs%\*.*
{
	FileName := A_LoopFileFullPath
	hIcon := DllCall("Shell32\ExtractAssociatedIconA", UInt, 0, AStr, FileName, UShortP, iIndex)
	if hIcon
	{
		DllCall("ImageList_ReplaceIcon", UInt, ImageListID1, Int, -1, UInt, hIcon)
		IconNumber := DllCall("ImageList_ReplaceIcon", UInt, ImageListID1, Int, -1, UInt, hIcon) + 1
		DllCall("DestroyIcon", Uint, hIcon)
	}
	else
		IconNumber := 1
	LV_Add("Icon" . IconNumber, A_LoopFileName)
}
and here's an example for V1

Code: Select all

Gui, Margin, 0, 0
Gui, Add, ListView, r20 w500, Name
ImageListID1 := IL_Create(10)
LV_SetImageList(ImageListID1)  
Loop %A_Programs%\*.*
{
	FileName := A_LoopFileFullPath
	hIcon := DllCall("Shell32\ExtractAssociatedIconA", UInt, 0, AStr, FileName, UShortP, iIndex)
	if hIcon
	{
		DllCall("ImageList_ReplaceIcon", UInt, ImageListID1, Int, -1, UInt, hIcon)
		IconNumber := DllCall("ImageList_ReplaceIcon", UInt, ImageListID1, Int, -1, UInt, hIcon) + 1
		DllCall("DestroyIcon", Uint, hIcon)
	}
	else
		IconNumber := 1
	LV_Add("Icon" . IconNumber, A_LoopFileName)
}
Gui, Show
and an example for V2

Code: Select all

Gui := GuiCreate()
Gui.MarginX := 0
Gui.MarginY := 0
LV := Gui.Add("ListView", "r20 w500", "Name")
ImageListID1 := IL_Create(10) 
LV.SetImageList(ImageListID1)
Loop Files A_Programs "\*.*"
{
	FileName := A_LoopFileFullPath
	hIcon := DllCall("Shell32\ExtractAssociatedIconA", UInt, 0, AStr, FileName, UShortP, iIndex)
	if hIcon
	{
		DllCall("ImageList_ReplaceIcon", UInt, ImageListID1, Int, -1, UInt, hIcon)
		IconNumber := DllCall("ImageList_ReplaceIcon", UInt, ImageListID1, Int, -1, UInt, hIcon) + 1
		DllCall("DestroyIcon", Uint, hIcon)
	}
	else
		IconNumber := 1
	LV.Add("Icon" . IconNumber, A_LoopFileName)
}
Gui.Show()
sasee_sw
Posts: 5
Joined: 31 Mar 2023, 05:41

Re: Get Icon For File *updated*

21 Jul 2023, 04:48

Maestr0 wrote:
14 Oct 2018, 16:24
[Mod edit: Removed huge unnecessary quote.]

That's awesome man 😃

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 126 guests