Sign in to follow this  
Followers 0

_GUICtrlListView_MoveItems - UDF

11 posts in this topic

#1 ·  Posted

;===============================================================================
; Function Name:    _GUICtrlListView_MoveItems()
; Description:      Move selected item(s) in ListView Up or Down.
;
; Parameter(s):     $hWnd               - Window handle of ListView control (can be a Title).
;                   $vListView          - The ID/Handle/Class of ListView control.
;                   $iDirection         - [Optional], define in what direction item(s) will move:
;                                            1 (default) - item(s) will move Next.
;                                           -1 item(s) will move Back.
;                   $sIconsFile         - Icon file to set image for the items (only for internal usage).
;                   $iIconID_Checked    - Icon ID in $sIconsFile for checked item(s).
;                   $iIconID_UnChecked  - Icon ID in $sIconsFile for Unchecked item(s).
;
; Requirement(s):   #include <GuiListView.au3>, AutoIt 3.2.10.0.
;
; Return Value(s):  On seccess - Move selected item(s) Next/Back.
;                   On failure - Return "" (empty string) and set @error as following:
;                                                                  1 - No selected item(s).
;                                                                  2 - $iDirection is wrong value (not 1 and not -1).
;                                                                  3 - Item(s) can not be moved, reached last/first item.
;
; Note(s):          * This function work with external ListView Control as well.
;                   * If you select like 15-20 (or more) items, moving them can take a while :( (depends on how many items moved).
;
; Author(s):        G.Sandler a.k.a CreatoR (http://creator-lab.ucoz.ru)
;===============================================================================
Func _GUICtrlListView_MoveItems($hWnd, $vListView, $iDirection=1, $sIconsFile="", $iIconID_Checked=0, $iIconID_UnChecked=0)
    Local $hListView = $vListView
    If Not IsHWnd($hListView) Then $hListView = ControlGetHandle($hWnd, "", $hListView)
    
    Local $aSelected_Indices = _GUICtrlListView_GetSelectedIndices($hListView, 1)
    If UBound($aSelected_Indices) < 2 Then Return SetError(1, 0, "")
    If $iDirection <> 1 And $iDirection <> -1 Then Return SetError(2, 0, "")
    
    Local $iTotal_Items = ControlListView($hWnd, "", $hListView, "GetItemCount")
    Local $iTotal_Columns = ControlListView($hWnd, "", $hListView, "GetSubItemCount")
    
    Local $iUbound = UBound($aSelected_Indices)-1, $iNum = 1, $iStep = 1
    Local $iCurrent_Index, $iUpDown_Index, $sCurrent_ItemText, $sUpDown_ItemText
    Local $iCurrent_Index, $iCurrent_CheckedState, $iUpDown_CheckedState
    
    If ($iDirection = -1 And $aSelected_Indices[1] = 0) Or _
        ($iDirection = 1 And $aSelected_Indices[$iUbound] = $iTotal_Items-1) Then Return SetError(3, 0, "")
    
    ControlListView($hWnd, "", $hListView, "SelectClear")
    
    Local $aOldSelected_IDs[1]
    Local $iIconsFileExists = FileExists($sIconsFile)
    
    If $iIconsFileExists Then
        For $i = 1 To $iUbound
            ReDim $aOldSelected_IDs[UBound($aOldSelected_IDs)+1]
            _GUICtrlListView_SetItemSelected($hListView, $aSelected_Indices[$i], True)
            $aOldSelected_IDs[$i] = GUICtrlRead($vListView)
            _GUICtrlListView_SetItemSelected($hListView, $aSelected_Indices[$i], False)
        Next
        ControlListView($hWnd, "", $hListView, "SelectClear")
    EndIf
    
    If $iDirection = 1 Then
        $iNum = $iUbound
        $iUbound = 1
        $iStep = -1
    EndIf
    
    For $i = $iNum To $iUbound Step $iStep
        $iCurrent_Index = $aSelected_Indices[$i]
        $iUpDown_Index = $aSelected_Indices[$i]+1
        If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i]-1
        
        $iCurrent_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iCurrent_Index)
        $iUpDown_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iUpDown_Index)
        
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)
        
        For $j = 0 To $iTotal_Columns-1
            $sCurrent_ItemText = _GUICtrlListView_GetItemText($hListView, $iCurrent_Index, $j)
            $sUpDown_ItemText = _GUICtrlListView_GetItemText($hListView, $iUpDown_Index, $j)
            
            _GUICtrlListView_SetItemText($hListView, $iUpDown_Index, $sCurrent_ItemText, $j)
            _GUICtrlListView_SetItemText($hListView, $iCurrent_Index, $sUpDown_ItemText, $j)
        Next
        
        _GUICtrlListView_SetItemChecked($hListView, $iUpDown_Index, $iCurrent_CheckedState)
        _GUICtrlListView_SetItemChecked($hListView, $iCurrent_Index, $iUpDown_CheckedState)
        
        If $iIconsFileExists Then
            If $iCurrent_CheckedState = 1 Then
                GUICtrlSetImage(GUICtrlRead($vListView), $sIconsFile, $iIconID_Checked, 0)
            Else
                GUICtrlSetImage(GUICtrlRead($vListView), $sIconsFile, $iIconID_UnChecked, 0)
            EndIf
            
            If $iUpDown_CheckedState = 1 Then
                GUICtrlSetImage($aOldSelected_IDs[$i], $sIconsFile, $iIconID_Checked, 0)
            Else
                GUICtrlSetImage($aOldSelected_IDs[$i], $sIconsFile, $iIconID_UnChecked, 0)
            EndIf
        EndIf
        
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index, 0)
    Next
    
    For $i = 1 To UBound($aSelected_Indices)-1
        $iUpDown_Index = $aSelected_Indices[$i]+1
        If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i]-1
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)
    Next
EndFunc


Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Not sure what's the purpose of $sIconsFile and other 2 parameters that follow it, but your func doesn't support images (text and checkbox state moves, image stays the same).

Easy to fix using

_GUICtrlListView_GetItemImage & _GUICtrlListView_SetItemImage.

And maybe also _GUICtrlListView_GetItemStateImage & _GUICtrlListView_SetItemStateImage too, just because we can.

Just be sure to check for the presence of corresponding ImageLists with _GUICtrlListView_GetImageList.

Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

#3 ·  Posted

Hi,

Not sure what's the purpose of $sIconsFile and other 2 parameters that follow it, but your func doesn't support images

It does support, but just images direct from file, in the old way :D - GUICtrlSetImage still work i think :P

You just set image file for items, and the index icon in that file for checked/unchecked items.

I thought that this func needs modification for the _GUICtrlListView_GetItemImage, but i wrote it a long time ago, and i still not worked with imges format (as in new version) for the ListView. So i will try to modify it later, thanks.


Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#4 ·  Posted

Ok, here is another version wich preserves the checkbox states and images list of the items:

;Demo for _GUICtrlListView_MoveItems() function.
;Just select one (or more) item, and press one of the buttons (Up or Down).

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <SendMessage.au3>
;

$GUI = GUICreate('Demo for _GUICtrlListView_MoveItems()', 300, 320)

$Up_Button = GUICtrlCreateButton("Up", 20, 20, 24, 24, $BS_ICON)
GUICtrlSetImage(-1, "netcfgx.dll", 1, 0)

$Down_Button = GUICtrlCreateButton("Down", 70, 20, 24, 24, $BS_ICON)
GUICtrlSetImage(-1, "netcfgx.dll", -2, 0)

$ListView = _GUICtrlListView_Create($GUI, "Column1|Column2", 20, 50, 260, 250, _
    BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT), $WS_EX_CLIENTEDGE)

_GUICtrlListView_SetExtendedListViewStyle($ListView, BitOR($LVS_EX_CHECKBOXES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))

$iImage_Size = 14

$hImage = _GUIImageList_Create($iImage_Size, $iImage_Size)

_GUIImageList_Add($hImage, _WinAPI_CreateSolidBitmap($ListView, 0xFF0000, $iImage_Size, $iImage_Size))
_GUIImageList_Add($hImage, _WinAPI_CreateSolidBitmap($ListView, 0x00FF00, $iImage_Size, $iImage_Size))
_GUIImageList_Add($hImage, _WinAPI_CreateSolidBitmap($ListView, 0x0000FF, $iImage_Size, $iImage_Size))

_GUICtrlListView_SetImageList($ListView, $hImage, 1)

For $i = 0 To 9
    _GUICtrlListView_AddItem($ListView, "Item " & $i+1, Random(0, 2, 1))
    _GUICtrlListView_AddSubItem($ListView, $i, "SubItem " & $i+1, 1, Random(0, 2, 1))
Next

$iRandom_1 = Random(0, 9, 1)
$iRandom_2 = Random(0, 9, 1)

_GUICtrlListView_SetItemChecked($ListView, $iRandom_1, 1)
_GUICtrlListView_SetItemSelected($ListView, $iRandom_1, 1)

_GUICtrlListView_SetItemChecked($ListView, $iRandom_2, 1)
_GUICtrlListView_SetItemSelected($ListView, $iRandom_2, 1)

_SendMessage($ListView, $LVM_SETCOLUMNWIDTH, 0, -1)
_SendMessage($ListView, $LVM_SETCOLUMNWIDTH, 1, -1)

ControlFocus($GUI, "", $ListView)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Up_Button
            _GUICtrlListView_MoveItems($ListView, -1)
            ControlFocus($GUI, "", $ListView)
        Case $Down_Button
            _GUICtrlListView_MoveItems($ListView, 1)
            ControlFocus($GUI, "", $ListView)
    EndSwitch
WEnd

;===============================================================================
; Function Name:    _GUICtrlListView_MoveItems()
; Description:      Moves Up or Down selected item(s) in ListView.
;
; Parameter(s):     $hListView          - ControlID or Handle of ListView control.
;                   $iDirection         - Define in what direction item(s) will move:
;                                           -1 - Move Up.
;                                            1 - Move Down.
;
; Requirement(s):   AutoIt 3.3.0.0
;
; Return Value(s):  On seccess - Move selected item(s) Up/Down and return 1.
;                   On failure - Return "" (empty string) and set @error as following:
;                                                                  1 - No selected item(s).
;                                                                  2 - $iDirection is wrong value (not 1 and not -1).
;                                                                  3 - Item(s) can not be moved, reached last/first item.
;
; Note(s):          * If you select like 15-20 (or more) items, moving them can take a while :( (second or two).
;
; Author(s):        G.Sandler a.k.a CreatoR
;===============================================================================
Func _GUICtrlListView_MoveItems($hListView, $iDirection)
    Local $aSelected_Indices = _GUICtrlListView_GetSelectedIndices($hListView, 1)
    
    If UBound($aSelected_Indices) < 2 Then Return SetError(1, 0, "")
    If $iDirection <> 1 And $iDirection <> -1 Then Return SetError(2, 0, "")
    
    Local $iTotal_Items = _GUICtrlListView_GetItemCount($hListView)
    Local $iTotal_Columns = _GUICtrlListView_GetColumnCount($hListView)
    
    Local $iUbound = UBound($aSelected_Indices)-1, $iNum = 1, $iStep = 1
    
    Local $iCurrent_Index, $iUpDown_Index, $sCurrent_ItemText, $sUpDown_ItemText
    Local $iCurrent_Index, $iCurrent_CheckedState, $iUpDown_CheckedState
    Local $iImage_Current_Index, $iImage_UpDown_Index
    
    If ($iDirection = -1 And $aSelected_Indices[1] = 0) Or _
        ($iDirection = 1 And $aSelected_Indices[$iUbound] = $iTotal_Items-1) Then Return SetError(3, 0, "")
    
    ControlListView($hListView, "", "", "SelectClear")
    
    If $iDirection = 1 Then
        $iNum = $iUbound
        $iUbound = 1
        $iStep = -1
    EndIf
    
    For $i = $iNum To $iUbound Step $iStep
        $iCurrent_Index = $aSelected_Indices[$i]
        $iUpDown_Index = $aSelected_Indices[$i]+1
        If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i]-1
        
        $iCurrent_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iCurrent_Index)
        $iUpDown_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iUpDown_Index)
        
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)
        
        For $j = 0 To $iTotal_Columns-1
            $sCurrent_ItemText = _GUICtrlListView_GetItemText($hListView, $iCurrent_Index, $j)
            $sUpDown_ItemText = _GUICtrlListView_GetItemText($hListView, $iUpDown_Index, $j)
            
            If _GUICtrlListView_GetImageList($hListView, 1) <> 0 Then
                $iImage_Current_Index = _GUICtrlListView_GetItemImage($hListView, $iCurrent_Index, $j)
                $iImage_UpDown_Index = _GUICtrlListView_GetItemImage($hListView, $iUpDown_Index, $j)
                
                _GUICtrlListView_SetItemImage($hListView, $iCurrent_Index, $iImage_UpDown_Index, $j)
                _GUICtrlListView_SetItemImage($hListView, $iUpDown_Index, $iImage_Current_Index, $j)
            EndIf
            
            _GUICtrlListView_SetItemText($hListView, $iUpDown_Index, $sCurrent_ItemText, $j)
            _GUICtrlListView_SetItemText($hListView, $iCurrent_Index, $sUpDown_ItemText, $j)
        Next
        
        _GUICtrlListView_SetItemChecked($hListView, $iUpDown_Index, $iCurrent_CheckedState)
        _GUICtrlListView_SetItemChecked($hListView, $iCurrent_Index, $iUpDown_CheckedState)
        
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index, 0)
    Next
    
    For $i = 1 To UBound($aSelected_Indices)-1
        $iUpDown_Index = $aSelected_Indices[$i]+1
        If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i]-1
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)
    Next
    
    Return 1
EndFunc

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

can this func manipulate external script's LV?

Yes:

;Demo for _GUICtrlListView_MoveItems() function.
;Just select one (or more) item, and press one of the buttons (Up or Down).

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <SendMessage.au3>
;

_External_GUI()

$GUI = GUICreate('Demo for _GUICtrlListView_MoveItems()', 300, 320, 200)

$Up_Button = GUICtrlCreateButton("Up", 20, 20, 24, 24, $BS_ICON)
GUICtrlSetImage(-1, "netcfgx.dll", 1, 0)

$Down_Button = GUICtrlCreateButton("Down", 70, 20, 24, 24, $BS_ICON)
GUICtrlSetImage(-1, "netcfgx.dll", -2, 0)

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Up_Button, $Down_Button
            $hListView = ControlGetHandle("_GUICtrlListView_MoveItems- External Test", "", "SysListView321")
            
            $iMoveMode = -1
            If $nMsg = $Down_Button Then $iMoveMode = 1
            
            _GUICtrlListView_MoveItems($hListView, $iMoveMode)
            ControlFocus("", "", $hListView)
    EndSwitch
WEnd

Func _External_GUI()
    Local $hGUI, $hListView, $hImageList, $iImage_Size = 14, $iRandom_1, $iRandom_2
    
    $hGUI = GUICreate('_GUICtrlListView_MoveItems- External Test', 300, 320, 520, -1, -1, $WS_EX_TOPMOST)
    
    $hListView = _GUICtrlListView_Create($hGUI, "Column1|Column2", 20, 50, 260, 250, _
        BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT), $WS_EX_CLIENTEDGE)
    
    _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_CHECKBOXES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    
    $hImageList = _GUIImageList_Create($iImage_Size, $iImage_Size)
    
    _GUIImageList_Add($hImageList, _WinAPI_CreateSolidBitmap($hListView, 0xFF0000, $iImage_Size, $iImage_Size))
    _GUIImageList_Add($hImageList, _WinAPI_CreateSolidBitmap($hListView, 0x00FF00, $iImage_Size, $iImage_Size))
    _GUIImageList_Add($hImageList, _WinAPI_CreateSolidBitmap($hListView, 0x0000FF, $iImage_Size, $iImage_Size))
    
    _GUICtrlListView_SetImageList($hListView, $hImageList, 1)
    
    For $i = 0 To 9
        _GUICtrlListView_AddItem($hListView, "Item " & $i+1, Random(0, 2, 1))
        _GUICtrlListView_AddSubItem($hListView, $i, "SubItem " & $i+1, 1, Random(0, 2, 1))
    Next
    
    $iRandom_1 = Random(0, 9, 1)
    $iRandom_2 = Random(0, 9, 1)
    
    _GUICtrlListView_SetItemChecked($hListView, $iRandom_1, 1)
    _GUICtrlListView_SetItemSelected($hListView, $iRandom_1, 1)
    
    _GUICtrlListView_SetItemChecked($hListView, $iRandom_2, 1)
    _GUICtrlListView_SetItemSelected($hListView, $iRandom_2, 1)
    
    _SendMessage($hListView, $LVM_SETCOLUMNWIDTH, 0, -1)
    _SendMessage($hListView, $LVM_SETCOLUMNWIDTH, 1, -1)
    
    ControlFocus($hGUI, "", $hListView)
    
    GUISetState()
EndFunc

;===============================================================================
; Function Name:    _GUICtrlListView_MoveItems()
; Description:    Moves Up or Down selected item(s) in ListView.
;
; Parameter(s):  $hListView       - ControlID or Handle of ListView control.
;                  $iDirection       - Define in what direction item(s) will move:
;                                          -1 - Move Up.
;                                           1 - Move Down.
;
; Requirement(s):   AutoIt 3.3.0.0
;
; Return Value(s):  On seccess - Move selected item(s) Up/Down and return 1.
;                  On failure - Return "" (empty string) and set @error as following:
;                                                                 1 - No selected item(s).
;                                                                 2 - $iDirection is wrong value (not 1 and not -1).
;                                                                 3 - Item(s) can not be moved, reached last/first item.
;
; Note(s):        * If you select like 15-20 (or more) items, moving them can take a while  (second or two).
;
; Author(s):        G.Sandler a.k.a CreatoR
;===============================================================================
Func _GUICtrlListView_MoveItems($hListView, $iDirection)
    Local $aSelected_Indices = _GUICtrlListView_GetSelectedIndices($hListView, 1)
    
    If UBound($aSelected_Indices) < 2 Then Return SetError(1, 0, "")
    If $iDirection <> 1 And $iDirection <> -1 Then Return SetError(2, 0, "")
    
    Local $iTotal_Items = _GUICtrlListView_GetItemCount($hListView)
    Local $iTotal_Columns = _GUICtrlListView_GetColumnCount($hListView)
    
    Local $iUbound = UBound($aSelected_Indices)-1, $iNum = 1, $iStep = 1
    
    Local $iCurrent_Index, $iUpDown_Index, $sCurrent_ItemText, $sUpDown_ItemText
    Local $iCurrent_Index, $iCurrent_CheckedState, $iUpDown_CheckedState
    Local $iImage_Current_Index, $iImage_UpDown_Index
    
    If ($iDirection = -1 And $aSelected_Indices[1] = 0) Or _
        ($iDirection = 1 And $aSelected_Indices[$iUbound] = $iTotal_Items-1) Then Return SetError(3, 0, "")
    
    ControlListView($hListView, "", "", "SelectClear")
    
    If $iDirection = 1 Then
        $iNum = $iUbound
        $iUbound = 1
        $iStep = -1
    EndIf
    
    For $i = $iNum To $iUbound Step $iStep
        $iCurrent_Index = $aSelected_Indices[$i]
        $iUpDown_Index = $aSelected_Indices[$i]+1
        If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i]-1
        
        $iCurrent_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iCurrent_Index)
        $iUpDown_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iUpDown_Index)
        
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)
        
        For $j = 0 To $iTotal_Columns-1
            $sCurrent_ItemText = _GUICtrlListView_GetItemText($hListView, $iCurrent_Index, $j)
            $sUpDown_ItemText = _GUICtrlListView_GetItemText($hListView, $iUpDown_Index, $j)
            
            If _GUICtrlListView_GetImageList($hListView, 1) <> 0 Then
                $iImage_Current_Index = _GUICtrlListView_GetItemImage($hListView, $iCurrent_Index, $j)
                $iImage_UpDown_Index = _GUICtrlListView_GetItemImage($hListView, $iUpDown_Index, $j)
                
                _GUICtrlListView_SetItemImage($hListView, $iCurrent_Index, $iImage_UpDown_Index, $j)
                _GUICtrlListView_SetItemImage($hListView, $iUpDown_Index, $iImage_Current_Index, $j)
            EndIf
            
            _GUICtrlListView_SetItemText($hListView, $iUpDown_Index, $sCurrent_ItemText, $j)
            _GUICtrlListView_SetItemText($hListView, $iCurrent_Index, $sUpDown_ItemText, $j)
        Next
        
        _GUICtrlListView_SetItemChecked($hListView, $iUpDown_Index, $iCurrent_CheckedState)
        _GUICtrlListView_SetItemChecked($hListView, $iCurrent_Index, $iUpDown_CheckedState)
        
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index, 0)
    Next
    
    For $i = 1 To UBound($aSelected_Indices)-1
        $iUpDown_Index = $aSelected_Indices[$i]+1
        If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i]-1
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)
    Next
    
    Return 1
EndFunc
Edited by MrCreatoR

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#7 ·  Posted

Ok, here is another version wich preserves the checkbox states and images list of the items:

Look in the help for _GUICtrlListView_CopyItems


SciTE for AutoItDirections for Submitting Standard UDFs


Don't argue with an idiot; people watching may not be able to tell the difference.

Share this post


Link to post
Share on other sites

#8 ·  Posted

Look in the help for _GUICtrlListView_CopyItems

I know about that function, thanks. But it's for...

Copy Items between 2 list-view controls

And this function move the items up / down ^_^.

P.S

Perhaps it could be added to the standard UDFs as well?


Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#9 ·  Posted

I know about that function, thanks. But it's for...

And this function move the items up / down ^_^.

P.S

Perhaps it could be added to the standard UDFs as well?

last param "$fDelFlag Delete after copying" would make a move, although it doesn't move items up/down.


SciTE for AutoItDirections for Submitting Standard UDFs


Don't argue with an idiot; people watching may not be able to tell the difference.

Share this post


Link to post
Share on other sites

#10 ·  Posted

Hello

i have made some change in your function, because i use it in a listview with Group and ItemParam . I have made 2 functions for moving items in last or first position.

;===============================================================================
; Function Name:    _GUICtrlListView_MoveItems()
; Description:      Move selected item(s) in ListView Up or Down.
;
; Parameter(s):     $hWnd               - Window handle of ListView control (can be a Title).
;                   $vListView          - The ID/Handle/Class of ListView control.
;                   $iDirection         - [Optional], define in what direction item(s) will move:
;                                           1 (default) - item(s) will move Next.
;                                           -1 item(s) will move Back.
;                   $sIconsFile         - Icon file to set image for the items (only for internal usage).
;                   $iIconID_Checked    - Icon ID in $sIconsFile for checked item(s).
;                   $iIconID_UnChecked  - Icon ID in $sIconsFile for Unchecked item(s).
;
; Requirement(s):   #include <GuiListView.au3>, AutoIt 3.2.10.0.
;
; Return Value(s):  On seccess - Move selected item(s) Next/Back.
;                   On failure - Return "" (empty string) and set @error as following:
;                                                               1 - No selected item(s).
;                                                               2 - $iDirection is wrong value (not 1 and not -1).
;                                                               3 - Item(s) can not be moved, reached last/first item.
;
; Note(s):          * This function work with external ListView Control as well.
;                   * If you select like 15-20 (or more) items, moving them can take a while :( (depends on how many items moved).
;
; Author(s):        G.Sandler a.k.a CreatoR ([url="http://creator-lab.ucoz.ru"]http://creator-lab.ucoz.ru[/url])
;===============================================================================
Func My_GUICtrlListView_MoveItems($hWnd, $vListView, $iDirection = 1, $sIconsFile = "", $iIconID_Checked = 0, $iIconID_UnChecked = 0)
Local $hListView = $vListView
If Not IsHWnd($hListView) Then $hListView = ControlGetHandle($hWnd, "", $hListView)

Local $aSelected_Indices = _GUICtrlListView_GetSelectedIndices($hListView, 1)
If UBound($aSelected_Indices) < 2 Then Return SetError(1, 0, "")
If $iDirection <> 1 And $iDirection <> -1 Then Return SetError(2, 0, "")

Local $iTotal_Items = ControlListView($hWnd, "", $hListView, "GetItemCount")
Local $iTotal_Columns = ControlListView($hWnd, "", $hListView, "GetSubItemCount")

Local $iUbound = UBound($aSelected_Indices) - 1, $iNum = 1, $iStep = 1
Local $iCurrent_Index, $iUpDown_Index, $sCurrent_ItemText, $sUpDown_ItemText
Local $iCurrent_CheckedState, $iUpDown_CheckedState
Local $iCurrent_GroupId, $iUpDown_GroupId
Local $iCurrent_Param, $iUpDown_Param

If ($iDirection = -1 And $aSelected_Indices[1] = 0) Or _
   ($iDirection = 1 And $aSelected_Indices[$iUbound] = $iTotal_Items - 1) Then Return SetError(3, 0, "")

ControlListView($hWnd, "", $hListView, "SelectClear")

Local $aOldSelected_IDs[1]
Local $iIconsFileExists = FileExists($sIconsFile)

If $iIconsFileExists Then
  For $i = 1 To $iUbound
   ReDim $aOldSelected_IDs[UBound($aOldSelected_IDs) + 1]
   _GUICtrlListView_SetItemSelected($hListView, $aSelected_Indices[$i], True)
   $aOldSelected_IDs[$i] = GUICtrlRead($vListView)
   _GUICtrlListView_SetItemSelected($hListView, $aSelected_Indices[$i], False)
  Next
  ControlListView($hWnd, "", $hListView, "SelectClear")
EndIf

If $iDirection = 1 Then
  $iNum = $iUbound
  $iUbound = 1
  $iStep = -1
EndIf

For $i = $iNum To $iUbound Step $iStep
  $iCurrent_Index = $aSelected_Indices[$i]
  $iUpDown_Index = $aSelected_Indices[$i] + 1
  If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i] - 1

  $iCurrent_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iCurrent_Index)
  $iUpDown_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iUpDown_Index)

  $iCurrent_GroupId = _GUICtrlListView_GetItemGroupID($hListView, $iCurrent_Index)
  $iUpDown_GroupId = _GUICtrlListView_GetItemGroupID($hListView, $iUpDown_Index)

  $iCurrent_Param = _GUICtrlListView_GetItemParam($hListView, $iCurrent_Index)
  $iUpDown_Param = _GUICtrlListView_GetItemParam($hListView, $iUpDown_Index)

  _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)

  For $j = 0 To $iTotal_Columns - 1
   $sCurrent_ItemText = _GUICtrlListView_GetItemText($hListView, $iCurrent_Index, $j)
   $sUpDown_ItemText = _GUICtrlListView_GetItemText($hListView, $iUpDown_Index, $j)

   _GUICtrlListView_SetItemText($hListView, $iUpDown_Index, $sCurrent_ItemText, $j)
   _GUICtrlListView_SetItemText($hListView, $iCurrent_Index, $sUpDown_ItemText, $j)
  Next

  _GUICtrlListView_SetItemChecked($hListView, $iUpDown_Index, $iCurrent_CheckedState)
  _GUICtrlListView_SetItemChecked($hListView, $iCurrent_Index, $iUpDown_CheckedState)

  _GUICtrlListView_SetItemGroupID($hListView, $iUpDown_Index, $iCurrent_GroupId)
  _GUICtrlListView_SetItemGroupID($hListView, $iCurrent_Index, $iUpDown_GroupId)

  _GUICtrlListView_SetItemParam($hListView, $iUpDown_Index, $iCurrent_Param)
  _GUICtrlListView_SetItemParam($hListView, $iCurrent_Index, $iUpDown_Param)

  If $iIconsFileExists Then
   If $iCurrent_CheckedState = 1 Then
    GUICtrlSetImage(GUICtrlRead($vListView), $sIconsFile, $iIconID_Checked, 0)
   Else
    GUICtrlSetImage(GUICtrlRead($vListView), $sIconsFile, $iIconID_UnChecked, 0)
   EndIf

   If $iUpDown_CheckedState = 1 Then
    GUICtrlSetImage($aOldSelected_IDs[$i], $sIconsFile, $iIconID_Checked, 0)
   Else
    GUICtrlSetImage($aOldSelected_IDs[$i], $sIconsFile, $iIconID_UnChecked, 0)
   EndIf
  EndIf

  _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index, 0)
Next

For $i = 1 To UBound($aSelected_Indices) - 1
  $iUpDown_Index = $aSelected_Indices[$i] + 1
  If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i] - 1
  _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)
Next
EndFunc   ;==>My_GUICtrlListView_MoveItems

Func My_GUICtrlListView_MoveItemsFirst($hWnd, $vListView)
_GUICtrlListView_BeginUpdate($vListView)
While _GUICtrlListView_GetSelectedIndices($vListView) > 0
  My_GUICtrlListView_MoveItems($hWnd, $vListView, -1)
WEnd
_GUICtrlListView_EndUpdate($vListView)
EndFunc   ;==>My_GUICtrlListView_MoveItemsFirst

Func My_GUICtrlListView_MoveItemsLast($hWnd, $vListView)
_GUICtrlListView_BeginUpdate($vListView)
While _GUICtrlListView_GetSelectedIndices($vListView) < _GUICtrlListView_GetItemCount($vListView) - 1
  My_GUICtrlListView_MoveItems($hWnd, $vListView, 1)
WEnd
_GUICtrlListView_EndUpdate($vListView)
EndFunc   ;==>My_GUICtrlListView_MoveItemsLast

Share this post


Link to post
Share on other sites

#11 ·  Posted

just wanted to say thanks for the great UDF - works wonderfully.

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