SaeidN

ImageSearch and MouseDrag in background window

33 posts in this topic

Hi,

I saw a couple topics about both mousedrag and imagesearch in background or minimized window, but none of them has the solution, and they were old topics.

So I wanted to ask whether there is a way to mouse drag without the cursor to move, (I want to use mouse when mousedrag is working on a window). I don't know whether I can mix ControlMove with some other stuff or any other way?

And also for the image search, is there any way to search for image (or pixel) in a background or minimized window? I thought maybe there is a au3 or dll or ... file for doing this.

Another question I have: Any way to pinch zoom (two fingers) a window like "windows photo viewer" to zoom in or out? I saw something almost the same here, but not what I want.

I appreciate it.

Share this post


Link to post
Share on other sites



If I remember correctly there's an alternate ImageSearch function (in the dll) that takes a handle to a bitmap, the bitmap you can get by using CaptureScreen. Then you can use SendMessage to send

  1. $WM_LBUTTONDOWN,
  2. $WM_MOUSEMOVE
  3. $WM_LBUTTONUP
#RequireAdmin
Func CaptureWindow($iLeft = 0, $iTop = 0, $iWidth = -1, $iHeight = -1, Const $bSaveImage = False, Const $hWnd = WinGetHandle("[Active]"))
    Local $rect_window = WinGetPos($hWnd)
    Local $hDC = _WinAPI_GetWindowDC($hWnd)
    Local $hDestDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $rect_window[2] - 16, $rect_window[3] - 38)
    Local $hDestSv = _WinAPI_SelectObject($hDestDC, $hBitmap)
    Local $hSrcDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBmp = _WinAPI_CreateCompatibleBitmap($hDC, $rect_window[2] - 16, $rect_window[3] - 38)
    Local $hSrcSv = _WinAPI_SelectObject($hSrcDC, $hBmp)

    _WinAPI_PrintWindow($hWnd, $hSrcDC, True)

    ScreenToClient($hWnd, $iLeft, $iTop)

    If ($iWidth > 0 and $iHeight > 0) Then
        _WinAPI_BitBlt($hDestDC, $iLeft, $iTop, $iWidth, $iHeight, $hSrcDC, $iLeft, $iTop, $MERGECOPY)
    Else
        _WinAPI_BitBlt($hDestDC, $iLeft, $iTop, $rect_window[2] - 16 - $iLeft, $rect_window[3] - 38 - $iTop, $hSrcDC, $iLeft, $iTop, $MERGECOPY)
    EndIf

    _WinAPI_SelectObject($hDestDC, $hDestSv)
    _WinAPI_SelectObject($hSrcDC, $hSrcSv)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    _WinAPI_DeleteDC($hDestDC)
    _WinAPI_DeleteDC($hSrcDC)
    _WinAPI_DeleteObject($hBmp)

    If ($image_search_tolerance < 50) Then $image_search_tolerance += 50
    If ($image_search_tolerance > 80) Then $image_search_tolerance = 80

    If ($bSaveImage) Then _ScreenCapture_SaveImage(@ScriptDir & "\CaptureWindow.jpg", $hBitmap, False)

    Return $hBitmap
EndFunc   ;==>CaptureWindow
DllCall("ImageSearchDll.dll", "str", "ImageSearchExt", "int", $iLeft, "int", $iTop, "int", $iRight, "int", $iBottom, "int", $iTolerance, "ptr", $hHbitmapImageToFind, "ptr", $hHbitmapImageToSearch)

Should get you started. Just put the DLLCall in your ImageSearch function. $hHBitmapImageToSearch is gonna be your CaptureWindow and $hHbitmapImage is a handle to a bitmap of the image to look for

1 person likes this

Share this post


Link to post
Share on other sites
9 hours ago, InunoTaishou said:

If I remember correctly there's an alternate ImageSearch function (in the dll) that takes a handle to a bitmap, the bitmap you can get by using CaptureScreen. Then you can use SendMessage to send

  1. $WM_LBUTTONDOWN,
  2. $WM_MOUSEMOVE
  3. $WM_LBUTTONUP
#RequireAdmin
Func CaptureWindow($iLeft = 0, $iTop = 0, $iWidth = -1, $iHeight = -1, Const $bSaveImage = False, Const $hWnd = WinGetHandle("[Active]"))
    Local $rect_window = WinGetPos($hWnd)
    Local $hDC = _WinAPI_GetWindowDC($hWnd)
    Local $hDestDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $rect_window[2] - 16, $rect_window[3] - 38)
    Local $hDestSv = _WinAPI_SelectObject($hDestDC, $hBitmap)
    Local $hSrcDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBmp = _WinAPI_CreateCompatibleBitmap($hDC, $rect_window[2] - 16, $rect_window[3] - 38)
    Local $hSrcSv = _WinAPI_SelectObject($hSrcDC, $hBmp)

    _WinAPI_PrintWindow($hWnd, $hSrcDC, True)

    ScreenToClient($hWnd, $iLeft, $iTop)

    If ($iWidth > 0 and $iHeight > 0) Then
        _WinAPI_BitBlt($hDestDC, $iLeft, $iTop, $iWidth, $iHeight, $hSrcDC, $iLeft, $iTop, $MERGECOPY)
    Else
        _WinAPI_BitBlt($hDestDC, $iLeft, $iTop, $rect_window[2] - 16 - $iLeft, $rect_window[3] - 38 - $iTop, $hSrcDC, $iLeft, $iTop, $MERGECOPY)
    EndIf

    _WinAPI_SelectObject($hDestDC, $hDestSv)
    _WinAPI_SelectObject($hSrcDC, $hSrcSv)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    _WinAPI_DeleteDC($hDestDC)
    _WinAPI_DeleteDC($hSrcDC)
    _WinAPI_DeleteObject($hBmp)

    If ($image_search_tolerance < 50) Then $image_search_tolerance += 50
    If ($image_search_tolerance > 80) Then $image_search_tolerance = 80

    If ($bSaveImage) Then _ScreenCapture_SaveImage(@ScriptDir & "\CaptureWindow.jpg", $hBitmap, False)

    Return $hBitmap
EndFunc   ;==>CaptureWindow
DllCall("ImageSearchDll.dll", "str", "ImageSearchExt", "int", $iLeft, "int", $iTop, "int", $iRight, "int", $iBottom, "int", $iTolerance, "ptr", $hHbitmapImageToFind, "ptr", $hHbitmapImageToSearch)

Should get you started. Just put the DLLCall in your ImageSearch function. $hHBitmapImageToSearch is gonna be your CaptureWindow and $hHbitmapImage is a handle to a bitmap of the image to look for

I included <ScreenCapture.au3> and <WinAPIEx.au3>. But still giving me couple error here.

I appreciate if you would show me where to get imagesearch dll file.

And also is $WM_LBUTTONDOWN and 2 others for mousedrag?

error.JPG

Share this post


Link to post
Share on other sites
#include <ScreenCapture.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Func ScreenToClient(Const ByRef $hWnd, ByRef $iX, ByRef $iY)
    Local $tReturn = DLLStructCreate("int iX;int iY")
    DllStructSetData($tReturn, 1, $iX)
    DllStructSetData($tReturn, 2, $iY)
    
    DllCall("user32.dll", "int", "ScreenToClient", "hwnd", $hWnd, "ptr", DllStructGetPtr($tReturn))
    
    $iX = DllStructGetData($tReturn, 1)
    $iY = DllStructGetData($tReturn, 2)
    $tReturn = Null
EndFunc   ;==>ClientToScreen

Func ClientToScreen(Const ByRef $hWnd, ByRef $iX, ByRef $iY)
    Local $tReutrn = DllStructCreate("int iX;int iY")
    DllStructSetData($tReturn, 1, $iX)
    DllStructSetData($tReturn, 2, $iY)
    
    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($tReturn))
    
    $iX = DllStructGetData($tReturn, 1)
    $iY = DllStructGetData($tReturn, 2)
    $tReturn = Null
EndFunc   ;==>ClientToScreen

Those windows messages (WM_*) variables send a message to the window telling it that the mouse has done that action (left button down, move, left button up) which is a mouse drag

Share this post


Link to post
Share on other sites
9 hours ago, InunoTaishou said:
#include <ScreenCapture.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Func ScreenToClient(Const ByRef $hWnd, ByRef $iX, ByRef $iY)
    Local $tReturn = DLLStructCreate("int iX;int iY")
    DllStructSetData($tReturn, 1, $iX)
    DllStructSetData($tReturn, 2, $iY)
    
    DllCall("user32.dll", "int", "ScreenToClient", "hwnd", $hWnd, "ptr", DllStructGetPtr($tReturn))
    
    $iX = DllStructGetData($tReturn, 1)
    $iY = DllStructGetData($tReturn, 2)
    $tReturn = Null
EndFunc   ;==>ClientToScreen

Func ClientToScreen(Const ByRef $hWnd, ByRef $iX, ByRef $iY)
    Local $tReutrn = DllStructCreate("int iX;int iY")
    DllStructSetData($tReturn, 1, $iX)
    DllStructSetData($tReturn, 2, $iY)
    
    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($tReturn))
    
    $iX = DllStructGetData($tReturn, 1)
    $iY = DllStructGetData($tReturn, 2)
    $tReturn = Null
EndFunc   ;==>ClientToScreen

Those windows messages (WM_*) variables send a message to the window telling it that the mouse has done that action (left button down, move, left button up) which is a mouse drag

I'm sorry, I'm not getting it. I know what it means to send message to a window to do a mouse action, but I don't know how to do it. Would you please give me a simple example (maybe with paint?), so I read it and understand what's happening?

Thank you

Share this post


Link to post
Share on other sites

There are plenty of examples of how to use SendMessage on the forums and in the help document
 

You can check that topic to see how they used SendMessage to click on a window. Modify it to do what you need.

Share this post


Link to post
Share on other sites
3 hours ago, InunoTaishou said:

There are plenty of examples of how to use SendMessage on the forums and in the help document
 

You can check that topic to see how they used SendMessage to click on a window. Modify it to do what you need.

I have checked https://www.autoitscript.com/autoit3/docs/libfunctions/_SendMessage.htm and it helped me understand better, but I don't know what are the codes in front of variables in the example and how can I obtain them. Like these two: 

 Local Const $WM_SYSCOMMAND = 274
    Local Const $SC_MONITORPOWER = 61808

So far I have checked two other topics and no result yet, but I'll search more.

Share this post


Link to post
Share on other sites

Okay. I read topics below . I really do not know what's wrong with the code in that topic. There were two other topics that doesn't give any clue at all. The codes in these topics don't give any error. I wish it was giving me error so I know what to fix.

I'm so tired of searching the whole net for almost 10 days. I also found this file, which is a good resource to send key and mouse message to a window, but again.

Doesn't work. Maybe because it's old or my win is 64bit.

 

 

 

PostMessage.au3

Share this post


Link to post
Share on other sites

I changed

$result = DllCall($h_ImageSearchDLL, "str", "ImageSearch", "int", $x1, "int", $y1, "int", $right, "int", $bottom, "str", $findImage)

to

DllCall("ImageSearchDll.dll", "str", "ImageSearchExt", "int", $iLeft, "int", $iTop, "int", $iRight, "int", $iBottom, "int", $iTolerance, "ptr", $hHbitmapImageToFind, "ptr", $hHbitmapImageToSearch)

I also have my screencapture as $hHbitmapImageToFind and the image to search as $hHbitmapImageToSearch.

But didn't work, anything else I need to change?

I wish I could have a simple example, so I could look and see what's happening. I literally searched the whole net, and didn't find an example, even in other languages other than Eng.

How can I know the difference between ImageSearchEx and ImageSearchExt, and ImageSearch. How Do I know how many parameters they should have?

What's the difference between str and ptr, Are they string and pointer? Why do we need pointer?

Share this post


Link to post
Share on other sites

Ayone knows?

I'm tired of searching and reading the tutorials. It's been two weeks now I'm looking for a solution and every day I'm spending more than 6 hours to find something, and no result yet. Please help.

Share this post


Link to post
Share on other sites

Most serious apps don't need ImageSearch, 99,9% of them can be automated with better AutoIt funcs or UDF's. So what's the name of the app. And also please post a runable (reproducer) script showing what you alreadey tried and showing the issues.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

1 hour ago, AutoBert said:

Most serious apps don't need ImageSearch, 99,9% of them can be automated with better AutoIt funcs or UDF's. So what's the name of the app. And also please post a runable (reproducer) script showing what you alreadey tried and showing the issues.

I already wrote about 900 lines of codes, and it works perfect. I just want to do the imagesearch for inactive windows too. I wanna write some programs and scripts for our club and other events in univesity. The imageseach and mousedrag that I have now, which is only for active windows is not an efficient way to present as a project. What other UDFs or funcs works better for doing these in inactive windows?

If I see a small example in Paint or any other app for both imagesearch and mousedrag, then I can implement it in my script.

I appreciate your help.

Edited by SaeidN

Share this post


Link to post
Share on other sites
12 minutes ago, SaeidN said:

I already wrote about 900 lines of codes, and it works perfect.

Fine than mark as solved :)

But seems ImageSearch make problems so name the app to find a better way.

Share this post


Link to post
Share on other sites
Just now, AutoBert said:

Fine than mark as solved :)

But seems ImageSearch make problems so name the app to find a better way.

I didn't get them to work in inactive window, so it's not solved.

It's not a commercial app or something, I wrote it and it doesn't have any name. I just need an example which I don't think it would be more than 20 lines.

Share this post


Link to post
Share on other sites

Is it written with AutoIt? Than it's very easy to find a solution.

Share this post


Link to post
Share on other sites
29 minutes ago, AutoBert said:

Is it written with AutoIt? Than it's very easy to find a solution.

Yes, it's in Autoit. I wish I knew how to do it. :(

Share this post


Link to post
Share on other sites

Use Au3Info and the control* functions.

Share this post


Link to post
Share on other sites
1 minute ago, AutoBert said:

Use Au3Info and the control* functions.

But that's just for click or send keys. i don't know how is it for drag and searchimage an inactive window.

Share this post


Link to post
Share on other sites

You can also use ControlMove to move the controls in your background GUI.

Share this post


Link to post
Share on other sites
12 minutes ago, AutoBert said:

You can also use ControlMove to move the controls in your background GUI.

But it doesnt move the mouse, click, drag in an inactive window. and also imagesearh.

It if does mousedrag and imagesearch in an inactive window so please give me an example to see how it is working.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • DeathChicken
      By DeathChicken
      If possible please add or edit the comments to explain how this works.
       
      ;includes functions from other things
      #include <GDIPlus.au3>
      #include <ScreenCapture.au3>
      ;hotkeys
      HotKeySet("{ESC}", _exit)
      HotKeySet("{F1}", _scan)
      ;global variables
      Global $win_title                    ;name of the window
      Global $area_x                        ;
      Global $area_y                        ;
      Global $area_w                       ;
      Global $area_h                        ;
      Global $cursor                         ;
      Global $rect_file                      ;
      Global $hbmpscreen              ;
      Global $i=  1043                      ;moves the rect
      Global $ii=    378                      ;moves the rect
      Global $x=1044                        ;inner rect offset
      Global $y=501                          ;inner rect offset
      Global $hbmprect                    ;image inside rect
      Global $hscreen                        ;image whole screen
                                         
      _GDIPlus_Startup()                    ;?
      ;display hotkeys on screen
      ToolTip("Press F1 to scan | Press ESC to Exit",0,0)
                                          ;infinite loop to keep prog running
      While 1
          Sleep(100)
      WEnd
      Func _scan()
      ;~ reads your screen area:
      _read()
      ;~ converts screen captured into bmp
      _convert()
      ;~ .  Loads converted bmp to be read
      _loadBMP()
      ;~ compares the bmp of your scanned screen to the actual screen that is being displayed
      _compare()
      EndFunc
                                          ;reads screen
      Func _read()
          $hscreen = _ScreenCapture_CaptureWnd("", WinGetHandle($win_title), $area_x, $area_y, $area_x + $area_w, $area_y + $area_h, $cursor)
      EndFunc
                                          ;converts screen into bmp
      Func _convert()
          _GDIPlus_BitmapCreateFromHBITMAP($hscreen)
      EndFunc
                                          ;loads converted screen
      Func _loadBMP()
      _GDIPlus_BitmapCreateFromFile($rect_file)
      EndFunc
                                          ;compares savedBMP to current screen
      Func _compare()
      ;If(                              below code                                     ) = (                   below code           ) Then
      If _GDIPlus_BitmapGetPixel($hbmpscreen, ($i + $x) - $area_x, ($ii + $y) - $area_y) = _GDIPlus_BitmapGetPixel($hbmprect, $x, $y) Then
      ;display message box titled found with a message of found
      MsgBox("","","found")
      ;if above is not correct then
      EndIf
      EndFunc
                                          ;exit func
      Func _exit()
          Exit
      EndFunc
    • magmet00
      By magmet00
      Do you have a working imagesearch.au3 with working dll?
      and how can i gethandle and send keyboard/mouse commands to hidden d3d window?
       
      i have tried imagesearch2015 but im getting dll error every time and tried older imagesearchs too in forum what i found.
      i have tried many things to send commands to hidden/background d3dwindow still nothing works.
      Can you help me please?
       
    • knuxfighter
      By knuxfighter
      Hello. I've been working with Imagesearch library lately and it did a good work, although I moved to a new PC and didn't copy the old files with me so I downloaded the Imagesearch from the following post
       hoping that it will work. It doesn't though. First time I when I use (run as subscript to my code) the Imagesearch.au3, Scite finds errors (missing spaces). Ctrl+T (scite tidy) fixes these missing spaces but the script returns the following error on every run after:
      _ImageSearch('search.bmp', 0, $x, $y, 0) outputs
       
      "C:\Users\Knuckles\Desktop\AutoIt\include\ImageSearch.au3" (44) : ==> Subscript used on non-accessible variable.: If $result[0] = "0" Then Return 0 If $result^ ERROR no matter if I put the searched bmp in the script folder or folder img in the script directory. Also, it doesn't matter if the searched image on screen or not, it returns the same.

      Can you provide me any help please? I remember having these problems 2 years ago when I first met the imagesearch library also (I fixed it somehow though in that time). Seems nothing changed.

      Using this version posted in the following post gives the same error:
      Also I run windows 10 64bit and I have no shell options for script editing, running as x86 or whatever as I used to have on windows xp/7. I went through some steps like deleting a key in registry and I even reinstalled autoit and scite but that only resulted to au3 as unrecognized file format and not in getting back the menus and the icon on au3 files. Any thoughts on this?

      Edit: Installing 64-bit AutoIt and using 64bit ImageSearch is no change.
      code.au3

      FOUND WORKING: http://www.codebot.de/index.php/Thread/12713-Imagesearch-au3-funktioniert-nicht/
       but why is this one working and the original aren't? :(
    • masterland
      By masterland
      Hello.
      I had made a bot that clicks and doing stuff. It is working fine, but when i move it to another PC ImageSearch stop working. I captured same image of a button again and i observed that they are a little bit different. How to deal with this? I wonder if i will make black and white detection might work, or not ... i don't know. The point is i am not willing to re-do photos on each and every PC i move the scripts.
       
      ClickOnImageWithToleranceFast("images/fb_about.bmp",16) Func ClickOnImageWithToleranceFast($image_path,$tolerance) If SearchForImageOnScreenWithTolerance($image_path,$tolerance) Then If $x <> 0 And $y <> 0 Then MouseMove($x+5, $y+5,0) Sleep(500) MouseClick("left") Sleep(500) EndIf EndIf EndFunc  
      Link : https://postimg.org/gallery/z6n168ps/585debf1/#
      Also i have attached them.
      Thank you
      fb_about.bmp
      fb_about2.bmp
    • Miliardsto
      By Miliardsto
      Hello i got problem with ImageSearchDll.dll
      It works for me perfect always I made program which one use a lot of ImageSearch funcs on 64bit.
      I got 64bit and i copied to c:/Windows - ImageSearchDll.dll 64bit version
      But I encountered some friends got 32bit OS and then if I compile program for 32bit then they put ImageSearchDll.dll 32bit ver on windows dir and program crash when function is call.
      it happens always :/
       
      How to run this func on 32bit?