Jump to content

Context menu on a sortable ListView


Wooltown
 Share

Recommended Posts

I have a sortable ListView, where I wish to have Context menus, but how do I do that, how can i via the Script find out what ControlID the single line has ? - GUICtrlCreateContextMenu ( [controlID] )

; *******************************************************
; Example 1 - sorting 3 column's different
; *******************************************************
Opt('GuiOnEventMode',1)
#include <GUIConstants.au3>

;Global Const $LVFI_PARAM          = 0x0001
;Global Const $LVIF_TEXT                = 0x0001
;Global Const $LVM_FIRST                = 0x1000
Global Const $LVM_GETITEM          = $LVM_FIRST + 5
;Global Const $LVM_FINDITEM      = $LVM_FIRST + 13
;Global Const $LVM_SETSELECTEDCOLUMN    = $LVM_FIRST + 140

Dim $nCurCol    = -1
Dim $nSortDir   = 1
Dim $bSet      = 0
Dim $nCol      = -1

$hGUI   = GUICreate("Test", 300, 200)

$lv  = GUICtrlCreateListView("Column1|Col2|Col3|Col4|Col5", 10, 10, 280, 180)
GUICtrlRegisterListViewSort(-1, "LVSort1"); Register the function "SortLV" for the sorting callback
GUICtrlSetOnEvent($lv,"LVsort2")

GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)
GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)
GUICtrlCreateListViewItem("B|2|R|8|H", $lv)
GUICtrlCreateListViewItem("C|3|S|9|I", $lv)
GUICtrlCreateListViewItem("D|4|T|0|J", $lv)
GUICtrlCreateListViewItem("E|5|U|1|K", $lv)
GUICtrlCreateListViewItem("F|6|V|2|L", $lv)
GUICtrlCreateListViewItem("G|7|W|3|M", $lv)
GUICtrlCreateListViewItem("H|8|X|4|N", $lv)
GUICtrlCreateListViewItem("I|9|Y|5|O", $lv)
GUICtrlCreateListViewItem("J|0|Z|6|P", $lv)
GUICtrlCreateListViewItem("K|1|A|7|Q", $lv)
GUICtrlCreateListViewItem("L|2|B|8|R", $lv)
GUICtrlCreateListViewItem("M|3|C|9|S", $lv)
GUICtrlCreateListViewItem("N|4|D|0|T", $lv)
GUICtrlCreateListViewItem("O|5|E|1|U", $lv)
GUICtrlCreateListViewItem("P|6|F|2|V", $lv)



GUISetState()

While 1
    Sleep (500)
;    $msg = GUIGetMsg()
;    Switch $msg
;        Case $GUI_EVENT_CLOSE
;            ExitLoop
;            
;        Case $lv
;            $bSet = 0
;            $nCurCol = $nCol
;            GUICtrlSendMsg($lv, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState($lv), 0)
;            DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle($lv), "int", 0, "int", 1)
;    EndSwitch
WEnd

Exit


; Our sorting callback funtion
Func LVSort1($hWnd, $nItem1, $nItem2, $nColumn)
    Local $nSort
    msgbox(0,"sort 1","",1)
   ; Switch the sorting direction
    If $nColumn = $nCurCol Then
        If Not $bSet Then
            $nSortDir = $nSortDir * -1
            $bSet = 1
        EndIf
    Else
        $nSortDir = 1
    EndIf
    $nCol = $nColumn
        
    $val1   = GetSubItemText($lv, $nItem1, $nColumn)
    $val2   = GetSubItemText($lv, $nItem2, $nColumn)
    
   ;$nResult = 0       ; No change of item1 and item2 positions
    
    If $val1 < $val2 Then
        $nResult = -1  ; Put item2 before item1
    Else
        $nResult = 1   ; Put item2 behind item1
    EndIf

    $nResult = $nResult * $nSortDir
     
    Return $nResult
EndFunc

; Retrieve the text of a listview item in a specified column
Func GetSubItemText($nCtrlID, $nItemID, $nColumn)
    Local $stLvfi      = DllStructCreate("uint;ptr;int;int[2];int")
    DllStructSetData($stLvfi, 1, $LVFI_PARAM)
    DllStructSetData($stLvfi, 3, $nItemID)

    Local $stBuffer  = DllStructCreate("char[260]")
    
    $nIndex = GUICtrlSendMsg($nCtrlID, $LVM_FINDITEM, -1, DllStructGetPtr($stLvfi));
    
    Local $stLvi        = DllStructCreate("uint;int;int;uint;uint;ptr;int;int;int;int")
    
    DllStructSetData($stLvi, 1, $LVIF_TEXT)
    DllStructSetData($stLvi, 2, $nIndex)
    DllStructSetData($stLvi, 3, $nColumn)
    DllStructSetData($stLvi, 6, DllStructGetPtr($stBuffer))
    DllStructSetData($stLvi, 7, 260)

    GUICtrlSendMsg($nCtrlID, $LVM_GETITEM, 0, DllStructGetPtr($stLvi));

    $sItemText  = DllStructGetData($stBuffer, 1)

    $stLvi    = 0
    $stLvfi  = 0
    $stBuffer   = 0
    
    Return $sItemText
EndFunc


Func LVsort2()
    $bSet = 0
     $nCurCol = $nCol
     GUICtrlSendMsg($lv, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState(@GUI_CTRLID), 0)
     DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle(@GUI_CTRLID), "int", 0, "int", 1)
EndFunc
Link to comment
Share on other sites

Don't you just need to say

$itemA = GUICtrlCreateListViewItem("A|1|Q|7|G", $lv)

and then $itema is the ID?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I sort the list clicking on a column header how do I then now which item has what ControlID ?m not sure

You might need to explain a bit more to me because I might be missing exactly what you are after.

If you want a context menu for a line then that can be done by setting up the context menus after each sort.

If you want a context menu for an individual item then it's a bit more difficult. (By which I mean do you want to detect which item is clicked rather then just the line, and then have the context menu appear for that item?)

Here is an example using Larry's mousehook

You need the dll, and save it in the scriptdir.

First I put the lines for the list view in this file to simplify the script. This file needs to be in the scriptdir as well.

Then I made a few changes to create the context menu depending on what was clicked

; *******************************************************
; Example 1 - sorting 3 column's different
; *******************************************************
Opt('GuiOnEventMode',1)
#include <GUIConstants.au3>
#include <guilistview.au3>

;Global Const $LVFI_PARAM          = 0x0001
;Global Const $LVIF_TEXT                = 0x0001
;Global Const $LVM_FIRST                = 0x1000
Global Const $NM_FIRST = 0
Global Const $NM_CLICK = ($NM_FIRST - 2)
Global Const $NM_DBLCLK = ($NM_FIRST - 3)
Global Const $LVM_GETITEM          = $LVM_FIRST + 5
;Global Const $LVM_FINDITEM      = $LVM_FIRST + 13
;Global Const $LVM_SETSELECTEDCOLUMN    = $LVM_FIRST + 140
Global Const $WM_NOTIFY    = 0x004E
Dim $nCurCol    = -1
Dim $nSortDir   = 1
Dim $bSet      = 0
Dim $nCol      = -1


#region larry's mousehook bits

Const $WH_KEYBOARD = 2
Const $WH_CBT = 5
Const $WH_MOUSE = 7

Const $WM_AUTOITLBUTTONDOWN = 0x1400 + 0x0A30
Const $WM_AUTOITRBUTTONDOWN = 0x1400 + 0x0A31
Const $WM_AUTOITMBUTTONDOWN = 0x1400 + 0x0A32
Const $WM_AUTOITXBUTTONDOWN1 = 0x1400 + 0x0A33
Const $WM_AUTOITXBUTTONDOWN2 = 0x1400 + 0x0A34
Const $WM_AUTOITLBUTTONUP = 0x1400 + 0x0B30
Const $WM_AUTOITRBUTTONUP = 0x1400 + 0x0B31
Const $WM_AUTOITMBUTTONUP = 0x1400 + 0x0B32
Const $WM_AUTOITXBUTTONUP1 = 0x1400 + 0x0B33
Const $WM_AUTOITXBUTTONUP2 = 0x1400 + 0x0B34
Const $WM_AUTOITLDBLCLK = 0x1400 + 0x0C30
Const $WM_AUTOITRDBLCLK = 0x1400 + 0x0C31
Const $WM_AUTOITMDBLCLK = 0x1400 + 0x0C32
Const $WM_AUTOITXDBLCLK1 = 0x1400 + 0x0C33
Const $WM_AUTOITXDBLCLK2 = 0x1400 + 0x0C34
Const $WM_AUTOITMOUSEWHEELUP = 0x1400 + 0x0D30
Const $WM_AUTOITMOUSEWHEELDOWN = 0x1400 + 0x0D31
Const $WM_AUTOITMOUSEMOVE = 0x1400 + 0x0F30

Const $HCBT_SETFOCUS = 0x1400 + 0x1A30
Const $HCBT_ACTIVATE = 0x1400 + 0x1A31
Const $HCBT_CREATEWND = 0x1400 + 0x1A32
Const $HCBT_DESTROYWND = 0x1400 + 0x1A33
Const $HCBT_MINMAX = 0x1400 + 0x1A34

Const $WM_KEYDOWN = 0x0400 + 0x0A30
Const $WM_KEYUP = 0x0400 + 0x0A31

Global $n,$msg,$buffer = ""

HotKeySet("{ESC}","GoAway")

;$gui = GUICreate("test")
$hooky = "d:\program files\autoit3\beta\koda\forms\mouse\hook.dll"
Global $DLLinst = DLLCall("kernel32.dll","hwnd","LoadLibrary","str",$hooky)
Global $mouseHOOKproc = DLLCall("kernel32.dll","hwnd","GetProcAddress","hwnd",$DLLInst[0],"str","MouseProc")
Global $keyHOOKproc = DLLCall("kernel32.dll","hwnd","GetProcAddress","hwnd",$DLLInst[0],"str","KeyProc")
Global $cbtHOOKproc = DLLCall("kernel32.dll","hwnd","GetProcAddress","hwnd",$DLLInst[0],"str","CBTProc")

Global $hhMouse = DLLCall("user32.dll","hwnd","SetWindowsHookEx","int",$WH_MOUSE, _
        "hwnd",$mouseHOOKproc[0],"hwnd",$DLLinst[0],"int",0)
Global $hhKey = DLLCall("user32.dll","hwnd","SetWindowsHookEx","int",$WH_KEYBOARD, _
        "hwnd",$keyHOOKproc[0],"hwnd",$DLLinst[0],"int",0)
Global $hhCBT = DLLCall("user32.dll","hwnd","SetWindowsHookEx","int",$WH_CBT, _
        "hwnd",$cbtHOOKproc[0],"hwnd",$DLLinst[0],"int",0)

Dim $wids[6]
#endregion larry's mousehook bits


$hGUI   = GUICreate("Test", 300, 200)

$lv  = GUICtrlCreateListView("Column1|Col2|Col3|Col4|Col5", 10, 10, 280, 180)
GUICtrlRegisterListViewSort(-1, "LVSort1"); Register the function "SortLV" for the sorting callback
GUICtrlSetOnEvent($lv,"LVsort2")

$maxlines = 50
Dim $lvlines[$maxlines]
$flist = FileOpen(@ScriptDir & "\list001.txt",0)
If $flist = -1 Then
    MsgBox(0,'error',"can't open file")
    Exit
EndIf

Dim $l
$nolines = 0
While 1
    $l = FileReadLine($flist)
   If @error = -1 Then Exitloop
    $er = @error
    If $nolines > $maxlines - 1 Then 
        $maxlines += 20
        ReDim $lvlines[$maxlines]
    EndIf
    
    $lvlines[$nolines] =  GUICtrlCreateListViewItem($l, $lv)
    $nolines += 1
WEnd
FileClose($flist)

$cm = GUiCtrlCreateContextMenu($lv);lines[0])
$cmIt1 = GUICtrlCreateMenuItem('1',$cm)
$cmit2 = GUICtrlCreateMenuItem('2',$cm)
$cmit3 = GUICtrlCreateMenuItem('3',$cm)
$buttonitem   = GUICtrlCreateMenuitem("About button", $lvlines[0])
DLLCall($hooky,"int","SetValuesMouse","hwnd",$hGUI,"hwnd",$hhMouse[0])
DLLCall($hooky,"int","SetValuesKey","hwnd",$hGUI,"hwnd",$hhKey[0])
DLLCall($hooky,"int","SetValuesCBT","hwnd",$hGUI,"hwnd",$hhCBT[0])


GUISetState()
;GUIRegisterMsg($WM_NOTIFY,"WM_Notify_Events")

GUIRegisterMsg($WM_AUTOITRBUTTONDOWN,"mgrkd");
$wids[0] = 0
For $wn = 1 To 5;find the column widths
    
    $wids[$wn] = _GUICtrlListViewGetColumnWidth($lv,$wn-1) + $wids[$wn - 1]
Next
;MsgBox(0,'widths are', $wids[0] & ', ' & $wids[1] & ', ' & $wids[2] & ', ' & $wids[3] & ', ' & $wids[4])

GUISetOnEvent($GUI_EVENT_CLOSE,"exit1")
While 1
    Sleep (5)
; $msg = GUIGetMsg()
; Switch $msg
;    Case $GUI_EVENT_CLOSE
;        ExitLoop
;
;        Case $lv
;            $bSet = 0
;            $nCurCol = $nCol
;            GUICtrlSendMsg($lv, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState($lv), 0)
;            DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle($lv), "int", 0, "int", 1)
;  EndSwitch
WEnd

Exit
Func mgrkd($hWndGUI, $MsgID, $WParam, $LParam)

    $gp = WinGetPos($hGUI)
    $gc = WinGetClientSize($hGUI)
    $mp = MouseGetPos(0) - $gp[0] - Int(($gp[2] - $gc[0])/2) - 10
    $sc = ''
    $sc = ''
    For $cn = 0 To 4
        If $mp > $wids[$cn] Then
            $sc = $cn;'col = ' & $cn + 1

        EndIf
    Next
    $selline =  _GUICtrlListViewGetCurSel($lv)
    If IsArray($lineitems) Then;this is in case cursor is outside the listview- needs improving
        GUICtrlSetData($cmIt1,$lineitems[$sc+1])
        GUICtrlSetData($cmIt2,'Line ' & $selline + 1)
        GUICtrlSetData($cmIt3,'Column ' & $sc + 1)
    EndIf
;ToolTip($mp & ', ' & $sc)
    Return 0
EndFunc


; Our sorting callback funtion
Func LVSort1($hWnd, $nItem1, $nItem2, $nColumn)
    Local $nSort
;msgbox(0,"sort 1","",1)
; Switch the sorting direction
    If $nColumn = $nCurCol Then
        If Not $bSet Then
            $nSortDir = $nSortDir * -1
            $bSet = 1
        EndIf
    Else
        $nSortDir = 1
    EndIf
    $nCol = $nColumn

    $val1   = GetSubItemText($lv, $nItem1, $nColumn)
    $val2   = GetSubItemText($lv, $nItem2, $nColumn)

;$nResult = 0; No change of item1 and item2 positions

    If $val1 < $val2 Then
        $nResult = -1; Put item2 before item1
    Else
        $nResult = 1; Put item2 behind item1
    EndIf

    $nResult = $nResult * $nSortDir

    Return $nResult
EndFunc

; Retrieve the text of a listview item in a specified column
Func GetSubItemText($nCtrlID, $nItemID, $nColumn)
    Local $stLvfi      = DllStructCreate("uint;ptr;int;int[2];int")
    DllStructSetData($stLvfi, 1, $LVFI_PARAM)
    DllStructSetData($stLvfi, 3, $nItemID)

    Local $stBuffer  = DllStructCreate("char[260]")

    $nIndex = GUICtrlSendMsg($nCtrlID, $LVM_FINDITEM, -1, DllStructGetPtr($stLvfi));

    Local $stLvi        = DllStructCreate("uint;int;int;uint;uint;ptr;int;int;int;int")

    DllStructSetData($stLvi, 1, $LVIF_TEXT)
    DllStructSetData($stLvi, 2, $nIndex)
    DllStructSetData($stLvi, 3, $nColumn)
    DllStructSetData($stLvi, 6, DllStructGetPtr($stBuffer))
    DllStructSetData($stLvi, 7, 260)

    GUICtrlSendMsg($nCtrlID, $LVM_GETITEM, 0, DllStructGetPtr($stLvi));

    $sItemText  = DllStructGetData($stBuffer, 1)

    $stLvi    = 0
    $stLvfi  = 0
    $stBuffer   = 0

    Return $sItemText
EndFunc


Func LVsort2()
    $bSet = 0
    $nCurCol = $nCol
    GUICtrlSendMsg($lv, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState(@GUI_CTRLID), 0)
    DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle(@GUI_CTRLID), "int", 0, "int", 1)
EndFunc

Func Exit1()
    Exit

EndFunc

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event
    $tagNMHDR = DllStructCreate("int;int;int", $lParam);NMHDR (hwndFrom, idFrom, code)
    If @error Then Return
    $event = DllStructGetData($tagNMHDR, 3)
    Select
        Case $wParam = $LV
            Select
;Case $event = $NM_CLICK+1
;   ListView_Click()
;Case $WM_AUTOITRBUTTONUP
;   ListView_Click()
                Case $event = $NM_DBLCLK
                    ListView_DoubleClick()
            EndSelect
    EndSelect
    $tagNMHDR = 0
    $event = 0
    $lParam = 0
EndFunc;

Func ListView_Click()
    GUICtrlSetData($cmIt1,'hhsa')
    MsgBox(0,'done it','')
;~  _DebugPrint("Button_Click")
EndFunc;==>Button_Click

Func ListView_DoubleClick()
;~  _DebugPrint("Button_DblClick")
    Local $itemIndex = _GUICtrlListViewGetCurSel ($LV)
;MsgBox(0,"Item dbl click on", $itemIndex)
    $nd = _GUICtrlListViewGetItemText($LV, $itemIndex,0)
;msgbox(0,'new dir = ',$nd)
;changedir($nd)
EndFunc;==>Button_Click





#region larry mousehook end BitShift
Func MyFunc($hWndGUI, $MsgID, $WParam, $LParam)
    Return
    $n += 1
    If $n > 25 Then
        $n = 25
        $buffer = StringTrimLeft($buffer,StringInStr($buffer,@LF))
    EndIf
    $buffer &= "Mouse: " & $MsgID & "," &  $WParam & "," &  $LParam & @LF
    ToolTip($buffer)
EndFunc

Func MyCBTFunc($hWndGUI, $MsgID, $WParam, $LParam)
    $n += 1
    If $n > 25 Then
        $n = 25
        $buffer = StringTrimLeft($buffer,StringInStr($buffer,@LF))
    EndIf
    $buffer &= "CBT: " & $MsgID & "," &  $WParam & "," &  $LParam & @LF
    ToolTip($buffer)
EndFunc

Func MyKeyFunc($hWndGUI, $MsgID, $WParam, $LParam)
    $n += 1
    If $n > 25 Then
        $n = 25
        $buffer = StringTrimLeft($buffer,StringInStr($buffer,@LF))
    EndIf
    $buffer &= "Keyboard: " & $MsgID & "," &  $WParam & "," &  $LParam & @LF
    ToolTip($buffer)
EndFunc

Func GoAway()
    Exit
EndFunc

Func OnAutoItExit()
    DLLCall("user32.dll","int","UnhookWindowsHookEx","hwnd",$hhMouse[0])
    DLLCall("user32.dll","int","UnhookWindowsHookEx","hwnd",$hhKey[0])
    DLLCall("user32.dll","int","UnhookWindowsHookEx","hwnd",$hhCBT[0])
    DLLCall("kernel32.dll","int","FreeLibrary","hwnd",$DLLinst[0])
EndFunc

#endregion

Of course someone might know an easier way but that's the only way I can figure out how to do it.

EDIT: added example then made small changes

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • 2 weeks later...

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...