Jump to content

Using _GUICtrlListView_Create with Icon view and detecting Icon click


TimRude
 Share

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))
Next
_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)
Next

; Process GUI messages, looping until the user exits
Do
    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
    EndSwitch
    If $Msg <> "" Then ConsoleWrite($Msg & "  [" & _ArrayToString($aMsg) & "]" & @CRLF)
Until $MsgNum = $GUI_EVENT_CLOSE
GUIDelete()
Exit

 

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()
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

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

;================================================================================
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
                     GUICtrlSendToDummy($cDummy)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

 

Some of my script sourcecode

Link to comment
Share on other sites

  • 2 years later...

How can I detect when the user clicks:

  • In the ListView control, but not on an item?
  • Outside the Listview control but in the window? The user may have clicked on another control or outside all controls.
Edited by c.haslam
Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

image.thumb.png.d13c0d9411e5128c84c591fc9ce1eea8.png

When user enters stuff in e.g. Date, Time and Message then clicks on Set the data appears in a Listview item.

If the user clicks on a ListView item and presses Edit (which disables Set), the data of that item appears in the Date,  Time and Message controls.

minutes advance warning is also available. e.g. if he enters 10 in that control, rather than 0, for the Send Comments item, the Date and Time changes to 20:38 and the Message changes to 20:48

My script has been working for years, but there is/are subtle bugs, so the time has come for a rewrite.

 

 

Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

To detect if the listbox or an item is clicked there is a simple change from my previous script needed:

 

If StringLen($tmptxt) > 0 Then
                If $tmptxt = "0" Then
                    MsgBox(0, "test", "Listview clicked but not on an item")
                Else
                    MsgBox(0, "test", $tmptxt)
                EndIf
            EndIf
Quote

Outside the Listview control but in the window? The user may have clicked on another control or outside all controls.

 

The command GUIGetCursorInfo() can give you the needed information.

 

Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

his is my TO GO snippet for the checkboxes.

 

Func _IsChecked($idControlID)
    ;Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED     ;Returns true or false (oneliner)
    ;The lines below convert true and false to numbers - 1 and 0
    local $x=BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
    If $x = True Then Return 1
    Return 0
EndFunc   ;==>_IsChecked

Func _CheckUncheck($id, $nr)
    If $nr = 0 Then
        GUICtrlSetState($id, $GUI_UNCHECKED)
    Else
        GUICtrlSetState($id, $GUI_CHECKED)
    EndIf
EndFunc   ;==>_CheckUncheck

You can see how the checkboxes are catched in the Help file. 
Basically they function just like normal buttons just their display is different.

 

Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

Is there a way of populating a listview without using a printable character to delineate columns? (Perhaps I should write subitems rather than columns.) The examples appear to use the | character, but what if a cell can contain any keyboard character?

Edited by c.haslam
Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

Not sure what you mean ... 

if you look at the help page for the GUICtrlCreateListView you may notice that it has (just before the example script) a related link section.

If you read it carefully, you may notice a link saying :  GUIDataSeparatorChar (Option) ... which is, what, i think, you are asking for.

BTW: My script is populating the Listview using an array ... 
Then there is a way to add things to the Listview by using the _GUICtrlListView_AddItem() and _GUICtrlListView_AddSubItem()

Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

Thanks.

Looking at the example for _GUICtrlListView_AddSubItem() I see this: if you think of a Listview as a table, with 1-based rows and 1-based columns, an Item is column 1 and a SubItem is any other column. To add column n one has to _GUICtrlListView_InsertColumn for columns 1 to n-1 first.

 

 

Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

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
 Share

  • Recently Browsing   0 members

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