#AutoIt3Wrapper_UseX64=y
#include-once
#include <WinAPIFiles.au3>
#include <ScreenCapture.au3>
#Region When running compiled script, Install needed DLLs if they don't exist yet
If Not FileExists("ImageSearchDLLx32.dll") Then FileInstall("ImageSearchDLLx32.dll", "ImageSearchDLLx32.dll", 1)
If Not FileExists("ImageSearchDLLx64.dll") Then FileInstall("ImageSearchDLLx64.dll", "ImageSearchDLLx64.dll", 1)
If Not FileExists("msvcr110d.dll") Then FileInstall("msvcr110d.dll", "msvcr110d.dll", 1)
If Not FileExists("msvcr110.dll") Then FileInstall("msvcr110.dll", "msvcr110.dll", 1)
#EndRegion
Local $h_ImageSearchDLL = -1
#Region TESTING/Example
Local $bTesting = False
If $bTesting Then
cr(">" & "_ImageSearchStartup()=" & _ImageSearchStartup(), 2)
OnAutoItExitRegister(_ImageSearchShutdown)
cr("!Testing...")
#Region Notepad Snapshot Creation
$hWin = WinGetHandle("[CLASS:Notepad]")
If Not IsHWnd($hWin) Then
If Run("notepad.exe") = 0 Then cr("Couldn't run notepad.exe")
Local $hWin = WinWait("[CLASS:Notepad]", "", 10)
If $hWin = 0 Then cr("Notepad WinWait Timeout!")
EndIf
WinSetState($hWin, "", @SW_RESTORE)
WinSetState($hWin, "", @SW_SHOW)
WinActivate($hWin)
Local $testimage = "24bit.bmp"
_ScreenCapture_CaptureWnd($testimage, $hWin, 0, 0, -1, -1, False)
cr("made Notepad Window screenshot")
#EndRegion Notepad Snapshot Creation
#Region Test if application appeared
Local $y = 0, $x = 0, $result
$result = _ImageSearch($testimage, 1, $x, $y, 0, 0)
If $result = 1 Then
cr("! $result=" & $result, 1)
MouseMove($x, $y, 0)
cr("+" & "recognised notepad! moved mouse to center of notepad!")
Else
cr("! $result=" & $result, 1)
Exit
EndIf
#EndRegion Test if application appeared
#Region Test if application vanished
WinSetState($hWin, "", @SW_MINIMIZE)
Local $y = 0, $x = 0, $result
$result = _ImageSearch($testimage, 1, $x, $y, 0, 0)
If $result = 1 Then
cr("! $result=" & $result, 1)
Exit
Else
cr("! $result=" & $result, 1)
cr("+" & "notepad dissapeared!")
EndIf
#EndRegion Test if application vanished
cr()
cr("!Test finished")
EndIf
#EndRegion TESTING/Example
#Region ImageSearch Startup/Shutdown
Func _ImageSearchStartup()
_WinAPI_Wow64EnableWow64FsRedirection(True)
$sOSArch = @OSArch
$sAutoItX64 = @AutoItX64
If $sOSArch = "X86" Or $sAutoItX64 = 0 Then
cr("+>" & "@OSArch=" & $sOSArch & @TAB & "@AutoItX64=" & $sAutoItX64 & @TAB & "therefore using x32 ImageSearch DLL")
$h_ImageSearchDLL = DllOpen("ImageSearchDLLx32.dll")
If $h_ImageSearchDLL = -1 Then Return "DllOpen failure"
ElseIf $sOSArch = "X64" And $sAutoItX64 = 1 Then
cr("+>" & "@OSArch=" & $sOSArch & @TAB & "@AutoItX64=" & $sAutoItX64 & @TAB & "therefore using x64 ImageSearch DLL")
$h_ImageSearchDLL = DllOpen("ImageSearchDLLx64.dll")
If $h_ImageSearchDLL = -1 Then Return "DllOpen failure"
Else
Return "Inconsistent or incompatible Script/Windows/CPU Architecture"
EndIf
Return True
EndFunc
Func _ImageSearchShutdown()
DllClose($h_ImageSearchDLL)
_WinAPI_Wow64EnableWow64FsRedirection(False)
cr(">" & "_ImageSearchShutdown() completed")
Return True
EndFunc
#EndRegion ImageSearch Startup/Shutdown
#Region ImageSearch UDF
Func _ImageSearch($findImage, $resultPosition, ByRef $x, ByRef $y, $tolerance, $transparency = 0)
Return _ImageSearchArea($findImage, $resultPosition, 0, 0, @DesktopWidth, @DesktopHeight, $x, $y, $tolerance, $transparency)
EndFunc
Func _ImageSearchArea($findImage, $resultPosition, $x1, $y1, $right, $bottom, ByRef $x, ByRef $y, $tolerance = 0, $transparency = 0)
If Not FileExists($findImage) Then Return "Image File not found"
If $tolerance < 0 Or $tolerance > 255 Then $tolerance = 0
If $h_ImageSearchDLL = -1 Then _ImageSearchStartup()
If $transparency <> 0 Then $findImage = "*" & $transparency & " " & $findImage
If $tolerance > 0 Then $findImage = "*" & $tolerance & " " & $findImage
$result = DllCall($h_ImageSearchDLL, "str", "ImageSearch", "int", $x1, "int", $y1, "int", $right, "int", $bottom, "str", $findImage)
If @error Then Return "DllCall Error=" & @error
If $result = "0" Or Not IsArray($result) Or $result[0] = "0" Then Return False
$array = StringSplit($result[0], "|")
If (UBound($array) >= 4) Then
$x = Int(Number($array[2]))
$y = Int(Number($array[3]))
If $resultPosition = 1 Then
$x = $x + Int(Number($array[4]) / 2)
$y = $y + Int(Number($array[5]) / 2)
EndIf
Return True
EndIf
EndFunc
Func _WaitForImageSearch($findImage, $waitSecs, $resultPosition, ByRef $x, ByRef $y, $tolerance, $transparency = 0)
$waitSecs = $waitSecs * 1000
$startTime = TimerInit()
While TimerDiff($startTime) < $waitSecs
Sleep(100)
If _ImageSearch($findImage, $resultPosition, $x, $y, $tolerance, $transparency) Then
Return True
EndIf
WEnd
Return False
EndFunc
Func _WaitForImagesSearch($findImage, $waitSecs, $resultPosition, ByRef $x, ByRef $y, $tolerance, $transparency = 0)
$waitSecs = $waitSecs * 1000
$startTime = TimerInit()
While TimerDiff($startTime) < $waitSecs
For $i = 1 To $findImage[0]
Sleep(100)
If _ImageSearch($findImage[$i], $resultPosition, $x, $y, $tolerance, $transparency) Then
Return $i
EndIf
Next
WEnd
Return False
EndFunc
#EndRegion ImageSearch UDF
#Region My Custom ConsoleWrite/debug Function
Func cr($text = "", $addCR = 1, $printTime = False)
Static $sToolTip
If Not @Compiled Then
If $printTime Then ConsoleWrite(@HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC & " ")
ConsoleWrite($text)
If $addCR >= 1 Then ConsoleWrite(@CR)
If $addCR = 2 Then ConsoleWrite(@CR)
Else
If $printTime Then $sToolTip &= @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC & " "
$sToolTip &= $text
If $addCR >= 1 Then $sToolTip &= @CR
If $addCR = 2 Then $sToolTip &= @CR
ToolTip($sToolTip)
EndIf
Return $text
EndFunc
#EndRegion My Custom ConsoleWrite/debug Function