Jump to content



Photo

Drag-and-drop reordering listbox


  • Please log in to reply
6 replies to this topic

#1 WideBoyDixon

WideBoyDixon

    Code Monkey

  • Active Members
  • PipPipPipPipPipPip
  • 381 posts

Posted 29 June 2009 - 11:32 AM

I had a quick look around and couldn't find this anywhere so I'm posting up some code. This allows you to drag-and-drop the items in the listbox in order to reorder them. A nice UI touch when you want users to have the ability to do this. I'm writing something right now that will use this but thought I'd put this code here in case someone wants to use it.

Plain Text         
#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 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


Enjoy.

WBD







#2 UEZ

UEZ

    Never say never

  • MVPs
  • 3,694 posts

Posted 29 June 2009 - 03:22 PM

Nice code example! Well done again!

UEZ

 
The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯


#3 xeroTechnologiesLLC

xeroTechnologiesLLC

    Prodigy

  • Active Members
  • PipPipPip
  • 158 posts

Posted 11 April 2012 - 05:32 PM

I like, only difficulty I am finding is that it doesn't shut down correctly - seems to crash.
I appreciate the hard work done on this code all the same.

I'm working a project that this will be invaluable!
+1

#4 Spenhouet

Spenhouet

    Seeker

  • Active Members
  • 37 posts

Posted 05 June 2012 - 08:16 AM

I used ure code but... where is $GWL_WNDPROC declared?

#5 UEZ

UEZ

    Never say never

  • MVPs
  • 3,694 posts

Posted 05 June 2012 - 09:17 AM

Global Const $GWL_WNDPROC = 0xFFFFFFFC (see Constants.au3).

Br,
UEZ

 
The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯


#6 Spenhouet

Spenhouet

    Seeker

  • Active Members
  • 37 posts

Posted 05 June 2012 - 12:29 PM

ah thx.
But i have to use this:
http://www.autoitscript.com/forum/topic/124980-guilistviewex-new-version-26-aug-11/page__p__867799#entry867799
because of ListView.
Thx to Melba in this case ;)

#7 Belini

Belini

    Polymath

  • Active Members
  • PipPipPipPip
  • 245 posts

Posted 05 June 2012 - 01:13 PM

Great job @ WideBoyDixon, sure I'll use your UDF.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users