Jump to content

Controlgetpos() and Guictrlsetpos()


Recommended Posts

I had this problem before and JohnOne helped me with some code but I failed to make it work for all 4 sides. I took the same concept and tried to simplify it but i'm having an issue.

With the code below is it possible to have it stop at the correct X and Y coords but still move below the specified X and Y.

Just use the code below and you might understand my question a little better.

#include<misc.au3>
#include<file.au3>
#include<guiconstants.au3>
$gui = GUICreate("Gui",400,400)
$input = GUICtrlCreateInput("",200,200,50,50)
$box = GUICtrlCreateInput("",20,20,50,50)
GUICtrlSetState($input,$gui_disable)
GUICtrlSetState($box,$gui_disable)
GUICtrlSetBkColor($box,0xFF0000)
GUICtrlSetBkColor($input,0x000000)
GUISetState()

while 1
   If _IsPressed("25") Then
      $cp = ControlGetPos($gui,"",$box)
      $cp2 = $cp[0] - 1
      GUICtrlSetPos($box,$cp2,$cp[1])
   EndIf
   If _IsPressed("26") Then
      $cp = ControlGetPos($gui,"",$box)
      $cp2 = $cp[1] - 1
      GUICtrlSetPos($box,$cp[0],$cp2)
   EndIf
   If _IsPressed("27") Then
      $cp = ControlGetPos($gui,"",$box)
      If down2() Then
      $cp2 = $cp[0] + 1
      GUICtrlSetPos($box,$cp2,$cp[1])
      EndIf
   EndIf
   If _IsPressed("28") Then
      $cp = ControlGetPos($gui,"",$box)
      If down3() Then
      $cp2 = $cp[1] + 1
      GUICtrlSetPos($box,$cp[0],$cp2)
      EndIf
      EndIf
   $msg = GUIGetMsg(1)
   Switch $msg[1]
   Case $gui
      Switch $msg[0]
      Case -3
         Exit
      EndSwitch
   EndSwitch
WEnd

Func down()
   $side_1 = $cp[1] + $cp[3]
   $side_2 = $cp[0] + $cp[2]
   $side_3 = $cp[0]
   If $side_1 >= 200 And $side_2 >= 200 And $side_3 <= 250 Then
      Return False
      EndIf
      Return True
   EndFunc

   Func down2()
      $obj = ControlGetPos($gui,"",$input)
      If $cp[0] = 200 Then
         Return False
      EndIf
      Return True
   EndFunc

      Func down3()
      $obj = ControlGetPos($gui,"",$input)
      $h = $obj[1] + 50
      If $cp[1] = 150 Then
         Return False
      EndIf
      Return True
      EndFunc

Thanks.

Link to comment
Share on other sites

Yes I read your answer and by using arrays do you mean set a variable to controlgetpos and say the variable is $cp you would set it as $cp[0] for x, $cp[1] for y, $cp[2] for width and $cp[3] for height. If I'm wrong about that please let me know and show an example of what you're saying because I'm on my phone and it's kind of difficult to type.

Thanks.

Link to comment
Share on other sites

Here you go :

#include <Misc.au3>
#include <GUIConstantsEx.au3>
#include <ColorConstants.au3>

Local $aBoxPos[4] = [20, 20, 50, 50]
Local Enum $__BOXPOS_LEFT, $__BOXPOS_TOP, $__BOXPOS_WIDTH, $__BOXPOS_HEIGHT

Local $aBoxBounds[4] = [0, 0, 400, 400]
Local Enum $__BOXBOUNDS_LEFT, $__BOXBOUNDS_TOP, $__BOXBOUNDS_RIGHT, $__BOXBOUNDS_BOTTOM

#Region GUI
Local $hGUI = GUICreate("GUI", 400, 400)

Local $iBox = GUICtrlCreateInput("", $aBoxPos[$__BOXPOS_LEFT], $aBoxPos[$__BOXPOS_TOP], $aBoxPos[$__BOXPOS_WIDTH], $aBoxPos[$__BOXPOS_HEIGHT])
GUICtrlSetState($iBox, $GUI_DISABLE)
GUICtrlSetBkColor($iBox, $COLOR_RED)

Local $iBox2 = GUICtrlCreateInput("", 200, 200, 50, 50)
GUICtrlSetState($iBox2, $GUI_DISABLE)
GUICtrlSetBkColor($iBox2, $COLOR_BLACK)

GUISetState(@SW_SHOW, $hGUI)
#EndRegion GUI

Local $iMsg = 0
Local $fMove = False

While 1
    If _IsPressed("25") Then ;LEFT
        If $aBoxPos[$__BOXPOS_LEFT] > $aBoxBounds[$__BOXBOUNDS_LEFT] Then
            $aBoxPos[$__BOXPOS_LEFT] -= 1
            $fMove = True
        EndIf
    EndIf
    If _IsPressed("26") Then ;UP
        If $aBoxPos[$__BOXPOS_TOP] > $aBoxBounds[$__BOXBOUNDS_TOP] Then
            $aBoxPos[$__BOXPOS_TOP] -= 1
            $fMove = True
        EndIf
    EndIf
    If _IsPressed("27") Then ;RIGHT
        If $aBoxPos[$__BOXPOS_LEFT] + $aBoxPos[$__BOXPOS_WIDTH] < $aBoxBounds[$__BOXBOUNDS_RIGHT] Then
            $aBoxPos[$__BOXPOS_LEFT] += 1
            $fMove = True
        EndIf
    EndIf
    If _IsPressed("28") Then ;DOWN
        If $aBoxPos[$__BOXPOS_TOP] + $aBoxPos[$__BOXPOS_HEIGHT] < $aBoxBounds[$__BOXBOUNDS_BOTTOM] Then
            $aBoxPos[$__BOXPOS_TOP] += 1
            $fMove = True
        EndIf
    EndIf

    If $fMove Then
        GUICtrlSetPos($iBox, $aBoxPos[$__BOXPOS_LEFT], $aBoxPos[$__BOXPOS_TOP])
        $fMove = False
    EndIf

    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

GUIDelete($hGUI)

Tell me if you want an anti collision with the black box.

Br, FireFox.

Link to comment
Share on other sites

Sorry for posting here but I thought this would be easier seeing that it's related to it.

I've been messing around with this for a couple days now and I finally managed to get it to work on all 4 sides and work the way I intended for it to work.

I'm really happy that I got it to work but I was wondering if there is an easier way to do this. I had to put the numbers 1 pixel less than its corresponding friend because it would return false other wise.

If you think or know an easier way to do this please let me know :thumbsup:

#include<misc.au3>
#include<file.au3>
#include<guiconstants.au3>
$gui = GUICreate("Gui",400,400)
$input = GUICtrlCreateInput("",200,200,50,50)
$box = GUICtrlCreateInput("",200,20,50,50)
GUICtrlSetState($input,$gui_disable)
GUICtrlSetState($box,$gui_disable)
GUICtrlSetBkColor($box,0xFF0000)
GUICtrlSetBkColor($input,0x000000)
GUISetState()

while 1
   If _IsPressed("25") Then
      $cp = ControlGetPos($gui,"",$box)
      If down1() Then
      $cp2 = $cp[0] - 1
      GUICtrlSetPos($box,$cp2,$cp[1])
      EndIf
   EndIf
   If _IsPressed("26") Then
      $cp = ControlGetPos($gui,"",$box)
      If down2() Then
      $cp2 = $cp[1] - 1
      GUICtrlSetPos($box,$cp[0],$cp2)
      EndIf
   EndIf
   If _IsPressed("27") Then
      $cp = ControlGetPos($gui,"",$box)
      If down3() Then
      $cp2 = $cp[0] + 1
      GUICtrlSetPos($box,$cp2,$cp[1])
      EndIf
   EndIf
   If _IsPressed("28") Then
      $cp = ControlGetPos($gui,"",$box)
      If down4() Then
      $cp2 = $cp[1] + 1
      GUICtrlSetPos($box,$cp[0],$cp2)
      EndIf
      EndIf
   $msg = GUIGetMsg(1)
   Switch $msg[1]
   Case $gui
      Switch $msg[0]
      Case -3
         Exit
      EndSwitch
   EndSwitch
WEnd

         Func down1()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 200 And $b >= 200 And $c <= 251 And $d <= 250 Then
         Return False
      EndIf
      Return True
   EndFunc

         Func down2()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 250 And $b >= 200 And $c <= 250 And $d <= 251 Then
         Return False
      EndIf
      Return True
   EndFunc

      Func down3()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 200 And $b >= 199 And $c <= 250 And $d <= 250 Then
         Return False
      EndIf
      Return True
      EndFunc

      Func down4()
      $obj = ControlGetPos($gui,"",$input)
      $a = $cp[1] + $cp[3]
      $b = $cp[0] + $cp[2]
      $c = $cp[0]
      $d = $cp[1]
      If $a >= 199 And $b >= 200 And $c <= 250 And $d <= 250 Then
         Return False
      EndIf
      Return True
   EndFunc
Edited by gottygolly
Link to comment
Share on other sites

I completely remade it.

It's not using functions returning True or False, it checks for bounds directly in the while loop and if a move is possible then the flag $fMove is True which is similar to your functions return value.

Link to comment
Share on other sites

FireFox I tried the script you wrote over and over again and even tried to modify some things but I can't get it to stop the red box when it touches the black box.

I'm not sure if there is an error some where in the code and I don't know enough about this to figure it out or you just coded it a different way than what I had in mind. If you try the script I posted it does what I wanted however, yours just makes the red box pass right through the black box.

Sorry if I offended you but the script just is working  :ermm: .

I did learn some though after looking through it.

Edited by gottygolly
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...