Listview - KeyUP & KeyDown abfangen und die Markierung verschieben

  • Hey,

    mal wieder ein Problem zu Listview, hier das Skript:

    Spoiler anzeigen
    [autoit]


    #include <ButtonConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <ListViewConstants.au3>
    #include <WindowsConstants.au3>
    #include <GUIListView.au3>
    Global Const $tagLVKEYDOWN = $tagNMHDR & "; USHORT wVKey; UINT flags;"
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", 633, 447, 193, 125)
    $ListView1 = GUICtrlCreateListView("Hallo|1", 56, 40, 433, 225,"",BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER))
    $ListView2 = GUICtrlCreateListView("Hallo|2", 56, 40, 433, 225,$LVS_NOSCROLL,BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER))
    $hListView1 = GUICtrlGetHandle($ListView1)
    GUICtrlSetState(-1,$GUI_HIDE)
    $Button1 = GUICtrlCreateButton("Button1", 64, 288, 129, 49, 0)
    $Button2 = GUICtrlCreateButton("Button2", 264, 288, 185, 49, 0)
    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###
    for $i = 0 to 20
    GUICtrlCreateListViewItem("test|" & $i,$ListView1)
    next
    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    case $Button1
    GUICtrlSetState($ListView1, $GUI_HIDE)
    GUICtrlSetState($ListView2, $GUI_Show)
    GUICtrlCreateListViewItem("test|test",$ListView1)
    case $Button2
    GUICtrlSetState($ListView2, $GUI_HIDE)
    GUICtrlSetState($ListView1, $GUI_Show)
    GUICtrlCreateListViewItem("test|test",$ListView2)
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iCode, $tNMHDR, $tInfo, $wVKey

    [/autoit] [autoit][/autoit] [autoit]

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
    Case $hListView1
    Switch $iCode
    Case $LVN_KEYDOWN
    $tNMHDR = DllStructCreate($tagLVKEYDOWN, $ilParam)
    $wVKey= DllStructGetData($tNMHDR, "wVKey")
    Switch $wVKey
    case 40
    if _GUICtrlListView_GetSelectionMark($hListView1)+1 = _GUICtrlListView_GetItemCount($hListView1) Then
    _GUICtrlListView_SetSelectionMark($hListView1,0)
    _GUICtrlListView_SetItemFocused($hListView1,0)
    endif
    case 38
    if _GUICtrlListView_GetSelectedIndices($hListView1) = 0 Then
    _GUICtrlListView_SetItemSelected($hListView1, _GUICtrlListView_GetItemCount($hListView1)-1)
    _GUICtrlListView_SetItemFocused($hListView1,_GUICtrlListView_GetItemCount($hListView1)-1)
    endif
    EndSwitch
    EndSwitch

    [/autoit] [autoit][/autoit] [autoit]

    EndSwitch
    Return $GUI_RUNDEFMSG
    EndFunc ;==>_SHLV_WM_NOTIFY

    [/autoit]


    Sinn des Skripts soll sein, dass man die Markierung auf ein KeyUP/Down Event setzen kann.
    Und zwar immer wenn das erste oder das letzte Item markiert ist.

    Funktioniert auch fast, allerdings springt die Markierung komischerweise immer ein Item zu weit? ?(
    Also anstatt z.B. Item 1 (Index 0) zu markieren wird Item 2 (Index 1) markiert.
    Probierts am besten mal selbst aus, dann sollte es klar werden.

    Gruß nuts

    2 Mal editiert, zuletzt von nuts (17. Januar 2010 um 00:15)

  • Wäre klasse, wenn das als Option bei der "GUICtrlCreateListView()" und anderen vorhanden sein könnte.

    Ach ja, hier mal eine ganz primitive Version leider noch mit Hotkeys... Aber es funktioniert ja

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <GUIListBox.au3>
    #include <WindowsConstants.au3>
    #Region ### START Koda GUI section ### Form=C:\Form1.kxf
    $Form1 = GUICreate("Form1", 162, 210, 192, 124)
    $List1 = GUICtrlCreateList("", 8, 8, 145, 188)
    GUICtrlSetData(-1, "Item 1|Item 2|Item 3|Item 4|Item 5|Item 6|Item 7")
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    HotKeySet ("{UP}","UP")
    HotKeySet ("{DOWN}","DOWN")

    [/autoit] [autoit][/autoit] [autoit]

    Do
    $nMsg = GUIGetMsg()
    Until $nMsg = $GUI_EVENT_CLOSE

    [/autoit] [autoit][/autoit] [autoit]

    Func UP()
    If _GUICtrlListBox_GetCurSel ($List1) = 0 Then
    _GUICtrlListBox_SetCurSel ($List1,_GUICtrlListBox_GetCount ($List1)-1)
    Else
    _GUICtrlListBox_SetCurSel ($List1,_GUICtrlListBox_GetCurSel ($List1)-1)
    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func DOWN()
    If _GUICtrlListBox_GetCurSel ($List1) = _GUICtrlListBox_GetCount ($List1)-1 Then
    _GUICtrlListBox_SetCurSel ($List1,0)
    Else
    _GUICtrlListBox_SetCurSel ($List1,_GUICtrlListBox_GetCurSel ($List1) +1)
    EndIf
    EndFunc

    [/autoit]