Monday, January 15, 2018

Sample AHK 2. find pixel color in background

bgfindcolor(color,WinTitle,x,y,L:=5,Cx:=0,Cy:=0)
{
;x:=x-4
;y:=y-32
loop
{
If !pToken := Gdip_Startup()
{
   MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
   ExitApp
}

; title of window you want to search into
;WinTitle:="PSPad"  ;just example

hwnd:=WinExist(WinTitle) ; get the handle to the window
;msgbox %hwnd%  ;for debugging to see if there is a handle
;needle=d:\script\testRGB.png
;bmpHaystack := Gdip_CreateBitmapFromFile(needle)
bmpHaystack := Gdip_BitmapFromHWND(hwnd)  ;you need the handle of the window to search in
;Gdip_SaveBitmapToFile(bmpHaystack,"HWND.png") ; you can test to see what has been captured for debugging
color1:=Gdip_GetPixel(bmpHaystack, x, y)
color2:= substr(ARGBtoRGB(color1),3,99)
Gdip_DisposeImage(bmpHaystack)
Gdip_Shutdown(pToken)
IfInString, color,%color2%
{
if Cx <> 0
{
x:=Cx
y:=Cy
}
sleep, 500
ControlClick, x%x% y%y% , ahk_id %hwnd%,,left,, Pos
return 1
}
sleep,3000
;mousemove,x, y
;msgbox %color%,  %color2%,%x%, %y%, %hwnd%
L:=L-1
if L=0
return 0
}
;msgbox "found"
}