Sign in to follow this  
Followers 0
_Kurt

[UDF] Drag Your GUI Controls

9 posts in this topic

#1 ·  Posted (edited)

Hello,

A little while ago I had written this, and I thought some of you may find it useful and interesting. I'm not sure if anyone has created something similar, I don't visit the forums much anymore, so forgive me if something like this has already been done. I included an optional parameter to change the cursorID while it is being dragged (look up GUICtrlSetCursorIcon in the helpfile for more details). I also added extra parameters to "trap" the control using X, Y, Width, Height. The dragging ability is very smooth. The UDF simply took some simple math work to figure out, if you follow it carefully it's pretty easy to understand.

This function is to be used within your While..WEnd Loop

_GUICtrlDrag($GUIHandle, $Control [[, $CursorMode = 2], $X="*", $Y="*", $W="*", $H="*"])

$GUIHandle - GUI's Handle

$Control - The Control you wish to be draggable

$CursorMode - (Optional) While Control is being dragged, change cursorID, see GUICtrlSetCursorIcon for more details

$X, $Y, $W, $H - (Optional) If you wish to "trap" your Control, specify the coordinates and size

==> If you wish to have a grid-snap scale when dragging your controls, check out the slightly modified version of this function so you can control the scale (20x20,10x10,etc.). So take a look at post 9, or if you're too lazy, just click here.

UDF:

;===============================================================================
;
; Description:      Makes A Control Draggable
; Syntax:           _GUICtrlDrag($GUIHandle, $Control [[, $CursorMode = 2], $X="*", $Y="*", $W="*", $H="*"])
; Parameter(s):     $GUIHandle     - The Handle of your GUI
;                   $Control       - The Control you wish to make Draggable
;                   $CursorMode    - (Optional) While Control is being dragged, change
;                                    cursor icon, see GUICtrlSetCursorIcon for more details
;                   $X, $Y, $W, $H - (Optional) If you wish to "trap" your Control, specify the coordinates and size
; Requirement(s):   None
; Return Value(s):  None
; Author(s):        Kurt (a.k.a. _Kurt)
; Note(s):          Function should be added in within the While..WEnd Loop
;
;===============================================================================
Func _GUICtrlDrag($GUIHandle, $Control, $CursorMode = 2, $X="*", $Y="*", $W="*", $H="*")
    Select
        Case $msg = $Control
            GUICtrlSetCursor($Control, $CursorMode)
            $pos = ControlGetPos($GUIHandle, "", $Control)
            $cursor = GUIGetCursorInfo()
            $XStayPos = $cursor[0]-$pos[0]
            $YStayPos = $cursor[1]-$pos[1]
            Do
                Sleep(5)
                $cursor = GUIGetCursorInfo()
                $sX = $cursor[0]-$XStayPos
                $sY = $cursor[1]-$YStayPos
                If $X <> "*" Then
                    If $sY < $Y Then $sY = $Y
                    If $sY > $H-$pos[3] Then $sY = $H-$pos[3]
                    If $sX < $X Then $sX = $X
                    If $sX > $W-$pos[2] Then $sX = $W-$pos[2]
                EndIf
                GUICtrlSetPos($Control, $sX, $sY)
            Until $cursor[2] = 0
            GUICtrlSetCursor($Control, 2)
    EndSelect
EndFuncoÝ÷ ØLZ^jëh×6#include <GUIConstants.au3>

$GUI = GUICreate("..Drag Test..")
GUISetBkColor(0x0000ff)
$item1 = GUICtrlCreateLabel("         Drag Me", 15, 15, 100, 20)
GUICtrlSetBkColor(-1, 0xff0000)
GUISetState()

While 1
    Sleep(10)
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
    _GUICtrlDrag($GUI, $item1, 9, 10, 10, 300, 200)
WEnd

I added an example so people can get the idea on how to use it. In this example, I've used the optional parameters to change the cursorID, and trap it in a box at (10,10,300,200) - X,Y,Width,Height.

Hope everything is clear, if you have any ideas/suggestions/feedback, post 'em :)

Enjoy,

Kurt

EDIT: Updated Avatar :)

Edited by _Kurt

Awaiting Diablo III..

Share this post


Link to post
Share on other sites



Nice idea.

Keep it going :)

Share this post


Link to post
Share on other sites

Thanks for the replies, guys.

WB _Kurt did something similar using a gutted version of the GuiBuilder

http://www.autoitscript.com/forum/index.ph...st&p=227342

Heh, that's cool. I like the idea of having a Grid Snap, and it wouldn't be too hard to make :) But the problem is that some people might not want the grid snap, and other might not a 10x10 grid. And if I add another parameter to that function I have a feeling it will look kind of ugly since I hate functions with several parameters.

Ugh :)

Kurt


Awaiting Diablo III..

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I came up with this surprisingly very quick, but here's the (ugly) UDF. Now it looks like this..

_GUICtrlDrag($GUIHandle, $Control, [[[$CursorMode = 2], $GridScale = 0], $X="*", $Y="*", $W="*", $H="*"])

And the UDF

Func _GUICtrlDrag($GUIHandle, $Control, $CursorMode = 2, $GridScale = 0, $X="*", $Y="*", $W="*", $H="*")
    Select
        Case $msg = $Control
            GUICtrlSetCursor($Control, $CursorMode)
            $pos = ControlGetPos($GUIHandle, "", $Control)
            $cursor = GUIGetCursorInfo()
            $XStayPos = $cursor[0]-$pos[0]
            $YStayPos = $cursor[1]-$pos[1]
            Do
                Sleep(5)
                $cursor = GUIGetCursorInfo()
                $sX = $cursor[0]-$XStayPos
                $sY = $cursor[1]-$YStayPos
                If $GridScale <> 0 Then
                    $sX = Round($sX/$GridScale)*$GridScale
                    $sY = Round($sY/$GridScale)*$GridScale
                EndIf
                If $X <> "*" Then
                    If $sY < $Y Then $sY = $Y
                    If $sY > $H-$pos[3] Then $sY = $H-$pos[3]
                    If $sX < $X Then $sX = $X
                    If $sX > $W-$pos[2] Then $sX = $W-$pos[2]
                EndIf
                GUICtrlSetPos($Control, $sX, $sY)
            Until $cursor[2] = 0
            GUICtrlSetCursor($Control, 2)
    EndSelect
EndFuncoÝ÷ Ù8^5ìÅ©©ì"¶¶Ó´±Æ¥y«­¢+Ø¥¹±Õ±ÐíU%
½¹ÍѹÑ̹ÔÌÐì((ÀÌØíU$ôU%
ÉÑ ÅÕ½Ð츹ÉQÍи¸ÅÕ½Ðì¤)U%MÑ    ­
½±½È ÁàÀÀÀÁ¤(ÀÌØí¥Ñ´ÄôU%
Ñɱ
ÉÑ1° ÅÕ½ÐìÉ5ÅÕ½Ðì°ÄÔ°ÄÔ°ÄÀÀ°ÈÀ¤)U%
ÑɱMÑ   ­
½±½È ´Ä°ÁáÀÀÀÀ¤)U%MÑMÑÑ ¤()]¡¥±Ä(%M±À ÄÀ¤($ÀÌØíµÍôU%Ñ5Í ¤(%M±Ð($%
ÍÀÌØíµÍôÀÌØíU%}Y9Q}
1=M($$%á¥Ð(%¹M±Ð(%}U%
ÑÉ±É ÀÌØíU$°ÀÌØí¥Ñ´Ä°ä°ÈÀ°ÄÀ°ÄÀ°ÌÀÀ°ÈÀÀ¤)]¹

See, too many parameters, it's too confusing! But on the bright side, the scale works marvelously :)

Kurt

EDIT: Just wanted to add in that leaving the scale at 0 will keep the drags unscaled. Also wanted to ask you guys if this function would be better like this.

Edited by _Kurt

Awaiting Diablo III..

Share this post


Link to post
Share on other sites

Thanks for the replies, guys.

Heh, that's cool. I like the idea of having a Grid Snap, and it wouldn't be too hard to make :) But the problem is that some people might not want the grid snap, and other might not a 10x10 grid. And if I add another parameter to that function I have a feeling it will look kind of ugly since I hate functions with several parameters.

Ugh :)

Kurt

That's why I made it so they could turn those on/off, also the drag control can be turned on/off.


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 (edited)

Yes, that's true.

I just wanted to add the option, if anyone uses this function it will be easy to control the dragging by an If..EndIf statement in the While..WEnd loop.

Kurt

EDIT: 600 posts :)

Edited by _Kurt

Awaiting Diablo III..

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

How would I use this to detect if a control is dragged over a specific area?

EDIT: Nevermind, figured it out :rolleyes:

Edited by unknown12

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