Jump to content

Using _GUICtrlListView_Create with Icon view and detecting Icon click


Go to solution Solved by Dan_555,

Recommended Posts

The code below displays the beginnings of a GUI with a ListView populated with 14 boxes (pseudo thumbnails), and the user can click and select only one icon at a time. However, I don't see an easy way to identify (from the GUIGetMsg function) when an item in my ListView has been clicked. All of the click messages just return the handle of the GUI, nowhere does it return the handle of the ListView control.

What vital piece of the puzzle am I missing so that I can know when the user has clicked (anywhere) on the ListView? Since I limit the user to only selecting a single ListView item at a time, I can easily figure out which ListView item is selected if any (by using _GUICtrlListView_GetSelectedIndices) once I know the ListView has been clicked.

I suppose I can maybe look at the mouse X & Y position returned in the array by the GUIGetMsg(1) call and then keep careful track of how the ListView is positioned and sized to see if the click fell inside that area, but is that really how this is normally done? Surely there has to be a better way of signaling a click on a ListView, isn't there? :huh:

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

; Create GUI with a ListView control
Global $hGUI = GUICreate("ListView", 700, 500, -1, -1, $WS_OVERLAPPEDWINDOW)
Global $hListView = _GUICtrlListView_Create($hGUI, "", 2, 2, 630, 420, BitOR($LVS_ICON, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS))
GUISetState(@SW_SHOW, $hGUI)

; Create and load ImageList control with psuedo thumbnails
Global $SizeX = 160 ; width of each thumbnail
Global $SizeY = 100 ; height of each thumbnail
Global $hImgLst = _GUIImageList_Create($SizeX, $SizeY)
Local $BoxColor = 0xFF0000
For $i = 0 To 13 ; create 14 pseudo thumbnails (boxes of varying colors)
    _GUIImageList_Add($hImgLst, _GUICtrlListView_CreateSolidBitMap($hListView, $BoxColor - ($i * 0x139d76), $SizeX, $SizeY))
_GUICtrlListView_SetImageList($hListView, $hImgLst, 0)

; Add the thumbnail images as items to the ListView
For $i = 0 To 13
    _GUICtrlListView_AddItem($hListView, "Item " & $i, $i)

; Process GUI messages, looping until the user exits
    Local $Msg = ""
    Local $aMsg = GUIGetMsg(1)
    Local $MsgNum = $aMsg[0]
    Switch $MsgNum
        Case 0
            $Msg = "" ; $GUI_EVENT_NONE
        Case -3
            $Msg = "$GUI_EVENT_CLOSE"
        Case -4
            $Msg = "$GUI_EVENT_MINIMIZE"
        Case -5
            $Msg = "$GUI_EVENT_RESTORE"
        Case -6
            $Msg = "$GUI_EVENT_MAXIMIZE"
        Case -7
            $Msg = "$GUI_EVENT_PRIMARYDOWN"
        Case -8
            $Msg = "$GUI_EVENT_PRIMARYUP"
        Case -9
            $Msg = "$GUI_EVENT_SECONDARYDOWN"
        Case -10
            $Msg = "$GUI_EVENT_SECONDARYUP"
        Case -11
            $Msg = "" ; $GUI_EVENT_MOUSEMOVE
        Case -12
            $Msg = "$GUI_EVENT_RESIZED"
        Case -13
            $Msg = "$GUI_EVENT_DROPPED"
        Case Else
            $Msg = $MsgNum
    If $Msg <> "" Then ConsoleWrite($Msg & "  [" & _ArrayToString($aMsg) & "]" & @CRLF)
Until $MsgNum = $GUI_EVENT_CLOSE


Link to comment
Share on other sites

  • Solution

I had a code which needed a doubleclick on the LW to work.

Here is a modified code to use the single click:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Local $test[5][2] = [['.au3', 'AutoIt'], ['.ahk', 'Auto Hotkey'], ['.txt', 'text'],['.sdlbas', 'Sdl Basic'], ['.html', 'Webpage']]
$Form1 = GUICreate("Create New File", 210, 307, -1,-1, $WS_CAPTION , $WS_EX_TOOLWINDOW)
$List = GUICtrlCreateListView("", 5,5, 200, 200)
_GUICtrlListView_InsertColumn($List, 0, "Extension", 80)
_GUICtrlListView_InsertColumn($List, 1, "Name", 130)
_GUICtrlListView_AddArray($List, $test)

$Button1 = GUICtrlCreateButton("Ok", 16, 214, 45, 26)
$Button2 = GUICtrlCreateButton("Cancel", 140, 214, 45, 26)
$cDummy = GUICtrlCreateDummy()


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $Button2
        Case $Button1, $cDummy
            local $tmptxt=StringSplit(_GUICtrlListView_GetItemTextString($List), "|")[1]
            if StringLen($tmptxt)>0 then
                MsgBox(0, "test", $tmptxt)

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $List
    If Not IsHWnd($List) Then $hWndListView = GUICtrlGetHandle($List)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK



Some of my script sourcecode

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...