nf67 Posted May 17, 2009 Posted May 17, 2009 (edited) Hi there,I was wondering how I can check if there's a collision between 2 windows.Assuming that there isn't a command like "if X overlaps Y" I took the more mathematical approach.I am not sure if this is a correct though, and I don't know how to translate it to AutoIt.Here's a picture that hopefully explains what I mean:Thanks Edited May 17, 2009 by nf67
ken82m Posted May 17, 2009 Posted May 17, 2009 (edited) expandcollapse popup#include <array.au3> $Win1 = Run("Notepad.exe") Sleep(3000) $Win1 = _WinGetByPID($Win1);get win handle to app window $Win2 = Run("mspaint.exe") Sleep(3000) $Win2 = _WinGetByPID($Win2);get win handle to app window While 1 Sleep(10) CheckCollission($Win1, $Win2) WEnd Func CheckCollission($Win1, $Win2) #cs Win 1 $Win1Pos[0];X start $Win1Pos[1];Y start $Win2Pos[2];X end $Win2Pos[3];Y end Win 2 $Win2Pos[0];X start $Win2Pos[1];Y start $Win2Pos[2];X end $Win2Pos[3];Y end #ce $Win1Pos = WinGetPos($Win1) $Win2Pos = WinGetPos($Win2) $Win1Pos[2] = $Win1Pos[0] + $Win1Pos[2] $Win1Pos[3] = $Win1Pos[1] + $Win1Pos[3] $Win2Pos[2] = $Win2Pos[0] + $Win2Pos[2] $Win2Pos[3] = $Win2Pos[1] + $Win2Pos[3] _ArrayDisplay($Win1Pos) _ArrayDisplay($Win2Pos) Sleep(5000) ;math to figure out if they are in each others area EndFunc Func _WinGetByPID($iPID) Local $aWList = WinList() For $iCC = 1 To $aWList[0][0] If WinGetProcess($aWList[$iCC][1]) = $iPID And _ BitAND(WinGetState($aWList[$iCC][1]), 2) Then Return $aWList[$iCC][0] EndIf Next Return SetError(1, 0, 0) EndFunc Edited May 17, 2009 by ken82m "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."
nf67 Posted May 17, 2009 Author Posted May 17, 2009 Sorry, I've messed around with it a little bit but I really don't understand what it does and how it does that... Could you maybe create a little example with a MsgBox that pops up when there is a collision? With a ball and a wall for example? #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> ;The ball. $Ball = GUICreate("Ball", 150, 150, 200, 200) GUISetBkColor(0x800000) $CreateWall = GUICtrlCreateButton("Wall", 0, 32, 49, 17, $WS_GROUP) $BallLabel = GUICtrlCreateLabel("Ball", 8, 8, 21, 17) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $CreateWall ;The wall. $Wall = GUICreate("Wall", 150, 500, 300, 300) GUISetBkColor(0x808080) $WallLabel = GUICtrlCreateLabel("Wall", 8, 8, 25, 17) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd EndSwitch WEnd I'm pretty confused
martin Posted May 17, 2009 Posted May 17, 2009 Sorry, I've messed around with it a little bit but I really don't understand what it does and how it does that... Could you maybe create a little example with a MsgBox that pops up when there is a collision? With a ball and a wall for example? #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> ;The ball. $Ball = GUICreate("Ball", 150, 150, 200, 200) GUISetBkColor(0x800000) $CreateWall = GUICtrlCreateButton("Wall", 0, 32, 49, 17, $WS_GROUP) $BallLabel = GUICtrlCreateLabel("Ball", 8, 8, 21, 17) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $CreateWall ;The wall. $Wall = GUICreate("Wall", 150, 500, 300, 300) GUISetBkColor(0x808080) $WallLabel = GUICtrlCreateLabel("Wall", 8, 8, 25, 17) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd EndSwitch WEnd I'm pretty confused Try this which is an extension of ken82m's post. expandcollapse popup#include <array.au3> $Win1 = Run("Notepad.exe") Sleep(3000) $Win1 = _WinGetByPID($Win1);get win handle to app window $Win2 = Run("mspaint.exe") Sleep(2000) $Win2 = _WinGetByPID($Win2);get win handle to app window While 1 Sleep(10) If CheckCollission($Win1, $Win2) Then ToolTip("collision") Else ToolTip("") EndIf WEnd Func CheckCollission($Win1, $Win2) $Win1Pos = WinGetPos($Win1) $Win2Pos = WinGetPos($Win2) If _CheckOverlap($Win1Pos, $Win2Pos) Then Return True If _CheckOverlap($Win2Pos, $Win1Pos) Then Return True EndFunc ;==>CheckCollission Func _WinGetByPID($iPID) Local $aWList = WinList() For $iCC = 1 To $aWList[0][0] If WinGetProcess($aWList[$iCC][1]) = $iPID And _ BitAND(WinGetState($aWList[$iCC][1]), 2) Then Return $aWList[$iCC][0] EndIf Next Return SetError(1, 0, 0) EndFunc ;==>_WinGetByPID Func _CheckOverlap($ar1, $a2) If _WinAPI_PtInRectEx($ar1[0], $ar1[1], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0] + $ar1[2], $ar1[1], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0] + $ar1[2], $ar1[1] + $ar1[3], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0], $ar1[1] + $ar1[3], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True EndFunc ;==>_CheckOverlap ; Note this_WinAPI_PtInRectEx() Not the same as 1st post. ; Here Width & height used, NOT Bottom right corner position. ; Also, declared $tagREC. $tagRECT required WinAPI.au3 to be included. ; Author - Malkey Func _WinAPI_PtInRectEx($iX, $iY, $iLeft, $iTop, $iWidth, $iHeight) Local $aResult, $tagREC = "int Left;int Top;int Right;int Bottom" Local $tRect = DllStructCreate($tagREC) DllStructSetData($tRect, "Left", $iLeft) DllStructSetData($tRect, "Top", $iTop) DllStructSetData($tRect, "Right", $iLeft + $iWidth) DllStructSetData($tRect, "Bottom", $iTop + $iHeight) $aResult = DllCall("User32.dll", "int", "PtInRect", "ptr", DllStructGetPtr($tRect), "int", $iX, "int", $iY) Return $aResult[0] <> 0 EndFunc Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
ken82m Posted May 17, 2009 Posted May 17, 2009 Thank you, I really didn't feel like figuring out the formula, I never said math was my strongpoint lol "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."
ken82m Posted May 17, 2009 Posted May 17, 2009 (edited) Here's an example using your code. Adlib is the best way to monitor since your using a loop within a loop. expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> ;=================================================================== ;GUI's should be created before monitoring begins ;=================================================================== ;The Wall $Wall = GUICreate("Wall", 150, 500, 300, 300) GUISetBkColor(0x808080) $WallLabel = GUICtrlCreateLabel("Wall", 8, 8, 25, 17) ;The ball. $Ball = GUICreate("Ball", 150, 150, 200, 200) GUISetBkColor(0x800000) $CreateWall = GUICtrlCreateButton("Wall", 0, 32, 49, 17, $WS_GROUP) $BallLabel = GUICtrlCreateLabel("Ball", 8, 8, 21, 17) GUISetState(@SW_SHOW) ;=================================================================== AdlibEnable("MonitorWindows", 10); Monitor for collissions While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $CreateWall ;The wall. GUICtrlSetData($WallLabel, "Wall"); update label text if you needed to GUISetState(@SW_SHOW, $Wall) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd EndSwitch WEnd ;================================================================================== ; FUNCTIONS FOR CHECK COLLISSION ;================================================================================== Func MonitorWindows() If BitAND(WinGetState($Ball), 2) AND BitAND(WinGetState($Wall), 2) AND CheckCollission($Ball, $Wall) Then ToolTip("collision") Else ToolTip("") EndIf EndFunc Func CheckCollission($Win1, $Win2) $Win1Pos = WinGetPos($Win1) If @error Then MsgBox(0,"","Error Reading Win1") Return 0 EndIf $Win2Pos = WinGetPos($Win2) If @error Then MsgBox(0,"","Error Reading Win2") Return 0 EndIf If _CheckOverlap($Win1Pos, $Win2Pos) Then Return True If _CheckOverlap($Win2Pos, $Win1Pos) Then Return True EndFunc;==>CheckCollission Func _WinGetByPID($iPID) Local $aWList = WinList() For $iCC = 1 To $aWList[0][0] If WinGetProcess($aWList[$iCC][1]) = $iPID And _ BitAND(WinGetState($aWList[$iCC][1]), 2) Then Return $aWList[$iCC][0] EndIf Next Return SetError(1, 0, 0) EndFunc;==>_WinGetByPID Func _CheckOverlap($ar1, $a2) If _WinAPI_PtInRectEx($ar1[0], $ar1[1], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0] + $ar1[2], $ar1[1], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0] + $ar1[2], $ar1[1] + $ar1[3], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0], $ar1[1] + $ar1[3], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True EndFunc;==>_CheckOverlap ; Note this_WinAPI_PtInRectEx() Not the same as 1st post. ; Here Width & height used, NOT Bottom right corner position. ; Also, declared $tagREC. $tagRECT required WinAPI.au3 to be included. ; Author - Malkey Func _WinAPI_PtInRectEx($iX, $iY, $iLeft, $iTop, $iWidth, $iHeight) Local $aResult, $tagREC = "int Left;int Top;int Right;int Bottom" Local $tRect = DllStructCreate($tagREC) DllStructSetData($tRect, "Left", $iLeft) DllStructSetData($tRect, "Top", $iTop) DllStructSetData($tRect, "Right", $iLeft + $iWidth) DllStructSetData($tRect, "Bottom", $iTop + $iHeight) $aResult = DllCall("User32.dll", "int", "PtInRect", "ptr", DllStructGetPtr($tRect), "int", $iX, "int", $iY) Return $aResult[0] <> 0 EndFunc Edited May 17, 2009 by ken82m "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."
Yogui Posted May 17, 2009 Posted May 17, 2009 just for fun ... expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> ;=================================================================== ;GUI's should be created before monitoring begins ;=================================================================== ;The Wall $Wall = GUICreate("Wall", 150, 500, 300, 300) GUISetBkColor(0x808080) $WallLabel = GUICtrlCreateLabel("Wall", 8, 8, 25, 17) ;The ball. $Ball = GUICreate("Ball", 150, 150, 200, 200) GUISetBkColor(0x800000) $CreateWall = GUICtrlCreateButton("Wall", 0, 32, 49, 17, $WS_GROUP) $BallLabel = GUICtrlCreateLabel("Ball", 8, 8, 21, 17) GUISetState(@SW_SHOW) ;=================================================================== AdlibEnable("MonitorWindows", 10); Monitor for collissions While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $CreateWall ;The wall. GUICtrlSetData($WallLabel, "Wall"); update label text if you needed to GUISetState(@SW_SHOW, $Wall) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd EndSwitch WEnd ;================================================================================== ; FUNCTIONS FOR CHECK COLLISSION ;================================================================================== Func MonitorWindows() If BitAND(WinGetState($Ball), 2) And BitAND(WinGetState($Wall), 2) And CheckCollission($Ball, $Wall) Then ToolTip("collision") $size = WinGetPos("Ball") For $i = 1 To 10 WinMove("Ball", "", $size[0] + 5, $size[1] + 5, $size[2], $size[3]) ;~ sleep(500) WinMove("Ball", "", $size[0] - 5, $size[1] - 5, $size[2], $size[3]) Next Else ToolTip("") EndIf EndFunc ;==>MonitorWindows Func CheckCollission($Win1, $Win2) $Win1Pos = WinGetPos($Win1) If @error Then MsgBox(0, "", "Error Reading Win1") Return 0 EndIf $Win2Pos = WinGetPos($Win2) If @error Then MsgBox(0, "", "Error Reading Win2") Return 0 EndIf If _CheckOverlap($Win1Pos, $Win2Pos) Then Return True If _CheckOverlap($Win2Pos, $Win1Pos) Then Return True EndFunc ;==>CheckCollission Func _WinGetByPID($iPID) Local $aWList = WinList() For $iCC = 1 To $aWList[0][0] If WinGetProcess($aWList[$iCC][1]) = $iPID And _ BitAND(WinGetState($aWList[$iCC][1]), 2) Then Return $aWList[$iCC][0] EndIf Next Return SetError(1, 0, 0) EndFunc ;==>_WinGetByPID Func _CheckOverlap($ar1, $a2) If _WinAPI_PtInRectEx($ar1[0], $ar1[1], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0] + $ar1[2], $ar1[1], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0] + $ar1[2], $ar1[1] + $ar1[3], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0], $ar1[1] + $ar1[3], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True EndFunc ;==>_CheckOverlap ; Note this_WinAPI_PtInRectEx() Not the same as 1st post. ; Here Width & height used, NOT Bottom right corner position. ; Also, declared $tagREC. $tagRECT required WinAPI.au3 to be included. ; Author - Malkey Func _WinAPI_PtInRectEx($iX, $iY, $iLeft, $iTop, $iWidth, $iHeight) Local $aResult, $tagREC = "int Left;int Top;int Right;int Bottom" Local $tRect = DllStructCreate($tagREC) DllStructSetData($tRect, "Left", $iLeft) DllStructSetData($tRect, "Top", $iTop) DllStructSetData($tRect, "Right", $iLeft + $iWidth) DllStructSetData($tRect, "Bottom", $iTop + $iHeight) $aResult = DllCall("User32.dll", "int", "PtInRect", "ptr", DllStructGetPtr($tRect), "int", $iX, "int", $iY) Return $aResult[0] <> 0 EndFunc ;==>_WinAPI_PtInRectEx
Valuater Posted May 17, 2009 Posted May 17, 2009 Might as well make it a "Ball" lol expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> ;=================================================================== ;GUI's should be created before monitoring begins ;=================================================================== ;The Wall $Wall = GUICreate("Wall", 150, 500, 300, 300) GUISetBkColor(0x808080) $WallLabel = GUICtrlCreateLabel("Wall", 8, 8, 25, 17) ;The ball. $Ball = GUICreate("Ball", 150, 120, 200, 200) GUISetBkColor(0x800000) $CreateWall = GUICtrlCreateButton("Wall", 85, 80, 49, 17, $WS_GROUP) $BallLabel = GUICtrlCreateLabel("Ball", 95, 60, 21, 17) _GuiRoundCorners($Ball, 75, 75, 75, 75) GUISetState(@SW_SHOW) ;=================================================================== AdlibEnable("MonitorWindows", 10); Monitor for collissions While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $CreateWall ;The wall. GUICtrlSetData($WallLabel, "Wall"); update label text if you needed to GUISetState(@SW_SHOW, $Wall) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd EndSwitch WEnd ;================================================================================== ; FUNCTIONS FOR CHECK COLLISSION ;================================================================================== Func MonitorWindows() If BitAND(WinGetState($Ball), 2) And BitAND(WinGetState($Wall), 2) And CheckCollission($Ball, $Wall) Then ToolTip("collision") $size = WinGetPos("Ball") For $i = 1 To 10 WinMove("Ball", "", $size[0] + 5, $size[1] + 5, $size[2], $size[3]) ;~ sleep(500) WinMove("Ball", "", $size[0] - 5, $size[1] - 5, $size[2], $size[3]) Next Else ToolTip("") EndIf EndFunc ;==>MonitorWindows Func CheckCollission($Win1, $Win2) $Win1Pos = WinGetPos($Win1) If @error Then MsgBox(0, "", "Error Reading Win1") Return 0 EndIf $Win2Pos = WinGetPos($Win2) If @error Then MsgBox(0, "", "Error Reading Win2") Return 0 EndIf If _CheckOverlap($Win1Pos, $Win2Pos) Then Return True If _CheckOverlap($Win2Pos, $Win1Pos) Then Return True EndFunc ;==>CheckCollission Func _WinGetByPID($iPID) Local $aWList = WinList() For $iCC = 1 To $aWList[0][0] If WinGetProcess($aWList[$iCC][1]) = $iPID And _ BitAND(WinGetState($aWList[$iCC][1]), 2) Then Return $aWList[$iCC][0] EndIf Next Return SetError(1, 0, 0) EndFunc ;==>_WinGetByPID Func _CheckOverlap($ar1, $a2) If _WinAPI_PtInRectEx($ar1[0], $ar1[1], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0] + $ar1[2], $ar1[1], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0] + $ar1[2], $ar1[1] + $ar1[3], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True If _WinAPI_PtInRectEx($ar1[0], $ar1[1] + $ar1[3], $a2[0], $a2[1], $a2[2], $a2[3]) Then Return True EndFunc ;==>_CheckOverlap ; Note this_WinAPI_PtInRectEx() Not the same as 1st post. ; Here Width & height used, NOT Bottom right corner position. ; Also, declared $tagREC. $tagRECT required WinAPI.au3 to be included. ; Author - Malkey Func _WinAPI_PtInRectEx($iX, $iY, $iLeft, $iTop, $iWidth, $iHeight) Local $aResult, $tagREC = "int Left;int Top;int Right;int Bottom" Local $tRect = DllStructCreate($tagREC) DllStructSetData($tRect, "Left", $iLeft) DllStructSetData($tRect, "Top", $iTop) DllStructSetData($tRect, "Right", $iLeft + $iWidth) DllStructSetData($tRect, "Bottom", $iTop + $iHeight) $aResult = DllCall("User32.dll", "int", "PtInRect", "ptr", DllStructGetPtr($tRect), "int", $iX, "int", $iY) Return $aResult[0] <> 0 EndFunc ;==>_WinAPI_PtInRectEx Func _GuiRoundCorners($h_win, $i_x1, $i_y1, $i_x3, $i_y3) ; Gary Frost Local $XS_pos, $XS_ret, $XS_ret2 $XS_pos = WinGetPos($h_win) $XS_ret = DllCall("gdi32.dll", "long", "CreateRoundRectRgn", "long", $i_x1, "long", $i_y1, "long", $XS_pos[2], "long", $XS_pos[3], "long", $i_x3, "long", $i_y3) If $XS_ret[0] Then $XS_ret2 = DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $h_win, "long", $XS_ret[0], "int", 1) EndIf EndFunc ;==>_GuiRoundCorners 8)
ken82m Posted May 17, 2009 Posted May 17, 2009 lol, yeah no extra charge for the wall "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now