Sign in to follow this  
Followers 0
tommeke228

Double click on an item in a list

8 posts in this topic

I want to know the code to let my program react on a double mouse click on an item in the listbox

and uh the code for if you click right that a menu shows

hmm that should be all ^^

hope somebody can help me xD

Share this post


Link to post
Share on other sites



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.

Share this post


Link to post
Share on other sites

_DebugPrint() is from the HelpFile ;)


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

thx i will try this ^^

edit

works with a listview but not on my listbox ;)

maybe you got it for listbox or else i need to change alot of code for just the listview :)

seeya thx

Edited by tommeke228

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

_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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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

Share this post


Link to post
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)

Share this post


Link to post
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
Sign in to follow this  
Followers 0