Get Icon in Listview

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Get Icon in Listview

15 May 2019, 00:40

Hi all I checked out the documentation on how to add icons to a listview. I've borrowed the coding from the docs and changed it up to get the icons in there. What I am trying to do:

When left-clicking on any of the 3 rows, I am trying to have the messagebox return the variable associated with the icon on that particular row.

But, I can't figure out how to assign a variable to the icon.

So for example if you click on the first row (which is the regedit icon), the messagebox should say "you clicked on Row1, the text is %description% and the variable associated with the icon is regedit"

..or something like that.

I hope this makes sense. Here's what I've got so far:

Code: Select all

Gui, Add, ListView, Altsubmit gClick vMyListView  h200 w180 , Icon & Number|Description  ; Create a ListView.
ImageListID := IL_Create(3)  ; Create an ImageList to hold 3 small icons.
LV_SetImageList(ImageListID)  ; Assign the above ImageList to the current ListView.

IL_Add(ImageListID gClick vregedit, "C:\Windows\regedit.exe", regedit) 

IL_Add(ImageListID gClick vnotepad, "C:\Windows\notepad.exe", notepad) 

IL_Add(ImageListID gClick vexplorer, "C:\Windows\explorer.exe", explorer)

Loop 3  ; Add rows to the ListView (for demonstration purposes, one for each icon).
    LV_Add("Icon" . A_Index, A_Index, "description")
LV_ModifyCol("Hdr")  ; Auto-adjust the column widths.

Gui Show
return


GuiClose:  ; Exit the script when the user closes the ListView's GUI window.
ExitApp
return


Click:
if A_GuiEvent = Normal
{

    LV_GetText(RowText1, A_EventInfo, 1) ; Get the text of the first field.
    LV_GetText(RowText2, A_EventInfo, 2)  ; Get the text of the second field
msgbox, the text in the first column is %RowText1% and the text in the second column is %RowText2%
}
Return
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get Icon in Listview

15 May 2019, 05:26

Code: Select all

IL_Add(ImageListID, Filename [, IconNumber, ResizeNonIcon])
Where did you find your syntax?

Code: Select all

IL_Add(ImageListID gClick vregedit, "C:\Windows\regedit.exe", regedit)
You can retrieve the 0-based index of an item's icon in the ListView's ImageList with LVM_GETITEM.
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: Get Icon in Listview

15 May 2019, 06:58

scriptor2016 wrote:
15 May 2019, 00:40
I can't figure out how to assign a variable to the icon.
It doesn't make sense. If you need to associate any information with LV rows, they should be in an array.

If you really need to identify the icon in a ListView, for example: to move items up/down, this is the function:

Code: Select all

LV_GetIconIndex(hLV, Row) {
    VarSetCapacity(LVITEM, A_PtrSize == 8 ? 88 : 60, 0)
    NumPut(0x2, LVITEM, 0, "UInt") ; mask (LVIF_IMAGE)
    NumPut(Row - 1, LVITEM, 4, "Int") ; iItem
    SendMessage 0x104B, 0, &LVITEM,, ahk_id %hLV% ; LVM_GETITEMW
    Return ErrorLevel != "FAIL" ? NumGet(LVITEM, A_PtrSize == 8 ? 36 : 28, "Int") + 1 : 0
}
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Get Icon in Listview

15 May 2019, 08:39

hmmm I think I should have asked this differently, I have it all mixed up

My script will read a text file which contains hundreds of items and load the information into a listview. The text file looks something like:

me
her
him
them
him
me
her
them
me
us
us

...etc. Each line contains something different. Sometimes a line will be duplicated elsewhere in the text file.

There will be 3 icons. Each row in the listview will have one of the 3 icons in the first column. The second column will contain a line in the text file. The icon could be any one of the 3, depending on the content of the current line in the text file. (or, it could all be in the same column if it's possible)

man, I hope this is making sense.

So, it will be something like this:

if %current row text% = her then icon = %regedit%
if %current row text% = him then icon = %notepad%
if %current row text% = us then icon = %explorer%
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Get Icon in Listview

15 May 2019, 10:25

I'm getting somewhere now. The listview properly shows the first row along with its icon (regedit).

But the second row is showing the incorrect icon (it's showing a printer icon instead of the notepad icon) and the third row is showing no icon at all.

Code: Select all

Gui, Add, ListView, Altsubmit gClick vMyListView  h200 w180 , Icon & Number|Description  ; Create a ListView.
ImageListID := IL_Create(3)  ; Create an ImageList to hold 3 small icons.
LV_SetImageList(ImageListID)  ; Assign the above ImageList to the current ListView.

IL_Add(ImageListID, "C:\Windows\regedit.exe", 1) ;is this icon now assigned as variable 1?
IL_Add(ImageListID, "C:\Windows\notepad.exe", 2) ;is this icon now assigned as variable 2?
IL_Add(ImageListID, "C:\Windows\explorer.exe", 3) ;is this icon now assigned as variable 3?

LV_Add("Icon" . 1, 1, "regedit")
LV_Add("Icon" . 2, 2, "notepad")
LV_Add("Icon" . 3, 3, "explorer")
LV_ModifyCol("Hdr")  ; Auto-adjust the column widths.

Gui Show
return

GuiClose:  ; Exit the script when the user closes the ListView's GUI window.
ExitApp
return

Click:
if A_GuiEvent = Normal
{
    LV_GetText(RowText1, A_EventInfo, 1) ; Get the text of the first column.
    LV_GetText(RowText2, A_EventInfo, 2)  ; Get the text of the second column
msgbox, the text in the first column is %RowText1% and the text in the second column is %RowText2%
}
Return
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: Get Icon in Listview

15 May 2019, 13:01

The third parameter of IL_Add() is the icon index, not the index they will assume in the image list.

Code: Select all

; Generated by AutoGUI 2.5.8
#SingleInstance Force
#NoEnv
SetWorkingDir %A_ScriptDir%
SetBatchLines -1

Gui Font, s9, Segoe UI
Gui Add, ListView, hWndhLVItems vLVItems gLVHandler x8 y8 w604 h403 +LV0x14000 +AltSubmit, Icon & Number|Description

ImageListID := IL_Create(3, 3, 0)
LV_SetImageList(ImageListID)

IL_Add(ImageListID, "C:\Windows\regedit.exe")
IL_Add(ImageListID, "C:\Windows\notepad.exe")
IL_Add(ImageListID, "C:\Windows\explorer.exe")

LV_Add("Icon" . 1, 1, "regedit")
LV_Add("Icon" . 2, 2, "notepad")
LV_Add("Icon" . 3, 3, "explorer")
LV_ModifyCol("Hdr")

DllCall("UxTheme.dll\SetWindowTheme", "Ptr", hLVItems, "WStr", "Explorer", "Ptr", 0)

Gui Show, w620 h420, ListView Icons
Return

LVHandler:
    If (A_GuiEvent == "Normal") {
        LV_GetText(RowText1, A_EventInfo, 1)
        LV_GetText(RowText2, A_EventInfo, 2)

        Icon := LV_GetIconIndex(hLVItems, A_EventInfo)

        Gui +OwnDialogs
        MsgBox 0x40, LV Item, First column text:  "%RowText1%"`nSecond column text:  "%RowText2%"`nImage list icon index:  %Icon%
    }
Return

GuiEscape:
GuiClose:
    ExitApp

LV_GetIconIndex(hLV, Row) {
    VarSetCapacity(LVITEM, A_PtrSize == 8 ? 88 : 60, 0)
    NumPut(0x2, LVITEM, 0, "UInt") ; mask (LVIF_IMAGE)
    NumPut(Row - 1, LVITEM, 4, "Int") ; iItem
    SendMessage 0x104B, 0, &LVITEM,, ahk_id %hLV% ; LVM_GETITEMW
    Return ErrorLevel != "FAIL" ? NumGet(LVITEM, A_PtrSize == 8 ? 36 : 28, "Int") + 1 : 0
}
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Get Icon in Listview

16 May 2019, 00:25

thanks Alguimist, that answers some of my questions - that the 3rd parameter is the icon index and it looks like the 2nd parameter is the text beside the icon. I will continue on with this one :)
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get Icon in Listview

16 May 2019, 03:05

scriptor2016 wrote:... that the 3rd parameter is the icon index and it looks like the 2nd parameter is the text beside the icon.
Are you talking about IL_Add?

Code: Select all

IL_Add(ImageListID, Filename [, IconNumber, ResizeNonIcon])
Filename is the name of an icon (.ICO), cursor (.CUR), or animated cursor (.ANI) file (animated cursors will not actually be animated when displayed in a ListView). Other sources of icons include the following types of files: EXE, DLL, CPL, SCR, and other types that contain icon resources. To use an icon group other than the first one in the file, specify its number for IconNumber. If IconNumber is negative, its absolute value is assumed to be the resource ID of an icon within an executable file. In the following example, the default icon from the second icon group would be used: IL_Add(ImageListID, "C:\My Application.exe", 2).
ImageLists don't store names or other textual content.
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Get Icon in Listview

16 May 2019, 08:37

I had come up with an icon as an example to add to a listview, but it could be an image as well like a .jpeg, .gif or .png for example. I'm not sure why I thought it had to be an icon. Is there any difference between using an icon or an image file? Would it be easier to just use an image?
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get Icon in Listview

16 May 2019, 10:25

IL_Add() wrote:Non-icon images such as BMP, GIF and JPG may also be loaded. However, in this case the last two parameters should be specified to ensure correct behavior: IconNumber should be the mask/transparency color number (0xFFFFFF [the color white] might be best for most pictures); and ResizeNonIcon should be non-zero to cause the picture to be scaled to become a single icon, or zero to divide up the image into however many icons can fit into its actual width.

All operating systems support GIF, JPG, BMP, ICO, CUR, and ANI images. On Windows XP or later, additional image formats such as PNG, TIF, Exif, WMF, and EMF are supported. Operating systems older than XP can be given support by copying Microsoft's free GDI+ DLL into the AutoHotkey.exe folder (but in the case of a compiled script, copy the DLL into the script's folder). To download the DLL, search for the following phrase at www.microsoft.com: gdi redistributable
What are you missing?
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: Get Icon in Listview

16 May 2019, 14:48

just an example , see example black.ico
with irfanview can save picture as xy.ico ( example here black.ico is only a small copied black.jpg saved as black.ico)

Code: Select all

#warn
#noenv
Setworkingdir,%a_scriptdir%
Gui,2:default
Gui,2: -DPIScale
Gui,2: Font,s14 CBlack,Lucida Console
e4x=
(Ltrim Join`r`n
VOLUME;Sndvol.exe
CHARMAP;Charmap.exe
Black;myblack.ahk
Notepad;Notepad.exe
mozilla;%a_programfiles%\Mozilla Firefox\Firefox.exe
)
   Gui,2:Add, ListView, backgroundGray  checked grid x10 y5  h360 w600 +hscroll altsubmit vMLV1A gMLV1B      , Icon|Program
   gosub,lb
gui,2:show, x100 y10 h380 w630,LV-Icon / Click on column-2 to start
return
2Guiclose:
exitapp
lb:
Gui,2:Submit,nohide
Gui,2:ListView,mlv1a
t1:=280
t2:=300
   LV_ModifyCol(1,T1)
   LV_ModifyCol(2,T2)
  ;ILStatus := IL_Create(1,1,0)                ;small
   ILStatus := IL_Create(1,1,1)                ;bigger
   LV_SetImageList(ILStatus, 1)
LV_Delete()
GuiControl,2: -Redraw,MLV1a

;---  for test -----------
R3I=C:\M_ICO
XXA:=% R3I . "\black.ico"
;-------------------------

loop,parse,e4x,`n,`r
  {
  x:=a_loopfield
  c1=
  c2=
  IconNumber := IL_Add(ILSTATUS, "%pictxx05%" )      ;- clear with picture which not exist
  stringsplit,C,x,`;,
  SplitPath,c2, name, dir, ext, name_no_ext, drive
  IconNumber := IL_Add(ILSTATUS, C2 )

  if C1=black
    IconNumber := IL_Add(ILSTATUS, XXA )

  LV_Add("icon" . IconNumber ,C1,name_no_ext)
  }
GuiControl,2: +Redraw,MLV1a
LV_ModifyCol(1,"right")     ;- move text to right
return
;-----------------------------------------------------------------
mlv1b:
Gui,2:submit,nohide
Gui,2:ListView, mlv1a
t1a:=(t1+20)
If A_GuiEvent = Normal
   {
    MouseGetPos,x,y
       {
       if x<%T1%
          return
       if x>%T1a%
          {
          LV_GetText(C2,A_EventInfo,2)
          ;msgbox, 262208, C2,%c2%
          try 
          run,%c2%
          return
          }
       }
}
return
;==============================================
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Get Icon in Listview

17 May 2019, 09:11

thanks for all the help - this is getting closer. This listview reads a text file and depending on each line in the text file, it will display the corresponding icon/image.

This is what the text file looks like (just as an example):


explorer
regedit
explorer
notepad
explorer
regedit
regedit
explorer
explorer
notepad
explorer
regedit


So the code will read that text file above, and the first row in the listview would display the explorer icon because the first line in the text file is "explorer", the second row will display the regedit icon because the second line in the text file is "regedit", the third row the explorer, the fourth row the notepad, the fifth row the explorer, etc.

Code: Select all

Gui Font, s9, Segoe UI
Gui Add, ListView, hWndhLVItems vLVItems gLVHandler x8 y8 w604 h403 +LV0x14000 +AltSubmit, Program Title 

Loop, read, C:\MyTextFile.txt
{
StringSplit, item, A_LoopReadLine, `,
LV_Add("",lastpart,item2)

ImageListID := IL_Create(3, 3, 0)
LV_SetImageList(ImageListID)
IL_Add(ImageListID, "C:\Windows\regedit.exe")
IL_Add(ImageListID, "C:\Windows\notepad.exe")
IL_Add(ImageListID, "C:\Windows\explorer.exe")

if A_LoopReadLine = regedit
LV_Add("Icon" . 1, A_LoopReadLine) ;add the regedit icon to this row in the listview

else if A_LoopReadLine = notepad
LV_Add("Icon" . 2, A_LoopReadLine) ;add the notepad icon to this row in the listview

else if A_LoopReadLine = explorer
LV_Add("Icon" . 3, A_LoopReadLine) ;add the explorer icon to this row in the listview
}
Gui Show, w620 h420, ListView Icons
Return

LVHandler:
    If (A_GuiEvent == "Normal") {
        LV_GetText(RowText1, A_EventInfo, 1)
        LV_GetText(RowText2, A_EventInfo, 2)

        Icon := LV_GetIconIndex(hLVItems, A_EventInfo)

        Gui +OwnDialogs
        MsgBox 0x40, LV Item, First column text:  "%RowText1%"`nImage list icon index:  %Icon%
    }
Return

GuiEscape:
GuiClose:
    ExitApp

LV_GetIconIndex(hLV, Row) {
    VarSetCapacity(LVITEM, A_PtrSize == 8 ? 88 : 60, 0)
    NumPut(0x2, LVITEM, 0, "UInt") ; mask (LVIF_IMAGE)
    NumPut(Row - 1, LVITEM, 4, "Int") ; iItem
    SendMessage 0x104B, 0, &LVITEM,, ahk_id %hLV% ; LVM_GETITEMW
    Return ErrorLevel != "FAIL" ? NumGet(LVITEM, A_PtrSize == 8 ? 36 : 28, "Int") + 1 : 0
}
It's very close now, the only problem is that the listview is adding a regedit icon (but with no text) in between each row.

If I could just get rid of those additional rows this will be gold :)
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: Get Icon in Listview

17 May 2019, 10:20

not sure ... an example

Code: Select all

#warn
setworkingdir,%a_scriptdir%
e1=
(Ltrim join`r`n
explorer,expl1
regedit,reged
explorer,expl2
notepad,notepad
explorer,expl3
regedit,reged2
regedit,reged3
explorer,expl4
explorer,expl5
notepad,notepad
explorer,expl1
regedit,reged4
)

Gui Font,s14 cYellow,Lucida Console
Gui Add, ListView, hWndhLVItems vLVItems gLVHandler x5 y5 w604 h500 +LV0x14000 backgroundblack grid +AltSubmit, Program|Title 
t1:=100
t2:=150
   LV_ModifyCol(1,T1)
   LV_ModifyCol(2,T2)
;ILSt:= IL_Create(1,1,0)                ;- small
ILSt := IL_Create(1,1,1)                ;- bigger
LV_SetImageList(ILSt, 1)
IL_Add(ILSt, "C:\Windows\regedit.exe")
IL_Add(ILSt, "C:\Windows\notepad.exe")
IL_Add(ILSt, "C:\Windows\explorer.exe")
loop,parse,e1,`n,`r
{
StringSplit,C, A_LoopField, `,
if C1 = regedit
  LV_Add("Icon" . 1,, C2) ;- add the regedit  icon to this row in the listview
else if C1 = notepad
  LV_Add("Icon" . 2,, C2) ;- add the notepad  icon to this row in the listview
else if C1 = explorer
  LV_Add("Icon" . 3,, C2) ;- add the explorer icon to this row in the listview
}
Gui Show, x210 y10 w620 h520, ListView Icons
Return
;---------
LVHandler:
    If (A_GuiEvent == "Normal") {
        LV_GetText(RowText1, A_EventInfo, 1)
        LV_GetText(RowText2, A_EventInfo, 2)
        Icon := LV_GetIconIndex(hLVItems, A_EventInfo)
        try
        run,%RowText2%
        msgbox, 262208,LV_ITEM ,First column Icon=%Icon%`nSecond column text:  "%RowText2%"
    }
Return
;------------
GuiEscape:
GuiClose:
ExitApp
;------------
LV_GetIconIndex(hLV, Row) {
    VarSetCapacity(LVITEM, A_PtrSize == 8 ? 88 : 60, 0)
    NumPut(0x2, LVITEM, 0, "UInt") ; mask (LVIF_IMAGE)
    NumPut(Row - 1, LVITEM, 4, "Int") ; iItem
    SendMessage 0x104B, 0, &LVITEM,, ahk_id %hLV% ; LVM_GETITEMW
    Return ErrorLevel != "FAIL" ? NumGet(LVITEM, A_PtrSize == 8 ? 36 : 28, "Int") + 1 : 0
}
;========================================
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Get Icon in Listview

18 May 2019, 00:17

hi garry yes thanks it works... it helped me complete my script. I was initially trying to just change the color of the font on each individual row in the listview depending on the text file it was reading (for example if the A_LoopReadLine in the text file was "explorer", then instead of displaying the explorer icon in the listview, the row's font would be yellow and it would just say "explorer"- with no icon. And if the A_LoopReadLine in the text was was "regedit", then the row's text color would be cyan and the text would say "regedit", and also no icon... and so on. But as I searched for a solution, I sort of concluded that it couldn't be done (in a simple way), so adding an icon beside the text instead was a second option - and it works!!! Thanks again :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: gongnl, Google [Bot], RandomBoy, Rohwedder and 353 guests