Jump to content

Double click on an item in a list


Recommended Posts

Study this a bit:

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

GUICreate("ListView Test",800,600)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ;this allows you to capture all window messages.

Global $GuiCtrlListView = GUICtrlCreateListView("Column0|Column1|Column2",10,10,700,500)
Global $hListView = GUICtrlGetHandle($GuiCtrlListView) ;we'll need the handle later.
For $i = 0 To 99
    GUICtrlCreateListViewItem("Item0." & $i & "|Item1." & $i & "|Item2." & $i,$GuiCtrlListView)
Next
GUISetState()

While GUIGetMsg() <> -3
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    ;there are a lot of window messages and they fire all the time.
    ;We'll need to filter the ones we want.
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $tInfo
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) ;this should give us the handle of the control that triggered the event.
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hListView ;Only react to messages from the listview
            Switch $iCode ;check the type of message
                Case $NM_DBLCLK ;double click message
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _DebugPrint("$NM_DBLCLK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                        "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                        "-->Code:" & @TAB & $iCode & @LF & _
                        "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @LF & _
                        "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF & _
                        "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @LF & _
                        "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @LF & _
                        "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @LF & _
                        "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @LF & _
                        "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @LF & _
                        "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @LF & _
                        "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                Case $NM_RCLICK ;right click message
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _DebugPrint("$NM_RCLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                        "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                        "-->Code:" & @TAB & $iCode & @LF & _
                        "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @LF & _
                        "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF & _
                        "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @LF & _
                        "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @LF & _
                        "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @LF & _
                        "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @LF & _
                        "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @LF & _
                        "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @LF & _
                        "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
            EndSwitch
    EndSwitch
    ; if you don't return $GUI_RUNDEFMSG, or 0 the rest of the messages won't be handles as they should.
    Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY

Func _DebugPrint($s_text, $line = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @LF & _
            "+======================================================" & @LF & _
            "-->Line(" & StringFormat("%04d", $line) & "):" & @TAB & $s_text & @LF & _
            "+======================================================" & @LF)
EndFunc ;==>_DebugPrint

Func _Exit()
    Exit
EndFunc

Looks like some of that isn't my code (at least _DebugPrint doesn't look like something I'd write), but I found it in another example script in my test folder, so I don't know the author. Thanks though!

This is just the code that captures the events. When you write the functions to handle them, keep in mind that WM_NOTIFY is supposed to return quickly, so if you plan on triggering a time-consuming function, set a flag in WM_NOTIFY that triggers the function after WM_NOTIFY returns.

Link to comment
Share on other sites

_DebugPrint() is from the HelpFile ;)

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

_DebugPrint() is from the HelpFile ;)

Well then the helpfile deserves credit! ;)

works with a listview but not on my listbox :)

I might have gotten a little ListView obsessed the last few days because of a script I'm making. I could have sworn you asked for a ListView.

I have tried adjusting the code to work for a listbox instead, but it doesn't seem to trigger any WM_NOTIFY messages at all. There are some message codes specified in ListBoxConstants.au3 though. I'll try a few things if and when I have some spare time, but I'm pretty much stabbing in the dark.

Edit:

Actually the example in the helpfile shows an example capturing WM_COMMAND which triggers when an item is double clicked, but it appears the wParam and lParam don't hold indformation about the item clicked. I think I'm out of my league.

Edited by Tvern
Link to comment
Share on other sites

Try this:

#include <GUIConstantsEx.au3>


#include <GUIListBox.au3>
#include <WindowsConstants.au3>


GUICreate("Form1", 200, 350)
$hListBox = GUICtrlCreateList("", 5, 5, 190, 340)
Local $t
For $i=1 To 10
    $t &="Item no."&$i&"|"
Next
GUICtrlSetData(-1,$t)
$mnuLBContext = GUICtrlCreateContextMenu($hListBox)
GUICtrlCreateMenuItem("MenuItem3", $mnuLBContext)
GUICtrlCreateMenuItem("MenuItem2", $mnuLBContext)
GUICtrlCreateMenuItem("MenuItem1", $mnuLBContext)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

GUISetState(@SW_SHOW)


While 1
    Sleep(10)
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode, $hWndListBox
    If Not IsHWnd($hListBox) Then $hWndListBox = GUICtrlGetHandle($hListBox)
    $hWndFrom = $ilParam
    $iIDFrom = BitAND($iwParam, 0xFFFF) ; Low Word
    $iCode = BitShift($iwParam, 16) ; Hi Word

    Switch $hWndFrom
        Case $hListBox, $hWndListBox
            Switch $iCode
                Case $LBN_DBLCLK ; Sent when the user double-clicks a string in a list box
                    ConsoleWrite("Item "& _GUICtrlListBox_GetCurSel($hListBox)&@CRLF)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND
Link to comment
Share on other sites

omg thx it works but uhm it only works for 1 listbox not 2 :)

maybe you know how to fix it ;) i don't have a clue xD

Tvern thx for your help also ^^

Case $list1
            GUIRegisterMsg($WM_COMMAND, "WM_COMMAND1")
        Case $list2
            GUIRegisterMsg($WM_COMMAND, "WM_COMMAND2")

Func WM_COMMAND1($hWnd1, $iMsg1, $iwParam1, $ilParam1)
    #forceref $hWnd1, $iMsg1
    Local $hWndFrom1, $iIDFrom1, $iCode1, $hWndListBox1
    If Not IsHWnd($list1) Then $hWndListBox1 = GUICtrlGetHandle($list1)
    $hWndFrom1 = $ilParam1
    $iIDFrom1 = BitAND($iwParam1, 0xFFFF) ; Low Word
    $iCode1 = BitShift($iwParam1, 16) ; Hi Word
    Switch $hWndFrom1
        Case $list1, $hWndListBox1
            Switch $iCode1
                Case $LBN_DBLCLK ; Sent when the user double-clicks a string in a list box
                    $id = WinGetState(_GUICtrlListBox_GetText($list1, _GUICtrlListBox_GetCurSel($list1)))
                    If $id = "5" Or $id = "37" Then
                        WinSetState(_GUICtrlListBox_GetText($list1, _GUICtrlListBox_GetCurSel($list1)), "", @SW_SHOW)
                    ElseIf $id = "7" Or $id = "39" Then
                        WinSetState(_GUICtrlListBox_GetText($list1, _GUICtrlListBox_GetCurSel($list1)), "", @SW_HIDE)
                    Else
                        WinSetState(_GUICtrlListBox_GetText($list1, _GUICtrlListBox_GetCurSel($list1)), "", @SW_SHOW)
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND1

Func WM_COMMAND2($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode, $hWndListBox
    If Not IsHWnd($list2) Then $hWndListBox = GUICtrlGetHandle($list2)
    $hWndFrom = $ilParam
    $iIDFrom = BitAND($iwParam, 0xFFFF) ; Low Word
    $iCode = BitShift($iwParam, 16) ; Hi Word
    Switch $hWndFrom
        Case $list2, $hWndListBox
            Switch $iCode
                Case $LBN_DBLCLK ; Sent when the user double-clicks a string in a list box
                    $id = WinGetState(_GUICtrlListBox_GetText($list2, _GUICtrlListBox_GetCurSel($list2)))
                    If $id = "5" Or $id = "37" Then
                        WinSetState(_GUICtrlListBox_GetText($list2, _GUICtrlListBox_GetCurSel($list2)), "", @SW_SHOW)
                    ElseIf $id = "7" Or $id = "39" Then
                        WinSetState(_GUICtrlListBox_GetText($list2, _GUICtrlListBox_GetCurSel($list2)), "", @SW_HIDE)
                    Else
                        WinSetState(_GUICtrlListBox_GetText($list2, _GUICtrlListBox_GetCurSel($list2)), "", @SW_SHOW)
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND2

thankyou guys

Edited by tommeke228
Link to comment
Share on other sites

You register the WM_COMMAND message for all controls in the GUI, so you only have to register it once, after which you can check in the function what listbox triggered the event. ($hWndFrom contains the handle to the control triggering the event)

Link to comment
Share on other sites

  • 12 years later...
  • Developers

Ok then I won't complain anymore and simply lock it. 

... but I did create a new topic for you this time! ;)

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Jos locked this topic
Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

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