#Region - TimeStamp
; 2012-03-13 19:12:46
#EndRegion - TimeStamp

#include-once
#Include <GuiListView.au3>
#Include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


Example()

Func Example()
    GUICreate("Move selected Item", 260, 250, 100, 200)
    $listview = GUICtrlCreateListView("col0|col1  |col2  |col3  ", 10, 10, 200, 150)
	$hLV = GuiCtrlGetHandle($listview)
	$down = GUICtrlCreateButton('/\', 215, 10, 15, 15)
	$up = GUICtrlCreateButton('\/', 215, 26, 15, 15)
    $item1 = GUICtrlCreateListViewItem("2|item_2|col_22|col_23", $listview)
    $item2 = GUICtrlCreateListViewItem("1|item_1|col_12|col_13", $listview)
    $item3 = GUICtrlCreateListViewItem("3|item_3|col_32|col_33", $listview)
    GUISetState()
    while 1
        Switch GUIGetMsg()
			Case $up
;~ 				_GUICtrlListView_MoveItem($hLV, 1)
				_GUICtrlListView_MoveItemCtrl($hLV, 1)
			case $down
;~ 				_GUICtrlListView_MoveItem($hLV, -1)
				_GUICtrlListView_MoveItemCtrl($hLV, -1)
			case -3
				exit
        EndSwitch
    wend
EndFunc   ;==>Example

;===============================================================================
; Function Name....: _GUICtrlListView_MoveItem
; Description......: Verschiebt den Inhalt eines markierten Item um jeweils eine Position auf-/abwärts
; Parameter(s).....: $_hWnd   Listview-ID oder /-Handle
; .................: $_i      RichtungsIndex (1 aufwärts/ -1 abwärts)
; Requirement(s)...: GuiListView.au3
; Return Value(s)..: keine
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _GUICtrlListView_MoveItem($_hWnd, $_i)
	If Not IsHWnd($_hWnd) Then $_hWnd = GuiCtrlGetHandle($_hWnd)
	Local $index = _GUICtrlListView_GetSelectedIndices($_hWnd)
	If $index = '' Or $index < 0 Then Return
	If $index = 0 And $_i = -1 Then Return
	Local $count = _GUICtrlListView_GetItemCount($_hWnd)
	If $index = $count -1 And $_i = 1 Then Return
	Local $aSelected = _GUICtrlListView_GetItemTextArray($_hWnd, $index)
	Local $indxTmp = $index + ($_i), $aTmp = _GUICtrlListView_GetItemTextArray($_hWnd, $indxTmp)
	For $i = 1 To $aSelected[0]
		_GUICtrlListView_SetItemText($_hWnd, $indxTmp, $aSelected[$i], $i-1)
		_GUICtrlListView_SetItemText($_hWnd, $index, $aTmp[$i], $i-1)
	Next
	_GUICtrlListView_SetItemSelected($_hWnd, $indxTmp, True, True)
EndFunc  ;==>_GUICtrlListView_MoveItem

;===============================================================================
; Function Name....: _GUICtrlListView_MoveItemCtrl
; Description......: Verschiebt den Inhalt eines Item um jeweils eine Position auf-/abwärts
; .................: Dazu werden die ListviewItem in neuer Reihenfolge neu erstellt.
; Parameter(s).....: $_hWnd   Listview-ID oder /-Handle
; .................: $_i      RichtungsIndex (1 aufwärts/ -1 abwärts)
; Requirement(s)...: GuiListView.au3, WinAPI.au3
; Return Value(s)..: Array mit neu erstellten ItemCtrl-ID
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _GUICtrlListView_MoveItemCtrl($_hWnd, $_i)
	If Not IsHWnd($_hWnd) Then $_hWnd = GuiCtrlGetHandle($_hWnd)
	Local $index = _GUICtrlListView_GetSelectedIndices($_hWnd)
	If $index = '' Or $index < 0 Then Return
	If $index = 0 And $_i = -1 Then Return
	Local $count = _GUICtrlListView_GetItemCount($_hWnd)
	If $index = $count -1 And $_i = 1 Then Return
	Local $indxTmp = $index + ($_i), $aItem[$count], $aOut[$count]
	For $i = 0 To $count -1
		$aItem[$i] = _GUICtrlListView_GetItemTextString($_hWnd, $i)
	Next
	_GUICtrlListView_BeginUpdate($_hWnd)
	_GUICtrlListView_DeleteAllItems($_hWnd)
	Local $sTmp, $ID_LV = _WinAPI_GetDlgCtrlID($_hWnd)
	For $i = 0 To $count -1
		Switch $i
		Case $index
			$sTmp = $aItem[$indxTmp]
		Case $indxTmp
			$sTmp = $aItem[$index]
		Case Else
			$sTmp = $aItem[$i]
		EndSwitch
		$aOut[$i] = GuiCtrlCreateListViewItem($sTmp, $ID_LV)
	Next
	_GUICtrlListView_SetItemSelected($_hWnd, $indxTmp, True, True)
	_GUICtrlListView_EndUpdate($_hWnd)
	Return $aOut
EndFunc  ;==>_GUICtrlListView_MoveItemCtrl

