Jump to content

ImageSearch and MouseDrag in background window


SaeidN
 Share

Recommended Posts

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.

Link to comment
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

Link to comment
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

Link to comment
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

Link to comment
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

Link to comment
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.

Link to comment
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

Link to comment
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?

Link to comment
Share on other sites

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
Link to comment
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.

Link to comment
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.

Link to comment
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
 Share

×
×
  • Create New...