Jump to content

Drag & Drop a GUI element


jolumaoj
 Share

Recommended Posts

Hello,

I need to make a GUI where I can move some elements (like a label) to a target area, like making a puzzle. Furthermore, I would like that the target area could recognize what elements has been released over it.

Could anyone say me if this is possible?

Thanks in advance.

Link to comment
Share on other sites

  • Moderators

jolumaoj,

Welcome to the AutoIt forum. :)

Yes it is quite possible - as you can see here: ;)

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>

; Mouse coords relative to GUI client area
Opt("MouseCoordMode", 2)

; Set target coords
Global $iTgt_Left = 10, $iTgt_Right = 210, $iTgt_Top = 10, $iTgt_Bot = 110

; Create GUI
$hGUI = GUICreate("Test", 300, 200)

$cTarget = GUICtrlCreateLabel("", $iTgt_Left, $iTgt_Top, $iTgt_Right - $iTgt_Left, $iTgt_Bot - $iTgt_Top, $SS_BLACKFRAME)
GUICtrlSetState(-1, $GUI_DISABLE)

$cLabel = GUICtrlCreateLabel("Move me", 10, 150, 60, 20)
GUICtrlSetBkColor(-1, 0x00FF00)

$cButton = GUICtrlCreateButton("Me too", 110, 150, 80, 23)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN
            ; If the mouse button is pressed - get info about where
            $cInfo = GUIGetCursorInfo($hGUI)
            ; Is it over a control
            $iControl = $cInfo[4]
            Switch $iControl
                ; If it is a control we want to move
                Case $cLabel, $cButton
                    ; Work out offset of mouse on control
                    $aPos = ControlGetPos($hGUI, "", $iControl)
                    $iSubtractX = $cInfo[0] - $aPos[0]
                    $iSubtractY = $cInfo[1] - $aPos[1]
                    ; And then move the control until the mouse button is released
                    Do
                        $cInfo = GUIGetCursorInfo($hGUI)
                        ControlMove($hGUI, "", $iControl, $cInfo[0] - $iSubtractX, $cInfo[1] - $iSubtractY)
                    Until Not $cInfo[2]
                    ; See if the mouse was released over the target
                    $aMPos = MouseGetPos()
                    If $aMPos[0] > $iTgt_Left And $aMPos[0] < $iTgt_Right Then
                        If $aMPos[1] > $iTgt_Top And $aMPos[1] < $iTgt_Bot Then
                            Switch $iControl
                                Case $cLabel
                                    $sItem = "label"
                                Case $cButton
                                    $sItem = "button"
                            EndSwitch
                            MsgBox(0, "Info", "Over target with " & $sItem)
                        EndIf
                    EndIf

            EndSwitch
    EndSwitch
WEnd

Please ask if you have any questions. :)

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

  • Moderators

jolumaoj,

Glad I could help. :)

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

An idea to make it look cooler ;)

After the ControlGetPos() use GDI to draw an outline of the button, and on mouse move event, redraw the image to make it look like you're dragging it XD

ooo and disable the control so its grayed out while you're moving it.

Edited by mechaflash213
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

  • 1 year later...

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