Search Image in an area in Inactive Window Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Bochoko
Posts: 9
Joined: 30 Jan 2020, 03:07

Search Image in an area in Inactive Window

30 Jan 2020, 03:31

Hi, i'm new to ahk and really need your help.
I've learned how to use controlclick, controlsend.
But i find it really hard to make a search image in an area in an inactive window with using bitmapfromhwnd , not bitmapfromscreen, since i want it to be off screen searching.
I've tried merging scripts from some posts, i can run the script, but still can not to find the image:
https://www.autohotkey.com/boards/viewtopic.php?t=17507
https://www.autohotkey.com/boards/viewtopic.php?t=38324
https://www.autohotkey.com/boards/viewtopic.php?p=23210#p23210




Here's my code:

Code: Select all

#NoEnv
ListLines, Off
SetBatchLines, -1
Process, Priority,, High
#Include GDIP.ahk
#Include Gdip_ImageSearch.ahk

OnExit, EXIT_LABEL

winTarget := "Untitled.png - Paint"
needleDirA:= "C:\Users\MyUserName\Desktop\ahk studies\test image\circle paint.png"


CustomImgSearch(ByRef out1, ByRef out2, x1, y1, x2, y2, needleDir, vari=0, trans="",direction=5) 
{
	If !pToken := Gdip_Startup()
	{
	   MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
	   ExitApp
	}
    bmpNeedle := Gdip_CreateBitmapFromFile(needleDir)
    bmpHaystack := Gdip_BitmapFromHWND(hwnd := WinExist(winTarget))  ;you need the handle of the window to search in
    found := Gdip_ImageSearch(bmpHaystack,bmpNeedle,tempxy,x1,y1,x2,y2,vari,trans,direction)
    if !( found < 1 ) ; if successful
    {
        out:=StrSplit(tempxy,"`,")
        out1:=out[1] + x1
        out2:=out[2] + y1
    }
    Gdip_DisposeImage(bmpHaystack)
    Gdip_DisposeImage(bmpNeedle)
    Gdip_Shutdown(pToken)
    return ( found < 1 ) ; will return 0 for SUCCESS, 1 for FAILURE
}


F4::
{
	if (!CustomImgSearch(OutputX,OutputY, 676, 244, 1126, 590,needleDirA)) ; got the image
	{	
		ControlClick, x%OutputX% y%OutputY%, %winTarget%
		Msgbox %winTarget%,%OutputX%,%OutputY%,%fileDir%
	}
	else
	{
		Msgbox Image not found : %needleDirA%, %winTarget%,%OutputX%,%OutputY%
		return
	}
	return
}


esc::
EXIT_LABEL: 
Gdip_Shutdown(pToken)
EXITAPP


User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Search Image in an area in Inactive Window

30 Jan 2020, 07:32

So you have an MS Paint window open with the file Untitled.png, and it contains the same image in it as your “circle paint.img”?

Can you post the contents of the MsgBox that appears? You can copy the contents of the MsgBox by pressing Ctrl+C while the MsgBox is the active window (without trying to select any of the text, which you can’t do anyway).
Bochoko
Posts: 9
Joined: 30 Jan 2020, 03:07

Re: Search Image in an area in Inactive Window

30 Jan 2020, 09:26

boiler wrote:
30 Jan 2020, 07:32
So you have an MS Paint window open with the file Untitled.png, and it contains the same image in it as your “circle paint.img”?

Can you post the contents of the MsgBox that appears? You can copy the contents of the MsgBox by pressing Ctrl+C while the MsgBox is the active window (without trying to select any of the text, which you can’t do anyway).
Yups, you're right. The Msgbox is triggered from this script after i press F4:

Code: Select all

F4::
...
	else
	{
		Msgbox Image not found : %needleDirA%, %winTarget%,%OutputX%,%OutputY%
		return
	}
it contains right for %needleDirA%, %winTarget%
and blank for %OutputX%,%OutputY% since the image is not found.

I did this with bitmapfromscreen and it worked, but fail when i try to change that bitmapfromscreen to become bitmapfromhwnd.

---------------------------
imagesearch inactivewin.ahk
---------------------------
Image not found : C:\Users\MyuserName\Desktop\ahk studies\test image\circle paint.bmp, Untitled.png - Paint,,
---------------------------
OK
---------------------------
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Search Image in an area in Inactive Window

30 Jan 2020, 10:25

Bochoko wrote:
30 Jan 2020, 09:26
I did this with bitmapfromscreen and it worked, but fail when i try to change that bitmapfromscreen to become bitmapfromhwnd.
add this line after bitmapFromHwnd:

Code: Select all

bmpHaystack := Gdip_BitmapFromHWND(hwnd := WinExist(winTarget))  ;you need the handle of the window to search in
msgbox, hwnd=%hwnd%     ; <-------
what do you get? we need to see if your winTarget is correct

feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

Re: Search Image in an area in Inactive Window  Topic is solved

30 Jan 2020, 16:56

global winTarget := "Untitled.png - Paint"
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Search Image in an area in Inactive Window

30 Jan 2020, 17:09

what windows version? what app are u gonna imagesearch? and define "off screen" and "inactive window". theres "ive only got 1 monitor and the window is maximized behind some other one"-off screen. theres "ive only got 1 monitor and the window is minimized"-off screen. theres "the same but for many monitors"-off screen. theres "ive only got many monitors and the window is maximized and visible on another screen"-off screen. many possible configurations
im asking this because if ure on win10 and trying to use this for some dumb game, 99.999999999% ure wasting ur time Gdip_BitmapFromHWND(it will fetch incomplete or pitch black images with wrong coordinates, if it even does anything at all)
Bochoko
Posts: 9
Joined: 30 Jan 2020, 03:07

Re: Search Image in an area in Inactive Window

30 Jan 2020, 23:36

guest3456 wrote:
30 Jan 2020, 10:25
Bochoko wrote:
30 Jan 2020, 09:26
I did this with bitmapfromscreen and it worked, but fail when i try to change that bitmapfromscreen to become bitmapfromhwnd.
add this line after bitmapFromHwnd:

Code: Select all

bmpHaystack := Gdip_BitmapFromHWND(hwnd := WinExist(winTarget))  ;you need the handle of the window to search in
msgbox, hwnd=%hwnd%     ; <-------
what do you get? we need to see if your winTarget is correct

thank you, i think it failed

---------------------------
imagesearch inactivewin.ahk
---------------------------
hwnd=0x0
---------------------------
OK
---------------------------


Can you help me how to get it right?
I'm still using windows 7.
Bochoko
Posts: 9
Joined: 30 Jan 2020, 03:07

Re: Search Image in an area in Inactive Window

30 Jan 2020, 23:42

swagfag wrote:
30 Jan 2020, 17:09
what windows version? what app are u gonna imagesearch? and define "off screen" and "inactive window". theres "ive only got 1 monitor and the window is maximized behind some other one"-off screen. theres "ive only got 1 monitor and the window is minimized"-off screen. theres "the same but for many monitors"-off screen. theres "ive only got many monitors and the window is maximized and visible on another screen"-off screen. many possible configurations
im asking this because if ure on win10 and trying to use this for some dumb game, 99.999999999% ure wasting ur time Gdip_BitmapFromHWND(it will fetch incomplete or pitch black images with wrong coordinates, if it even does anything at all)
thank you for your concern, i'm still on win 7. I just wanna see it works first, about how off screen it will be or the coordinate will go wrong we'll figure out the solution later if that happens.
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Search Image in an area in Inactive Window

30 Jan 2020, 23:49

@Bochoko - Did you ignore feiyue’s post? He’s definitely right that you had a variable scope problem.
feiyue wrote:
30 Jan 2020, 16:56
global winTarget := "Untitled.png - Paint"
Bochoko
Posts: 9
Joined: 30 Jan 2020, 03:07

Re: Search Image in an area in Inactive Window

31 Jan 2020, 00:41

feiyue wrote:
30 Jan 2020, 16:56
global winTarget := "Untitled.png - Paint"
Thank you so much Feiyue. That's great, It's working now. Can you tell me, why i should put global there?
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Search Image in an area in Inactive Window

31 Jan 2020, 00:55

It’s like I said. It’s a variable scope problem. You assigned its value outside a function, and that variable doesn’t exist inside your CustomImgSearch function unless you make it global or otherwise pass it to it.
Bochoko
Posts: 9
Joined: 30 Jan 2020, 03:07

Re: Search Image in an area in Inactive Window

31 Jan 2020, 01:13

boiler wrote:
31 Jan 2020, 00:55
It’s like I said. It’s a variable scope problem. You assigned its value outside a function, and that variable doesn’t exist inside your CustomImgSearch function unless you make it global or otherwise pass it to it.
oh i see what you mean, so function will not use any variable outside of it if it's not declared as global, and declaring it on top doesn't mean it's global, you need to add the global word.
Thank you :)
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Search Image in an area in Inactive Window

31 Jan 2020, 01:59

Bochoko wrote:
31 Jan 2020, 01:13
oh i see what you mean, so function will not use any variable outside of it if it's not declared as global, and declaring it on top doesn't mean it's global, you need to add the global word.
Thank you :)
declaring it on top does make it global already, so alternatively you could access that global variable by adding the line global winTarget as the first line of your function. by adding the "global" prefix that fieyue suggseted, you are making that variable super-global, which means you dont have to explicitly define it inside your func.

or you could create a local variable inside your function and define your winTarget string in there. or you could create a parameter for your func where you pass the wintitle string in (which is the best idea)

Bochoko
Posts: 9
Joined: 30 Jan 2020, 03:07

Re: Search Image in an area in Inactive Window

31 Jan 2020, 02:46

guest3456 wrote:
31 Jan 2020, 01:59
Bochoko wrote:
31 Jan 2020, 01:13
oh i see what you mean, so function will not use any variable outside of it if it's not declared as global, and declaring it on top doesn't mean it's global, you need to add the global word.
Thank you :)
declaring it on top does make it global already, so alternatively you could access that global variable by adding the line global winTarget as the first line of your function. by adding the "global" prefix that fieyue suggseted, you are making that variable super-global, which means you dont have to explicitly define it inside your func.

or you could create a local variable inside your function and define your winTarget string in there. or you could create a parameter for your func where you pass the wintitle string in (which is the best idea)
Thank you so much for the explanation, now i really understand it :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Theda and 272 guests