Jump to content

Moving a button within GUI with a hotkey


Dyson
 Share

Recommended Posts

I am attempting to recreate pong using Autoit, and I don't want any ideas on how to do this, i need to figure this out on my own... anyways, for now i have decided to use Guictrlcreatebutton for my pong paddles however the way that i thought about moving them isn't working and i've played around and can't figure it out. I will include a copy of my script and I hope that someone can figure out how to manipulate the button to move up and down.

CODE
#include <GUIConstantsEx.au3>

hotkeyset ("{down}", "_down")

hotkeyset ("{UP}", "_up")

func _Down()

EndFunc

func _up()

EndFunc

guicreate ("", 1020, 705)

guisetbkcolor (0x000000)

GUISetState (@sw_Show)

$start = GUICtrlCreateButton ("Start", 0, 0, 510, 60)

$exit = GUICtrlCreateButton ("Exit", 510, 0, 510, 60)

while 1

$msg = Guigetmsg()

select

case $msg = $Gui_Event_Close

msgbox (0, "Goodbye", "Goodbye", 2)

guidelete()

Exit

case $msg = $exit

msgbox (0, "Goodbye", "Goodbye", 2)

Guidelete()

Exit

case $msg = $start

ExitLoop

EndSelect

WEnd

guidelete()

guicreate ("", 1020, 710)

guisetbkcolor (0x000000)

guisetstate (@sw_Show)

$top = 150

if _down() Then

$top +=5

ElseIf _up() Then

$top -=5

EndIf

$paddle1 = guictrlcreatebutton ("", 0, $top, 10, 80)

$paddle2 = Guictrlcreatebutton ("", 1010, 540, 10, 80)

while 1

$msg = Guigetmsg()

Select

case $msg = $Gui_Event_Close

guidelete()

Exit

EndSelect

WEnd

Link to comment
Share on other sites

Little example:

#include <GUIConstantsEx.au3>

HotKeySet('{LEFT}', 'MoveButton')
HotKeySet('{RIGHT}', 'MoveButton')
HotKeySet('{DOWN}', 'MoveButton')
HotKeySet('{UP}', 'MoveButton')

Global $Increase = 50
Global $Width = 400
Global $Height = 400


$MyGUI = GUICreate('Moving Button', $Width, $Height)
$Button = GUICtrlCreateButton('Move', 50, 50, 50, 30)
GUISetState()


While 1
    $nMsg = GUIGetMsg()
    If $nMsg = $GUI_EVENT_CLOSE Then
        Exit
    EndIf
WEnd

Func MoveButton()
    $Pos = ControlGetPos($MyGUI, '', $Button)
    $Command = @HotKeyPressed
    Switch $Command
        Case '{LEFT}'
            If $Pos[0] >= $Increase Then
                GUICtrlSetPos($Button, $Pos[0] - $Increase, $Pos[1])
            ElseIf $Pos[0] - $Increase < $Increase Then
                GUICtrlSetPos($Button, 0, $Pos[1])
            EndIf
        Case '{RIGHT}'
            If $Pos[0] + $Pos[2] <= $Width - $Increase Then
                GUICtrlSetPos($Button, $Pos[0] + $Increase, $Pos[1])
            ElseIf $Pos[0] + $Pos[2] >= $Width - $Increase Then
                GUICtrlSetPos($Button, $Width - $Pos[2], $Pos[1])
            EndIf
            
        Case '{DOWN}'
            If $Pos[1] + $Pos[3] <= $Height - $Increase Then
                GUICtrlSetPos($Button, $Pos[0], $Pos[1] + $Increase)
            ElseIf $Pos[1] + $Pos[3] >= $Height - $Increase Then
                GUICtrlSetPos($Button, $Pos[0], $Height - $Pos[3])
            EndIf
        Case '{UP}'
            If $Pos[1] >= $Increase Then
                GUICtrlSetPos($Button, $Pos[0], $Pos[1] - $Increase)
            ElseIf $Pos[1] <= $Increase Then
                GUICtrlSetPos($Button, $Pos[0], 0)
            EndIf
    EndSwitch

EndFunc ;==>MoveButton
Edited by oMBra
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...