Jump to content

Set Drag & Drop items for your ListBox'es


MrCreatoR
 Share

Recommended Posts

Hi,

This script was inspired by the example of PaulIA here.

I made it easy to set dragable items for specific ListBox control, you can unset them to :idea:

Here is an example:

#include <GuiConstantsEx.au3>
#include <DragList_UDF.au3>

$hGUI = GUICreate("_DragList_... Demo", 400, 200)

$nList1 = GUICtrlCreateList("", 20, 10, 160, 140, $WS_BORDER+$WS_VSCROLL)
GUICtrlSetData($nList1, "Hi,|How|Are|You?")

$nList2 = GUICtrlCreateList("", 220, 10, 160, 140, $WS_BORDER+$WS_VSCROLL)
GUICtrlSetData($nList2, "AutoIt|Is|The|Best!")

$SetList1_CheckBox = GUICtrlCreateCheckBox("Set List 1", 60, 160, 70, 20)
$SetList2_CheckBox = GUICtrlCreateCheckBox("Set List 2", 260, 160, 70, 20)

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $SetList1_CheckBox
            If GUICtrlRead($SetList1_CheckBox) = $GUI_CHECKED Then
                _DragList_SetList($nList1, $hGUI)
            Else
                _DragList_SetList($nList1)
            EndIf
        Case $SetList2_CheckBox
            If GUICtrlRead($SetList2_CheckBox) = $GUI_CHECKED Then
                _DragList_SetList($nList2, $hGUI)
            Else
                _DragList_SetList($nList2)
            EndIf
    EndSwitch
Wend

Just set the checkboxes, and try to drag the items :(.

This example + DragList_UDF.au3 (functions and the main handler) attached as zip file:

_DragList_UDF.zip

Enjoy! :P

Edited by MrCreatoR

 

Spoiler

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

Spoiler

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

Link to comment
Share on other sites

  • 3 months later...

Exactly what I was looking for. As always thank you :)

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

is it possible to select lets say a group of 5 items and move them all at once?

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

  • 3 years later...

You shouldn't be commenting on every ancient topic that helps you. Necroposting is a big no-no.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • 1 month later...
  • Moderators

Spenhouet,

No, this UDF deals with ListBoxes - ListViews are a very different beast. If you want to drag items within a ListView, take a look at the GUIListViewEx UDF in my sig. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 4 years later...

Replace the old version of _DragList_GetItemFromPoint with this new version:

#cs
Func _DragList_GetItemFromPoint($hList, $stPoint, $iAutoscroll = 0)
  ;The return value contains the index of the nearest item in the low-order word.
  ;The high-order word is zero if the specified point is in the client area of the list box,
  ;or one if it is outside the client area.>>>Note. this call to sendmessage always results in the same number<<<
  Local $iX = DllStructGetData($stPoint, 1)
  Local $iY = DllStructGetData($stPoint, 2)
  
  Local $aRet = DllCall("comctl32.dll", "int", "LBItemFromPt", "hwnd", $hList, "int", $iX, "int", $iY,  "int", $iAutoscroll)
  Return $aRet[0]
EndFunc
#ce

Func _DragList_GetItemFromPoint($hList, $stPoint, $iAutoscroll = 0)
  ;The return value contains the index of the nearest item in the low-order word.
  ;The high-order word is zero if the specified point is in the client area of the list box,
  ;or one if it is outside the client area.>>>Note. this call to sendmessage always results in the same number<<<
  Local $tPOINT64 = DllStructCreate( "int64", DllStructGetPtr( $stPoint ) )
  
  Local $aRet = DllCall("comctl32.dll", "int", "LBItemFromPt", "hwnd", $hList, "int64", DllStructGetData( $tPOINT64, 1 ), "int", $iAutoscroll)
  Return $aRet[0]
EndFunc

The new version works on both 32 and 64 bit.

Link to comment
Share on other sites

2 hours ago, LarsJ said:

Replace the old version of _DragList_GetItemFromPoint with this new version:

#cs
Func _DragList_GetItemFromPoint($hList, $stPoint, $iAutoscroll = 0)
  ;The return value contains the index of the nearest item in the low-order word.
  ;The high-order word is zero if the specified point is in the client area of the list box,
  ;or one if it is outside the client area.>>>Note. this call to sendmessage always results in the same number<<<
  Local $iX = DllStructGetData($stPoint, 1)
  Local $iY = DllStructGetData($stPoint, 2)
  
  Local $aRet = DllCall("comctl32.dll", "int", "LBItemFromPt", "hwnd", $hList, "int", $iX, "int", $iY,  "int", $iAutoscroll)
  Return $aRet[0]
EndFunc
#ce

Func _DragList_GetItemFromPoint($hList, $stPoint, $iAutoscroll = 0)
  ;The return value contains the index of the nearest item in the low-order word.
  ;The high-order word is zero if the specified point is in the client area of the list box,
  ;or one if it is outside the client area.>>>Note. this call to sendmessage always results in the same number<<<
  Local $tPOINT64 = DllStructCreate( "int64", DllStructGetPtr( $stPoint ) )
  
  Local $aRet = DllCall("comctl32.dll", "int", "LBItemFromPt", "hwnd", $hList, "int64", DllStructGetData( $tPOINT64, 1 ), "int", $iAutoscroll)
  Return $aRet[0]
EndFunc

The new version works on both 32 and 64 bit.

Wow! Thanks for that :)

But now I have a different kind of problem.
When using this UDF it seems like the style $LBS_EXTENDEDSEL has to be turned off in order to see what item is selected. (blue highlighted)

But I need to create the ListBox with this style: 

$List = GUICtrlCreateList("", 40, 104, 193, 227, BitOR($WS_BORDER, $WS_VSCROLL, $LBS_EXTENDEDSEL))

to use some functions like "_GUICtrlListBox_GetSelItems($List)"

Any suggestions on this one? :)

Link to comment
Share on other sites

  • 1 year later...
  • Moderators

@x_bennY do you honestly believe this is enough information for someone to help you?? How about providing your code, rather than asking us to guess?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

This is not an error. The code is designed to work that way.

When you move items in a listbox with the mouse, you can either drop items above or below the target item. Here it's chosen to drop items above the target item with the consequence that you cannot drop items below last item (only above last item).

You could also have chosen to drop items below the target item. Then there would have been a similar problem with the first item.

Solution. Drop your item just above the last item. Move the last item one item up.

Link to comment
Share on other sites

I found a solution searching here on autoit forum.

 

#include <Constants.au3>
#include <GUIListBox.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global $gnDRAGLISTMSGSTRING = _WinAPI_RegisterWindowMessage("commctrl_DragListMsg")
Global $DL_BEGINDRAG = $WM_USER + 133
Global $DL_DRAGGING = $WM_USER + 134
Global $DL_DROPPED = $WM_USER + 135
Global $DL_CANCELDRAG = $WM_USER + 136
Global Enum $DL_STOPCURSOR = 1, $DL_COPYCURSOR, $DL_MOVECURSOR
Global $gtDRAGLISTINFO = "long uNotification;long hWnd;long x;long y"
Global $gfItemAdded = False

Global $hMain = GUICreate("DragList", 200, 400)
Global $cListbox = GUICtrlCreateList("", 16, 16, 168, 368, $WS_BORDER + $WS_VSCROLL)
GUICtrlSetFont($cListbox, 10, Default, Default, "Tahoma")
Global $hListbox = GUICtrlGetHandle($cListbox)
GUICtrlSetData($cListbox, "Apples|Oranges|Bananas|Pears|Grapefruits|Limes|Lemons|Strawberries|Plums|Melons|Grapes|")
GUISetState()

_ComCtl32_MakeDragList($hListbox)

Global $wProcNew = DllCallbackRegister("_MyWndProc", "int", "hwnd;int;wparam;lparam")
Global $wProcOld = _WinAPI_SetWindowLong($hMain, $GWL_WNDPROC, DllCallbackGetPtr($wProcNew))


While GUIGetMsg() <> -3
    Sleep(10)
Wend

_WinAPI_SetWindowLong($hMain, $GWL_WNDPROC, DllCallbackGetPtr($wProcOld))
Exit

Func _MyWndProc($hWnd, $nMsg, $wParam, $lParam)
    Local $aRet, $nOldIndex, $sItemText
    If $nMsg = $gnDRAGLISTMSGSTRING Then
        Local $tDRAGLISTINFO = DllStructCreate($gtDRAGLISTINFO, $lParam)
        Local $uNotification = DllStructGetData($tDRAGLISTINFO, "uNotification")
        Local $x = DllStructGetData($tDRAGLISTINFO, "x"), $y = DllStructGetData($tDRAGLISTINFO, "y")
        Local $nItem = _ComCtl32_LBItemFromPt($hListbox, $x, $y)
        Switch $uNotification
            Case $DL_BEGINDRAG
                If $nItem < (_GUICtrlListBox_GetCount($hListbox) - 1) Then
                    _GUICtrlListBox_AddString($hListbox, "")
                    $gfItemAdded = True
                EndIf
                Return 1
            Case $DL_DRAGGING
                _ComCtl32_DrawInsert($hMain, $hListbox, $nItem)
                If $nItem = _GUICtrlListBox_GetCurSel($hListbox) Then Return $DL_STOPCURSOR
                Return $DL_MOVECURSOR
            Case $DL_DROPPED
                If $nItem > -1 Then
                    $nOldIndex = _GUICtrlListBox_GetCurSel($hListbox)
                    If $nItem <> $nOldIndex Then
                        $sItemText = _GUICtrlListBox_GetText($hListbox, $nOldIndex)
                        If $nItem < $nOldIndex Then $nOldIndex += 1
                        _GUICtrlListBox_InsertString($hListbox, $sItemText, $nItem)
                        _GUICtrlListBox_DeleteString($hListbox, $nOldIndex)
                        If $nItem > $nOldIndex Then $nItem -= 1
                        _GUICtrlListBox_SetCurSel($hListbox, $nItem)
                    EndIf
                EndIf
                If $gfItemAdded Then
                    _GUICtrlListBox_DeleteString($hListbox, _GUICtrlListBox_GetCount($hListbox) - 1)
                    $gfItemAdded = False
                EndIF
                _ComCtl32_DrawInsert($hMain, $hListbox, -1)
                Return 0
            Case $DL_CANCELDRAG
                If $gfItemAdded Then
                    _GUICtrlListBox_DeleteString($hListbox, _GUICtrlListBox_GetCount($hListbox) - 1)
                    $gfItemAdded = False
                EndIF
                _ComCtl32_DrawInsert($hMain, $hListbox, -1)
                Return 0
        EndSwitch
    EndIf
    Return _WinAPI_CallWindowProc($wProcOld, $hWnd, $nMsg, $wParam, $lParam)
EndFunc

Func _ComCtl32_MakeDragList($hWnd)
    Local $aRet = DllCall("comctl32.dll", "int", "MakeDragList", "hwnd", $hWnd)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aRet[0]
EndFunc

Func _ComCtl32_LBItemFromPt($hWnd, $x, $y)
    Local $aRet = DllCall("comctl32.dll", "long", "LBItemFromPt", "hwnd", $hWnd, "long", $x, "long", $y, "long", 1)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aRet[0]
EndFunc

Func _ComCtl32_DrawInsert($hWndParent, $hWnd, $nItem)
    DllCall("comctl32.dll", "none", "DrawInsert", "hwnd", $hWndParent, "hwnd", $hWnd, "long", $nItem)
    If @error Then Return SetError(@error, @extended, 0)
    Return
EndFunc

 

 

 

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