Realistphill

Check if controls overlap

11 posts in this topic

Hey guys,

Is there an easy way to check if controls on a GUI overlap?

something like: IF $control overlaps $othercontrol then dosomething.

^ i realise that syntax is wrong, but thats sort of what im trying to achieve.

Any help would be appreciated!

thanks!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

you could use controlgetpos to get the position of the controls in the window by comparing the two positions you could test wether they overlap

Edited by Surya

No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

Share this post


Link to post
Share on other sites

use ControlGetPos to retrieve and compare position and size of controls. i'll leave you to do the math yourself, not very difficult.

if you wish to prevent controls overlap at runtime, due to user resizing your GUI, use GUICtrlSetResizing to dock your controls in a way they will not overlap.

Share this post


Link to post
Share on other sites

Thats what ive been doing so far, before i got fully into it though i wanted to make sure there was no easier way, thanks for info

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

made a function for you (CODE UNTESTED): check wether this works

; #FUNCTION# ====================================================================================================================
; Name ..........: _checkifOverlap
; Description ...: 
; Syntax ........: _checkifOverlap($hndl, $ctrl1, $ctrl2)
; Parameters ....: $hndl                - a handle to the window
;                  $ctrl1               - the handle to the first cintrol
;                  $ctrl2               - the handle to the second control
; Return values .: true if overlaps false if doesnt
; Author ........: Surya Saradhi.B
; Modified ......: 06/09/15
; ===============================================================================================================================
Func _checkifOverlap($hndl,$ctrl1,$ctrl2)
$pos1 = ControlGetPos($hndl,"",$ctrl1)
$pos2 = ControlGetPos($hndl,"",$ctrl2)
$tws1 = $pos1[0] + $pos1[2]
$tms2 = $pos1[1] + $pos1[3]
If $pos1[0] <= $pos2[0] And $pos2[0] <= $tws1 And $pos1[1] <= $pos2[1] And $pos2[1] <= $tms2 Then
    Return True
EndIf
Return False
EndFunc

if  you want any more help feel free to ask

Edited by Surya

No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

Share this post


Link to post
Share on other sites

made a function for you (CODE UNTESTED): check wether this works

; #FUNCTION# ====================================================================================================================
; Name ..........: _checkifOverlap
; Description ...: 
; Syntax ........: _checkifOverlap($hndl, $ctrl1, $ctrl2)
; Parameters ....: $hndl                - a handle to the window
;                  $ctrl1               - the handle to the first cintrol
;                  $ctrl2               - the handle to the second control
; Return values .: true if overlaps false if doesnt
; Author ........: Surya Saradhi.B
; Modified ......: 06/09/15
; ===============================================================================================================================
Func _checkifOverlap($hndl,$ctrl1,$ctrl2)
$pos1 = ControlGetPos($hndl,"",$ctrl1)
$pos2 = ControlGetPos($hndl,"",$ctrl2)
$tws1 = $pos1[0] + $pos1[2]
$tms2 = $pos1[1] + $pos1[3]
If $pos1[0] <= $pos2[0] And $pos2[0] <= $tws1 And $pos1[1] <= $pos2[1] And $pos2[1] <= $tms2 Then
    Return True
EndIf
Return False
EndFunc

if  you want any more help feel free to ask

Wow, thank you! ill test in a bit and ill report my results.

Share this post


Link to post
Share on other sites

It doesnt work properly :(

I think one of the math forumlas you have there are wrong

Share this post


Link to post
Share on other sites

i tested the code and I think it works actual;ly:

try changing the position of the label over the button when the label is over the button it will return true

#include <GUIConstantsEx.au3>

Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example")
    Local $idOK = GUICtrlCreateButton("OK", 310, 370, 85, 25)
    Local $lbl = GUICtrlCreateLabel ("hello",310,390)
    MsgBox("","",_checkifOverlap($hGUI,$idOK,$lbl))
    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idOK
                ExitLoop

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example


Func _checkifOverlap($hndl,$ctrl1,$ctrl2)
$pos1 = ControlGetPos($hndl,"",$ctrl1)
$pos2 = ControlGetPos($hndl,"",$ctrl2)
$tws1 = $pos1[0] + $pos1[2]
$tms2 = $pos1[1] + $pos1[3]
If $pos1[0] <= $pos2[0] And $pos2[0] <= $tws1 And $pos1[1] <= $pos2[1] And $pos2[1] <= $tms2 Then
    Return True
EndIf
Return False
EndFunc

post me your code i will check that also


No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

Share this post


Link to post
Share on other sites

Ok check my code out:

 

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



guicreate("test", 700, 700)

guisetstate()



$player = guictrlcreateGraphic(150, 150, 50, 50)
GUICtrlSetbkColor(-1, 0xfffff)

$object = GUICtrlCreateGraphic(100, 200, 50, 50)
GUICtrlSetbkColor(-1, 0xfffdddd)


$action = 0
while $action <> $GUI_EVENT_CLOSE
    $action = GUIGetMsg()

    Select
        case _IsPressed(27) and _IsPressed(28) ;right-down
        $pos = controlgetpos("", "", $player)
        controlmove("", "", $player, $pos[0] + 2, $pos[1] + 2)



        case _IsPressed(27) ;right
        $pos = controlgetpos("", "", $player)
        controlmove("", "", $player, $pos[0] + 2, $pos[1])


        case _IsPressed(28) ;down
        $pos = controlgetpos("", "", $player)
        controlmove("", "", $player, $pos[0], $pos[1] + 2)

        case _IsPressed(25) ;left
        $pos = controlgetpos("", "", $player)
        controlmove("", "", $player, $pos[0] - 2, $pos[1])

            case _IsPressed(26) ;up
        $pos = controlgetpos("", "", $player)
        controlmove("", "", $player, $pos[0], $pos[1] - 2)


    EndSelect



 if _checkifOverlap("test", $object, $player) = true Then
     traytip("x", "x", 200)
     endif


WEnd


Func _checkifOverlap($hndl,$ctrl1,$ctrl2)
$pos1 = ControlGetPos($hndl,"",$ctrl1)
$pos2 = ControlGetPos($hndl,"",$ctrl2)
$tws1 = $pos1[0] + $pos1[2]
$tms2 = $pos1[1] + $pos1[3]
If $pos1[0] <= $pos2[0] And $pos2[0] <= $tws1 And $pos1[1] <= $pos2[1] And $pos2[1] <= $tms2 Then
    Return True
EndIf
Return False
EndFunc

Just play it and youll see what i mean

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Ok i figured it out, heres the updated UDF: (me and my dad rewrote most of it)

Func _checkifOverlap($ctrl1, $ctrl2)
    ;expects 2 control variables to check if overlapping.
    ; return TRUE if overlapped, otherwise false
    Local $pos1 = ControlGetPos("", "", $ctrl1)
    Local $pos2 = ControlGetPos("", "", $ctrl2)
    local $iX = 0, $iY = 1, $iWidth = 2, $iHeight = 3
    Local $iCtrl1_X1 = $pos1[$iX]
    Local $iCtrl1_Y1 = $pos1[$iY]
    Local $iCtrl1_X2 = $pos1[$iX] + $pos1[$iWidth]
    Local $iCtrl1_Y2 = $pos1[$iY] + $pos1[$iHeight]

    Local $iCtrl2_X1 = $pos2[$iX]
    Local $iCtrl2_Y1 = $pos2[$iY]
    Local $iCtrl2_X2 = $pos2[$iX] + $pos2[$iWidth]
    Local $iCtrl2_Y2 = $pos2[$iY] + $pos2[$iHeight]

    ;top left corner
    If ($iCtrl2_X1 >= $iCtrl1_X1 And $iCtrl2_X1 <= $iCtrl1_X2) And ($iCtrl2_Y1 >= $iCtrl1_Y1 And $iCtrl2_Y1 <= $iCtrl1_Y2) Then
        Return True
    EndIf
    ;Bottom left corner
    If ($iCtrl2_X1 >= $iCtrl1_X1 And $iCtrl2_X1 <= $iCtrl1_X2) And ($iCtrl2_Y2 >= $iCtrl1_Y1 And $iCtrl2_Y2 <= $iCtrl1_Y2) Then
        Return True
    EndIf

    ;Bottom right corner
    If ($iCtrl2_X2 >= $iCtrl1_X1 And $iCtrl2_X2 <= $iCtrl1_X2) And ($iCtrl2_Y2 >= $iCtrl1_Y1 And $iCtrl2_Y2 <= $iCtrl1_Y2) Then
        Return True
    EndIf
    ;top right corner
    If ($iCtrl2_X2 >= $iCtrl1_X1 And $iCtrl2_X2 <= $iCtrl1_X2) And ($iCtrl2_Y1 >= $iCtrl1_Y1 And $iCtrl2_Y1 <= $iCtrl1_Y2) Then
        Return True
    EndIf

    Return False
EndFunc   ;==>_checkifOverlap

It works!!!

Edited by Realistphill

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