Pixelsearch a specific area of a (possibly background) window using the gdip library
Posted: 24 Sep 2016, 17:18
I am currently trying to make a function for my script that would find and return the (X,Y) coordinates of a specified color within a delimited area of a window that is either visible on screen or in the background covered by other windows.
Using the gdip library and with the help of one of tic's examples, the following code will print and display an area of a window. The area of the window is delimited with four parameters; x,y,w,h, which are respectively the top-left x, top-left y, width of the area and height of the area :
I'm not quite sure how I could go on about making a function that would find a specific color within an area defined by a function similar to Fn_ScreenCapture. The source bitmap of the area wouldn't have to be printed out, just saved until the pixelsearch is done finding the color.
I would greatly appreciate any help or advice, I tried looking for similar problems on the forums and on google, but none of them match my problem. Also, I'm pretty new to Ahk, so any extensive explanation is most welcome.
Using the gdip library and with the help of one of tic's examples, the following code will print and display an area of a window. The area of the window is delimited with four parameters; x,y,w,h, which are respectively the top-left x, top-left y, width of the area and height of the area :
Code: Select all
#Include Gdip_All.ahk
global ScrBoxOn := 0
global win_title:="Untitled - Notepad"
Fn_ScreenCapture(0,0,1500,1000)
Fn_ScreenCapture(x,y,w,h)
{
If !pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
ScrBoxOn := !ScrBoxOn
Gui, ScrBox: +E0x80000 +LastFound +AlwaysOnTop ;+ToolWindow +OwnDialogs -Caption
Gui, ScrBox: Show, NA
hwnd1 := WinExist()
pBitmap := Gdip_BitmapFromHWND(WinExist(win_title))
If !pBitmap
{
MsgBox, 4096, File loading error!, Could not load the image specified
ExitApp
}
Width := Gdip_GetImageWidth(pBitmap), Height := Gdip_GetImageHeight(pBitmap)
hbm := CreateDIBSection(Width, Height)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
Gdip_SetInterpolationMode(G, 7)
Gdip_DrawImage(G, pBitmap,x,y,w,h,x,y,w,h)
UpdateLayeredWindow(hwnd1, hdc, (A_ScreenWidth -Width)/2, (A_ScreenHeight-Height)/2, Width, Height)
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_DisposeImage(pBitmap)
ToolTip, Press Esc to close.
Return
}
#If ScrBoxOn = 1
Esc::
Gdip_Shutdown(pToken)
Gui, ScrBox:Destroy
ToolTip
ExitApp
#If
I would greatly appreciate any help or advice, I tried looking for similar problems on the forums and on google, but none of them match my problem. Also, I'm pretty new to Ahk, so any extensive explanation is most welcome.