ImageSearch starts at the top row of the designated region, and moves downward. If there is more than one match, the one closest to the top will be found. Therefore, start searching at the top left corner of the screen; whenever an image is found, there are two more regions to search: to the right of the found image (x+width, y, A_ScreenWidth, y+height), and below the found image (0, y+height, A_ScreenWidth, A_ScreenHeight). Those coordinates are known, because you know the image's dimensions (width x height). You keep doing that-- search to the right, and search below-- until you reach the bottom right corner.
This method will find every instance of the image.
The script is already written and posted somewhere; search the forum if you like, or it might even be faster just to write it again. Below is one example of what I described.
Code: Select all
image = e:\data\temp2\t2.png
Global places := []
CoordMode, Pixel
CoordMode, Mouse
imgFindAll(image, 0, 0, A_ScreenWidth, A_ScreenHeight)
If places.Count() {
size := imgSize(image)
Loop, % places.Count() {
SoundBeep, 1500, 20
MouseMove, xpos(places[A_Index]) + size.w / 2, ypos(places[A_Index]) + size.h / 2
Sleep, 400
}
}
ExitApp
imgSize(img) { ; Returns an array indicating the image's width (w) and height (h),
; obtained from the file's properties
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81665
SplitPath, img, fn, dir
objShell := ComObjCreate("Shell.Application")
objFolder := objShell.NameSpace(dir)
objFolderItem := objFolder.ParseName(fn)
scale := StrSplit(RegExReplace(objFolder.GetDetailsOf(objFolderItem, 31), ".(.+).", "$1"), " x ")
Return {w: scale.1, h: scale.2}
}
imgFindAll(image, x1, y1, x2, y2) {
ImageSearch, x, y, x1, y1, x2, y2, %image%
If ErrorLevel
Return
places.Push(pos(x,y))
size := imgSize(image)
imgFindAll(image, x + size.w, y, x2, y + size.h)
imgFindAll(image, 0, y + size.h, x2, y2)
}
xpos(pos) { ; This function provides the x-value of the combined position value; it is a remainder
Return Mod(pos, 65536)
}
ypos(pos) { ; This function provides the y-value of the combined position value
Return Floor(pos/65536)
}
pos(x,y) { ; This routine provides a single number that is a function of x and y coordinates
Return 65536*y+x
}