Jump to content

Listview - Drag & Drop items/subitems - edit text of any item/subitem


careca
 Share

Recommended Posts

Hi, i decided to take on a personal challenge:

To enable drag and drop of items and subitems, and to allow editing of any item in the default listview with a double click.

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <GuiStatusBar.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
;=============================================================================
OnAutoItExitRegister("Quit")
Opt("TrayMenuMode", 1)
Opt("TrayIconHide", 0)
Opt("GUICoordMode", 0)
Opt("GUIResizeMode", 1)
Opt("TrayIconDebug", 1)
Opt("TrayAutoPause", 0)
Opt("MouseCoordMode", 2)
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)
Opt("GUIEventOptions", 1)
Opt("TrayOnEventMode", 1)
Opt("ExpandEnvStrings", 1)
Opt("WinTitleMatchMode", 3)
Opt("WinDetectHiddenText", 1)
;=============================================================================
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
Global $g_idListView, $g_hStatusBar, $g_iIndex = -1, $g_iSubIndex = -1
Global $hImage, $hGUI, $LVGui, $lvedit, $aHit, $WPos, $subrect, $Window, $WE = 0
Global $CursorInfo, $Drag0Based, $DragSub, $Drop0Based, $DropSub, $idMsg, $GetDragTxt, $GetDropTxt
Global $hDLL = DllOpen("user32.dll")
;=============================================================================
; Create GUI
$hGUI = GUICreate("ListView SubItem Hit Test", 400, 300)
$g_idListView = GUICtrlCreateListView("", 2, 2, 394, 268, $LVS_SINGLESEL, $LVS_EX_GRIDLINES+$LVS_EX_SUBITEMIMAGES+$LVS_EX_FULLROWSELECT)
$g_idListView = GUICtrlGetHandle($g_idListView) ; get the handle for use in the notify events
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "Drag", $hGUI)
GUISetOnEvent($GUI_EVENT_PRIMARYUP, "Drop", $hGUI)
;=============================================================================
; Load images
$hImage = _GUIImageList_Create()
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($g_idListView, 0xFF0000, 16, 16))
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($g_idListView, 0x00FF00, 16, 16))
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($g_idListView, 0x0000FF, 16, 16))
_GUICtrlListView_SetImageList($g_idListView, $hImage, 1)
; Add columns
_GUICtrlListView_AddColumn($g_idListView, "Column 1", 100)
_GUICtrlListView_AddColumn($g_idListView, "Column 2", 100)
_GUICtrlListView_AddColumn($g_idListView, "Column 3", 100)
; Add items
_GUICtrlListView_AddItem($g_idListView, "Row 1: Col 1", 0)
_GUICtrlListView_AddSubItem($g_idListView, 0, "Row 1: Col 2", 1, 1)
_GUICtrlListView_AddSubItem($g_idListView, 0, "Row 1: Col 3", 2, 2)
_GUICtrlListView_AddItem($g_idListView, "Row 2: Col 1", 1)
_GUICtrlListView_AddSubItem($g_idListView, 1, "Row 2: Col 2", 1, 2)
_GUICtrlListView_AddItem($g_idListView, "Row 3: Col 1", 2)
For $i = 4 To 30
    _GUICtrlListView_AddItem($g_idListView, "Row "&$i&": Col 1", Random(0, 2, 1))
Next
;=============================================================================
While 1
    If WinActive($LVGui) Then
        If _IsPressed('0D', $hDLL) Then
            _GUICtrlListView_SetItemText($g_idListView, $aHit[0], GUICtrlRead($lvedit), $aHit[1])
            GUICtrlDelete($lvedit)
            GUIDelete($LVGui)
        EndIf
    Else
        If WinExists($LVGui) Then
        GUICtrlDelete($lvedit)
        GUIDelete($LVGui)
        EndIf
    EndIf
    Sleep(100)
WEnd
;=============================================================================
Func Drag()
    $aHit = _GUICtrlListView_SubItemHitTest($g_idListView)
    If $aHit[0] <> -1 Then
        $Drag0Based = $aHit[0]
        $DragSub = $aHit[1]
        ConsoleWrite('# '&$Drag0Based&' - '& $DragSub&@CRLF)
    Else
        $Drag0Based = -1
        $DragSub = -1
    EndIf
EndFunc   ;==>ListView_Click
;=============================================================================
Func Drop()
    $aHit = _GUICtrlListView_SubItemHitTest($g_idListView)
    If $aHit[0] <> -1 Then
        $Drop0Based = $aHit[0]
        $DropSub = $aHit[1]
        ConsoleWrite('# '&$Drop0Based&' - '& $DropSub&@CRLF)
        If $Drag0Based <> -1 And $DragSub <> -1 Then
        ;=============================================================================
        $GetDragTxt = _GUICtrlListView_GetItemText($g_idListView, $Drag0Based, $DragSub)
        $GetDropTxt = _GUICtrlListView_GetItemText($g_idListView, $Drop0Based, $DropSub)
        _GUICtrlListView_SetItemText($g_idListView, $Drop0Based, $GetDragTxt, $DropSub)
        _GUICtrlListView_SetItemText($g_idListView, $Drag0Based, $GetDropTxt, $DragSub)
        EndIf
    EndIf
EndFunc   ;==>ListView_Click
;=============================================================================
Func ListView_Click()
    $aHit = _GUICtrlListView_SubItemHitTest($g_idListView)
    If $aHit[0] <> -1 Then
    $WPos = WinGetPos($hGUI)
    $subrect = _GUICtrlListView_GetSubItemRect($g_idListView, $aHit[0], $aHit[1], 0)
    $LVGui = GUICreate('input', 100, 20, $subrect[0] + $WPos[0], $subrect[1] + $WPos[1] + 30, $WS_POPUP, $WS_EX_TOPMOST)
    $lvedit = GUICtrlCreateInput(_GUICtrlListView_GetItemText($g_idListView, $aHit[0], $aHit[1]), 0, 0, $subrect[2] - $subrect[0], $subrect[3] - $subrect[1])
    GUISetState(@SW_SHOW, $LVGui)
    EndIf
EndFunc   ;==>ListView_Click
;=============================================================================
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $g_idListView
    If Not IsHWnd($g_idListView) Then $hWndListView = GUICtrlGetHandle($g_idListView)
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    ListView_Click()
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
;=============================================================================
Func Quit()
    Exit
EndFunc   ;==>Quit
;=============================================================================

So, the functions are:

Double click any item to edit it's text, enter to confirm.

Drag any cell and drop in any other to replace it.

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...