Jump to content

Recommended Posts

Posted

 image.png

This program is an example of using ImageSearchUDF.

Features:
 - Multi-Target Search: Configure and search for up to 12 different images.
 - Selective Search: Use checkboxes to select which images to include in a search operation.
 - Dual Search Modes:
     - Multi Search: Finds all selected images in a single, efficient operation.
     - Single Search: Iterates through selected images, searching for them one by one.
 - Configurable Actions: For each found image, you can:
     - Move the mouse to the location.
     - Perform a single or double left-click.
     - Set a custom delay after each action.
 - Advanced Search Parameters:
     - Wait for Image: Pauses the script and waits for an image to appear within a specified timeout.
     - Color Tolerance: Set a tolerance value (0-255) to find images that are not an exact match.
     - Custom Search Area: Define a specific rectangular region on the screen to search within.
     - Visual Area Selection: A "Select Area" button allows you to draw a rectangle on the screen to define coordinates.
 - Interactive GUI:
     - Image Previews: A dedicated panel shows all 12 target image slots.
     - Individual Image Creation: Each slot has its own "Create" button to capture a new image.
     - Bulk Selection: "Select All" and "Deselect All" buttons for quick management.
     - Status Bar: Provides real-time feedback on the current operation.
     - Activity Log: Logs all operations, results, and errors with timestamps.
     - System Information Panel: Displays key details about the environment (OS, AutoIt, DLL versions).
 - Robust Error Handling:
     - Clearly distinguishes between "image not found" and critical DLL/UDF errors.
     - Smart Validation: Automatically skips and deselects checked images that do not exist on disk.
     - Debug Mode: An option to enable detailed logging directly from the DLL for advanced troubleshooting.

#RequireAdmin
#cs ----------------------------------------------------------------------------
    ;
    ;    Title .........: ImageSearch Automation Suite
    ;    AutoIt Version : 3.3.16.1
    ;    Author ........: Dao Van Trong (TRONG.PRO)
    ;    Date ..........: 2025-07-23
    ;   ;Note...........: Include ImageSearch UDF Embedded dll version only supported from Windows 7-11 (Windows XP not supported)
    ;    Script Function:
    ;    An ultimate, professional GUI tool for complex, configurable image search tasks,
    ;    built upon the high-performance ImageSearch UDF.
    ;
    ;    Features:
    ;    - Multi-Target Search: Configure and search for up to 12 different images.
    ;    - Selective Search: Use checkboxes to select which images to include in a search operation.
    ;    - Dual Search Modes:
    ;        - Multi Search: Finds all selected images in a single, efficient operation.
    ;        - Single Search: Iterates through selected images, searching for them one by one.
    ;    - Configurable Actions: For each found image, you can:
    ;        - Move the mouse to the location.
    ;        - Perform a single or double left-click.
    ;        - Set a custom delay after each action.
    ;    - Advanced Search Parameters:
    ;        - Wait for Image: Pauses the script and waits for an image to appear within a specified timeout.
    ;        - Color Tolerance: Set a tolerance value (0-255) to find images that are not an exact match.
    ;        - Custom Search Area: Define a specific rectangular region on the screen to search within.
    ;        - Visual Area Selection: A "Select Area" button allows you to draw a rectangle on the screen to define coordinates.
    ;    - Interactive GUI:
    ;        - Image Previews: A dedicated panel shows all 12 target image slots.
    ;        - Individual Image Creation: Each slot has its own "Create" button to capture a new image.
    ;        - Bulk Selection: "Select All" and "Deselect All" buttons for quick management.
    ;        - Status Bar: Provides real-time feedback on the current operation.
    ;        - Activity Log: Logs all operations, results, and errors with timestamps.
    ;        - System Information Panel: Displays key details about the environment (OS, AutoIt, DLL versions).
    ;    - Robust Error Handling:
    ;        - Clearly distinguishes between "image not found" and critical DLL/UDF errors.
    ;        - Smart Validation: Automatically skips and deselects checked images that do not exist on disk.
    ;        - Debug Mode: An option to enable detailed logging directly from the DLL for advanced troubleshooting.
    ;
#ce ----------------------------------------------------------------------------
#include <Array.au3>
#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <Date.au3>
#include <Misc.au3>
#include <GuiEdit.au3>
#include <GuiStatusBar.au3>
#include "ImageSearch_UDF.au3"

;Opt("MustDeclareVars", 1)

; === GLOBAL CONSTANTS AND VARIABLES ===
Global Const $MAX_IMAGES = 12
Global Const $g_sPlaceholderPath = @WindowsDir & "\Web\Wallpaper\Windows\img0.jpg"
Global $g_asImagePaths[$MAX_IMAGES], $g_nMsg, $g_hMainGUI, $g_hLog, $g_hStatusBar
; --- GUI Control IDs ---
Global $g_idBtnStart, $g_idBtnSelectAll, $g_idBtnDeselectAll, $g_idBtnSelectArea
Global $g_idInputDelay, $g_idChkMoveMouse
Global $g_idRadNoClick, $g_idRadSingleClick, $g_idRadDoubleClick
Global $g_idChkWait, $g_idInputWaitTime
Global $g_idChkUseArea, $g_idInputLeft, $g_idInputTop, $g_idInputRight, $g_idInputBottom
Global $g_idChkMultiSearch, $g_idChkUseTolerance, $g_idInputTolerance, $g_idChkEnableDebug
Global $g_aidPic[$MAX_IMAGES], $g_aidChkSearch[$MAX_IMAGES], $g_aidBtnCreate[$MAX_IMAGES]

_Main()

; #FUNCTION# ====================================================================================================================
; Name...........: _Main
; Description....: Main program entry point. Initializes components and enters the GUI message loop.
; ===============================================================================================================================
Func _Main()
    _GDIPlus_Startup()
    _InitializeImagePaths()
    _CreateGUI()
    _UpdateAllImagePreviews()

    ; Main message loop to handle GUI events.
    While 1
        $g_nMsg = GUIGetMsg()
        Switch $g_nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $g_idBtnStart
                _StartSearch()

            Case $g_idBtnSelectAll
                _SelectAll(True)

            Case $g_idBtnDeselectAll
                _SelectAll(False)

            Case $g_idBtnSelectArea
                _SelectAreaOnScreen()

            Case $g_aidBtnCreate[0] To $g_aidBtnCreate[$MAX_IMAGES - 1]
                _HandleImageCreation($g_nMsg)

        EndSwitch
    WEnd
    _Exit()
EndFunc   ;==>_Main

; === GUI AND INITIALIZATION FUNCTIONS ===

; #FUNCTION# ====================================================================================================================
; Name...........: _InitializeImagePaths
; Description....: Populates the global array with default paths for the search images.
; ===============================================================================================================================
Func _InitializeImagePaths()
    For $i = 0 To $MAX_IMAGES - 1
        $g_asImagePaths[$i] = @ScriptDir & "\Search_" & $i + 1 & ".bmp"
    Next
EndFunc   ;==>_InitializeImagePaths

; #FUNCTION# ====================================================================================================================
; Name...........: _CreateGUI
; Description....: Creates the entire graphical user interface, defining all controls and their positions.
; ===============================================================================================================================
Func _CreateGUI()
    $g_hMainGUI = GUICreate("ImageSearch Automation Suite  by  Dao Van Trong - TRONG.PRO", 904, 650)

    ; --- TOP: CONFIGURATION ---
    GUICtrlCreateGroup("Configuration", 10, 10, 390, 276)
    ; --- Search Mode ---
    GUICtrlCreateGroup("Search Mode", 20, 30, 180, 150)
    $g_idChkMultiSearch = GUICtrlCreateCheckbox("Multi Search (All at once)", 30, 50, 140, 20)
    $g_idChkWait = GUICtrlCreateCheckbox("Wait for Image Found", 30, 75, 140, 20)
    $g_idChkUseTolerance = GUICtrlCreateCheckbox("Use Tolerance", 30, 100, 140, 20)
    GUICtrlSetState(-1, $GUI_CHECKED)
    $g_idChkUseArea = GUICtrlCreateCheckbox("Use Custom Area", 30, 125, 140, 20)
    $g_idChkEnableDebug = GUICtrlCreateCheckbox("Enable DLL Debug", 30, 150, 140, 20)
    GUICtrlSetState(-1, $GUI_CHECKED)
    GUICtrlSetTip(-1, "Log detailed information from the DLL.")
    ; --- Parameters ---
    GUICtrlCreateGroup("Parameters", 210, 30, 180, 94)
    GUICtrlCreateLabel("Timeout (ms)", 220, 50, 80, 20)
    $g_idInputWaitTime = GUICtrlCreateInput("5000", 300, 47, 80, 21)
    GUICtrlCreateLabel("Tolerance:", 220, 75, 80, 20)
    $g_idInputTolerance = GUICtrlCreateInput("10", 300, 72, 80, 21)
    GUICtrlCreateLabel("Delay (ms)", 220, 100, 80, 20)
    $g_idInputDelay = GUICtrlCreateInput("500", 300, 97, 80, 21)
    ; --- Search Area ---
    GUICtrlCreateGroup("Search Area", 16, 182, 188, 98)
    GUICtrlCreateLabel("Left:", 26, 202, 30, 20)
    $g_idInputLeft = GUICtrlCreateInput("0", 61, 199, 50, 21)
    GUICtrlCreateLabel("Top:", 121, 202, 30, 20)
    $g_idInputTop = GUICtrlCreateInput("0", 156, 199, 30, 21)
    GUICtrlCreateLabel("Right:", 26, 227, 35, 20)
    $g_idInputRight = GUICtrlCreateInput(@DesktopWidth, 61, 224, 50, 21)
    GUICtrlCreateLabel("Bottom:", 121, 227, 40, 20)
    $g_idInputBottom = GUICtrlCreateInput(@DesktopHeight, 156, 224, 30, 21)
    $g_idBtnSelectArea = GUICtrlCreateButton("Select Area", 24, 248, 163, 25)
    ; --- Actions on Found ---
    GUICtrlCreateGroup("Actions on Found", 214, 134, 174, 146)
    $g_idChkMoveMouse = GUICtrlCreateCheckbox("Move Mouse", 224, 154, 100, 20)
    GUICtrlSetState(-1, $GUI_CHECKED)
    GUICtrlCreateLabel("Click:", 224, 179, 40, 20)
    $g_idRadNoClick = GUICtrlCreateRadio("None", 264, 179, 55, 20)
    GUICtrlSetState(-1, $GUI_CHECKED)
    $g_idRadSingleClick = GUICtrlCreateRadio("Single", 224, 199, 60, 20)
    $g_idRadDoubleClick = GUICtrlCreateRadio("Double", 284, 199, 65, 20)

    ; --- RIGHT COLUMN: IMAGES & INFO ---
    GUICtrlCreateGroup("Image Targets", 410, 6, 486, 464)
    Local $iPicWidth = 100, $iPicHeight = 100
    Local $iX_Start = 425, $iY_Start = 38
    Local $iX = $iX_Start, $iY = $iY_Start
    Local $iColWidth = 118
    For $i = 0 To $MAX_IMAGES - 1
        If $i > 0 And Mod($i, 4) = 0 Then ; New row
            $iX = $iX_Start
            $iY += 144
        EndIf
        $g_aidChkSearch[$i] = GUICtrlCreateCheckbox(String($i + 1), $iX, $iY, 34, 20)
        $g_aidBtnCreate[$i] = GUICtrlCreateButton("Create", $iX + 37, $iY, 59, 22)
        $g_aidPic[$i] = GUICtrlCreatePic("", $iX, $iY + 30, $iPicWidth, $iPicHeight, $SS_CENTERIMAGE)
        $iX += $iColWidth
    Next

    ; --- BOTTOM: LOGS & SYSTEM INFO ---
    GUICtrlCreateGroup("Activity Log", 13, 472, 880, 142)
    $g_hLog = GUICtrlCreateEdit("", 18, 487, 862, 114, BitOR($ES_MULTILINE, $ES_READONLY, $WS_VSCROLL, $ES_AUTOVSCROLL))
    GUICtrlSetFont(-1, 9, 400, 0, "Consolas")

    GUICtrlCreateGroup("System Information", 12, 363, 392, 104)
    GUICtrlCreateLabel("OS: " & @OSVersion & " (" & @OSArch & ")" & "   |   AutoIt: " & @AutoItVersion & (@AutoItX64 ? " (x64)" : ""), 28, 385, 360, 20)
    GUICtrlCreateLabel("DLL In Use:" & " v" & $__IMAGESEARCH_UDF_VERSION, 28, 410, 360, 20)
    GUICtrlCreateInput($g_sImageSearchDLL_Path, 23, 437, 360, 21, $ES_READONLY)

    ; --- MAIN ACTION BUTTONS ---
    $g_idBtnStart = GUICtrlCreateButton("Start Search", 9, 294, 264, 64, $BS_DEFPUSHBUTTON)
    GUICtrlSetFont(-1, 14, 700)
    $g_idBtnSelectAll = GUICtrlCreateButton("Select All", 295, 292, 105, 33)
    $g_idBtnDeselectAll = GUICtrlCreateButton("Deselect All", 295, 332, 105, 25)

    ; --- STATUS BAR ---
    $g_hStatusBar = _GUICtrlStatusBar_Create($g_hMainGUI)
    _UpdateStatus("Ready")

    GUISetState(@SW_SHOW)
EndFunc   ;==>_CreateGUI


; === CORE LOGIC FUNCTIONS ===

; #FUNCTION# ====================================================================================================================
; Name...........: _StartSearch
; Description....: Gathers all settings from the GUI, validates them, and initiates the appropriate search function.
; ===============================================================================================================================
Func _StartSearch()
    GUICtrlSetData($g_hLog, "")
    _UpdateStatus("Starting search...")

    ; --- Read and Validate GUI inputs ---
    Local $iDelay = Number(GUICtrlRead($g_idInputDelay))
    Local $bMoveMouse = (GUICtrlRead($g_idChkMoveMouse) = $GUI_CHECKED)
    Local $iClickType = 0 ; 0 = None, 1 = Single, 2 = Double
    If GUICtrlRead($g_idRadSingleClick) = $GUI_CHECKED Then $iClickType = 1
    If GUICtrlRead($g_idRadDoubleClick) = $GUI_CHECKED Then $iClickType = 2
    Local $bWaitSearch = (GUICtrlRead($g_idChkWait) = $GUI_CHECKED)
    Local $iWaitTime = Number(GUICtrlRead($g_idInputWaitTime))
    Local $bMultiSearch = (GUICtrlRead($g_idChkMultiSearch) = $GUI_CHECKED)
    Local $iTolerance = 0
    If GUICtrlRead($g_idChkUseTolerance) = $GUI_CHECKED Then
        $iTolerance = Number(GUICtrlRead($g_idInputTolerance))
    EndIf
    Local $iDebugMode = (GUICtrlRead($g_idChkEnableDebug) = $GUI_CHECKED ? 1 : 0)


    ; --- Determine Search Area ---
    Local $iLeft, $iTop, $iRight, $iBottom
    If GUICtrlRead($g_idChkUseArea) = $GUI_CHECKED Then
        $iLeft = GUICtrlRead($g_idInputLeft)
        $iTop = GUICtrlRead($g_idInputTop)
        $iRight = GUICtrlRead($g_idInputRight)
        $iBottom = GUICtrlRead($g_idInputBottom)
    Else
        $iLeft = 0
        $iTop = 0
        $iRight = @DesktopWidth
        $iBottom = @DesktopHeight
    EndIf

    ; --- Get list of images to search for, validating existence ---
    Local $aSearchList[1] = [0]
    For $i = 0 To $MAX_IMAGES - 1
        If GUICtrlRead($g_aidChkSearch[$i]) = $GUI_CHECKED Then
            If Not FileExists($g_asImagePaths[$i]) Then
                _LogWrite("WARN: Image " & $i + 1 & " not found. Unchecking and skipping.")
                GUICtrlSetState($g_aidChkSearch[$i], $GUI_UNCHECKED)
                _UpdateSingleImagePreview($i)
                ContinueLoop
            EndIf
            _ArrayAdd($aSearchList, $g_asImagePaths[$i])
            $aSearchList[0] += 1
        EndIf
    Next

    If $aSearchList[0] = 0 Then
        _LogWrite("ERROR: No valid images selected for search.")
        _UpdateStatus("Error: No valid images selected. Ready.")
        Return
    EndIf

    _LogWrite("====================================")
    _LogWrite("Starting search for " & $aSearchList[0] & " image(s)...")

    If $bMultiSearch Then
        _SearchMultipleImages($aSearchList, $bWaitSearch, $iWaitTime, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $iDebugMode, $bMoveMouse, $iClickType, $iDelay)
    Else
        _SearchSingleImages($aSearchList, $bWaitSearch, $iWaitTime, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $iDebugMode, $bMoveMouse, $iClickType, $iDelay)
    EndIf

    _LogWrite("====================================" & @CRLF)
    _UpdateStatus("Search complete. Ready.")
EndFunc   ;==>_StartSearch

; #FUNCTION# ====================================================================================================================
; Name...........: _SearchMultipleImages
; Description....: Performs a search for all selected images at once.
; Parameters.....: $aImageList - Array of image paths to search for.
;                  $bWait      - Boolean, True to use wait search, False otherwise.
;                  $iWaitTime  - Timeout in milliseconds for wait search.
;                  ... and other search parameters.
; ===============================================================================================================================
Func _SearchMultipleImages($aImageList, $bWait, $iWaitTime, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $iDebug, $bMove, $iClickType, $iDelay)
    _UpdateStatus("Mode: Multi Search (All at once)...")
    _LogWrite("Mode: Multi Search (All at once)")
    Local $sImageListStr = _ArrayToString($aImageList, "|", 1)
    Local $aResult
    If $bWait Then
        $aResult = _ImageSearch_WaitArea($iWaitTime, $sImageListStr, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, -1, 99, 1, $iDebug, 1.0, 1.0, 0.1, True)
    Else
        $aResult = _ImageSearch_Area($sImageListStr, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, -1, 99, 1, $iDebug, 1.0, 1.0, 0.1, True)
    EndIf

    If $iDebug = 1 Then _LogWrite("DLL Return: " & $g_sLastDllReturn)

    Local $iStatusCode = $aResult[0][0]
    If $iStatusCode > 0 Then
        _ProcessMultiResults($aResult, $bMove, $iClickType, $iDelay)
    Else
        _LogSearchError($iStatusCode)
    EndIf
EndFunc   ;==>_SearchMultipleImages

; #FUNCTION# ====================================================================================================================
; Name...........: _SearchSingleImages
; Description....: Performs a search for each selected image individually, one by one.
; Parameters.....: $aImageList - Array of image paths to search for.
;                  ... and other search parameters.
; ===============================================================================================================================
Func _SearchSingleImages($aImageList, $bWait, $iWaitTime, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $iDebug, $bMove, $iClickType, $iDelay)
    _LogWrite("Mode: Single Search (One by one)")
    Local $iTotalFound = 0
    For $i = 1 To $aImageList[0]
        Local $sCurrentImage = $aImageList[$i]
        Local $sImageName = StringRegExpReplace($sCurrentImage, ".+\\(.+)", "$1")
        _UpdateStatus("Searching for: " & $sImageName & "...")
        _LogWrite(" -> Searching for: " & $sImageName)
        Local $aResult
        If $bWait Then
            $aResult = _ImageSearch_WaitArea($iWaitTime, $sCurrentImage, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, -1, 1, 1, $iDebug, 1.0, 1.0, 0.1, False)
        Else
            $aResult = _ImageSearch_Area($sCurrentImage, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, -1, 1, 1, $iDebug, 1.0, 1.0, 0.1, False)
        EndIf

        If $iDebug = 1 Then _LogWrite("DLL Return: " & $g_sLastDllReturn)

        Local $iStatusCode = $aResult[0]
        If $iStatusCode > 0 Then
            $iTotalFound += 1
            _ProcessSingleResult($aResult, $bMove, $iClickType, $iDelay)
        Else
            _LogSearchError($iStatusCode)
        EndIf
    Next
    _LogWrite("Single search finished. Total matches found: " & $iTotalFound)
EndFunc   ;==>_SearchSingleImages

; #FUNCTION# ====================================================================================================================
; Name...........: _ProcessMultiResults
; Description....: Processes the 2D array result from a multi-image search and performs actions.
; Parameters.....: $aResult    - The 2D result array from the UDF.
;                  $bMove      - Boolean, whether to move the mouse.
;                  $iClickType - 0 for none, 1 for single, 2 for double click.
;                  $iDelay     - Delay in ms after actions.
; ===============================================================================================================================
Func _ProcessMultiResults($aResult, $bMove, $iClickType, $iDelay)
    Local $iFoundCount = $aResult[0][0]
    _LogWrite("Success: Found " & $iFoundCount & " match(es). Performing actions...")
    For $i = 1 To $iFoundCount
        Local $iX = $aResult[$i][1], $iY = $aResult[$i][2]
        _UpdateStatus("Performing action on match #" & $i & " at " & $iX & "," & $iY & "...")
        _LogWrite("  -> Found match #" & $i & " at X=" & $iX & ", Y=" & $iY)
        _HighlightFoundArea($iX, $iY, 0xFF00FF00)
        _PerformActions($iX, $iY, $bMove, $iClickType, $iDelay)
    Next
    _LogWrite("All actions complete for this search cycle.")
EndFunc   ;==>_ProcessMultiResults

; #FUNCTION# ====================================================================================================================
; Name...........: _ProcessSingleResult
; Description....: Processes the 1D array result from a single-image search and performs actions.
; Parameters.....: $aResult    - The 1D result array from the UDF.
;                  ... and other action parameters.
; ===============================================================================================================================
Func _ProcessSingleResult($aResult, $bMove, $iClickType, $iDelay)
    _LogWrite("Success: Found 1 match. Performing actions...")
    Local $iX = $aResult[1], $iY = $aResult[2]
    _UpdateStatus("Performing action on match at " & $iX & "," & $iY & "...")
    _LogWrite("  -> Found match at X=" & $iX & ", Y=" & $iY)
    _HighlightFoundArea($iX, $iY, 0xFF00FF00)
    _PerformActions($iX, $iY, $bMove, $iClickType, $iDelay)
EndFunc   ;==>_ProcessSingleResult

; #FUNCTION# ====================================================================================================================
; Name...........: _PerformActions
; Description....: Executes the user-defined actions (move, click, delay) at a given coordinate.
; Parameters.....: $iX, $iY    - The coordinates to perform actions at.
;                  ... and other action parameters.
; ===============================================================================================================================
Func _PerformActions($iX, $iY, $bMove, $iClickType, $iDelay)
    If $bMove Then
        _LogWrite("     - Moving mouse...")
        MouseMove($iX, $iY, 10)
    EndIf
    If $iClickType > 0 Then
        _LogWrite("     - Performing " & ($iClickType = 1 ? "single" : "double") & " click...")
        MouseClick("left", $iX, $iY, $iClickType, 0)
    EndIf
    _LogWrite("     - Delaying for " & $iDelay & "ms...")
    Sleep($iDelay)
EndFunc   ;==>_PerformActions


; === HELPER AND UTILITY FUNCTIONS ===

; #FUNCTION# ====================================================================================================================
; Name...........: _HandleImageCreation
; Description....: Event handler that determines which "Create" button was pressed and calls the creation function.
; Parameters.....: $nMsg - The control ID of the pressed button.
; ===============================================================================================================================
Func _HandleImageCreation($nMsg)
    For $i = 0 To $MAX_IMAGES - 1
        If $nMsg = $g_aidBtnCreate[$i] Then
            _CreateImageFile($g_asImagePaths[$i], "Create/Update Image " & $i + 1, $i)
            Return
        EndIf
    Next
EndFunc   ;==>_HandleImageCreation

; #FUNCTION# ====================================================================================================================
; Name...........: _CreateImageFile
; Description....: Manages the process of calling the screen capture function and updating the log/GUI.
; Parameters.....: $sFilePath - The path to save the image file.
;                  $sTitle    - The title for the capture window.
;                  $iIndex    - The index of the image slot being updated.
; ===============================================================================================================================
Func _CreateImageFile($sFilePath, $sTitle, $iIndex)
    _UpdateStatus("Preparing to create image " & $iIndex + 1 & "...")
    Local $iResult = _CaptureRegion($sTitle, $sFilePath)
    If $iResult = -1 Then
        _LogWrite("ERROR: Could not capture screen.")
    ElseIf $iResult = -2 Then
        _LogWrite("CANCELLED: User cancelled image creation for " & $sFilePath)
    Else
        _LogWrite("Image saved successfully: " & $sFilePath)
        _UpdateSingleImagePreview($iIndex)
    EndIf
    _UpdateStatus("Ready")
EndFunc   ;==>_CreateImageFile

; #FUNCTION# ====================================================================================================================
; Name...........: _SelectAreaOnScreen
; Description....: Manages the process of selecting a screen area and updating the GUI input fields.
; ===============================================================================================================================
Func _SelectAreaOnScreen()
    _UpdateStatus("Preparing to select search area...")
    Local $aCoords = _CaptureRegion("Select an area and release the mouse", "")
    If Not IsArray($aCoords) Then
        _LogWrite("INFO: Area selection cancelled.")
    Else
        GUICtrlSetData($g_idInputLeft, $aCoords[0])
        GUICtrlSetData($g_idInputTop, $aCoords[1])
        GUICtrlSetData($g_idInputRight, $aCoords[2])
        GUICtrlSetData($g_idInputBottom, $aCoords[3])
        _LogWrite("INFO: Search area updated to L:" & $aCoords[0] & " T:" & $aCoords[1] & " R:" & $aCoords[2] & " B:" & $aCoords[3])
    EndIf
    _UpdateStatus("Ready")
EndFunc   ;==>_SelectAreaOnScreen

; #FUNCTION# ====================================================================================================================
; Name...........: _CaptureRegion
; Description....: Creates a transparent GUI to allow the user to select a screen region by dragging the mouse.
; Parameters.....: $sTitle    - The title for the capture window.
;                  $sFilePath - If provided, captures and saves an image. If empty, returns coordinates.
; Return values..: If $sFilePath is provided: 0 on success, -1 on capture error, -2 on user cancel.
;                  If $sFilePath is empty: A 4-element array [Left, Top, Right, Bottom] on success, -2 on user cancel.
; ===============================================================================================================================
Func _CaptureRegion($sTitle, $sFilePath)
    Local $hUserDLL = DllOpen("user32.dll")
    If $hUserDLL = -1 Then Return -1
    Local $hCrossGUI = GUICreate($sTitle, @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOPMOST)
    GUISetBkColor(0x000001)
    WinSetTrans($hCrossGUI, "", 1)
    GUISetState(@SW_SHOW, $hCrossGUI)
    GUISetCursor(3, 1, $hCrossGUI)
    _UpdateStatus("Drag the mouse to select an area. Press ESC to cancel.")
    ToolTip("Drag the mouse to select an area. Press ESC to cancel.", 0, 0)
    While Not _IsPressed("01", $hUserDLL)
        If _IsPressed("1B", $hUserDLL) Then
            ToolTip("")
            GUIDelete($hCrossGUI)
            DllClose($hUserDLL)
            Return -2
        EndIf
        Sleep(20)
    WEnd
    ToolTip("")
    Local $aStartPos = MouseGetPos()
    Local $iX1 = $aStartPos[0], $iY1 = $aStartPos[1]
    Local $hRectGUI
    While _IsPressed("01", $hUserDLL)
        Local $aCurrentPos = MouseGetPos()
        Local $iX2 = $aCurrentPos[0], $iY2 = $aCurrentPos[1]
        If IsHWnd($hRectGUI) Then GUIDelete($hRectGUI)
        Local $iLeft = ($iX1 < $iX2 ? $iX1 : $iX2)
        Local $iTop = ($iY1 < $iY2 ? $iY1 : $iY2)
        Local $iWidth = Abs($iX1 - $iX2)
        Local $iHeight = Abs($iY1 - $iY2)
        $hRectGUI = GUICreate("", $iWidth, $iHeight, $iLeft, $iTop, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST))
        GUISetBkColor(0xFF0000)
        _WinAPI_SetLayeredWindowAttributes($hRectGUI, 0, 100)
        GUISetState(@SW_SHOWNOACTIVATE, $hRectGUI)
        Sleep(10)
    WEnd
    Local $aEndPos = MouseGetPos()
    Local $iX2 = $aEndPos[0], $iY2 = $aEndPos[1]
    GUIDelete($hCrossGUI)
    If IsHWnd($hRectGUI) Then GUIDelete($hRectGUI)
    DllClose($hUserDLL)
    Local $iLeft = ($iX1 < $iX2 ? $iX1 : $iX2)
    Local $iTop = ($iY1 < $iY2 ? $iY1 : $iY2)
    Local $iRight = ($iX1 > $iX2 ? $iX1 : $iX2)
    Local $iBottom = ($iY1 > $iY2 ? $iY1 : $iY2)

    ; If $sFilePath is empty, it's an area selection, not an image capture
    If $sFilePath = "" Then
        Local $aReturn[4] = [$iLeft, $iTop, $iRight, $iBottom]
        Return $aReturn
    EndIf

    Local $aMousePos = MouseGetPos()
    MouseMove(0, 0, 0)
    Sleep(250)
    Local $hBitmap = _ScreenCapture_Capture("", $iLeft, $iTop, $iRight, $iBottom, False)
    If @error Then
        MouseMove($aMousePos[0], $aMousePos[1], 0)
        Return -1
    EndIf
    Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    _GDIPlus_ImageSaveToFile($hImage, $sFilePath)
    _GDIPlus_BitmapDispose($hImage)
    _WinAPI_DeleteObject($hBitmap)
    MouseMove($aMousePos[0], $aMousePos[1], 0)
    Return 0
EndFunc   ;==>_CaptureRegion

; #FUNCTION# ====================================================================================================================
; Name...........: _HighlightFoundArea
; Description....: Creates a temporary, semi-transparent GUI to highlight a found image location.
; Parameters.....: $iX, $iY    - The center coordinates of the area to highlight.
;                  $iColor     - [optional] The color of the highlight rectangle.
;                  $iWidth     - [optional] The width of the highlight rectangle.
;                  $iHeight    - [optional] The height of the highlight rectangle.
; ===============================================================================================================================
Func _HighlightFoundArea($iX, $iY, $iColor = 0xFFFF0000, $iWidth = 50, $iHeight = 50)
    Local $hGUI = GUICreate("", $iWidth, $iHeight, $iX - $iWidth / 2, $iY - $iHeight / 2, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
    GUISetBkColor($iColor)
    _WinAPI_SetLayeredWindowAttributes($hGUI, 0, 128)
    GUISetState(@SW_SHOWNOACTIVATE)
    Sleep(500)
    GUIDelete($hGUI)
EndFunc   ;==>_HighlightFoundArea

; #FUNCTION# ====================================================================================================================
; Name...........: _LogWrite
; Description....: Writes a timestamped message to the activity log and ensures it scrolls to the bottom.
; Parameters.....: $sMessage - The string message to log.
; ===============================================================================================================================
Func _LogWrite($sMessage)
    GUICtrlSetData($g_hLog, _NowTime() & " " & $sMessage & @CRLF, 1)
    _GUICtrlEdit_SetSel(GUICtrlGetHandle($g_hLog), 0x7FFFFFFF, 0x7FFFFFFF)
EndFunc   ;==>_LogWrite

; #FUNCTION# ====================================================================================================================
; Name...........: _LogSearchError
; Description....: Translates an error code from the UDF into a human-readable message and logs it.
; Parameters.....: $iErrorCode - The status code returned by the _ImageSearch* function.
; ===============================================================================================================================
Func _LogSearchError($iErrorCode)
    Switch $iErrorCode
        Case 0
            _LogWrite("    - Not found.")
        Case -1
            _LogWrite("ERROR: DllCall failed. Check if the DLL is corrupted or blocked by antivirus.")
        Case -2
            _LogWrite("ERROR: Invalid format returned from DLL. The UDF could not parse the result.")
        Case -3
            _LogWrite("ERROR: Invalid content returned from DLL. The result string was malformed.")
        Case -11
            _LogWrite("ERROR: The source image file was not found on disk (checked by UDF).")
        Case -12
            _LogWrite("ERROR: Failed to deploy or load the ImageSearch DLL.")
        Case Else
            _LogWrite("ERROR: An internal DLL error occurred. Code: " & $iErrorCode)
    EndSwitch
EndFunc   ;==>_LogSearchError

; #FUNCTION# ====================================================================================================================
; Name...........: _UpdateAllImagePreviews
; Description....: Iterates through all image slots and updates their preview images.
; ===============================================================================================================================
Func _UpdateAllImagePreviews()
    For $i = 0 To $MAX_IMAGES - 1
        _UpdateSingleImagePreview($i)
    Next
EndFunc   ;==>_UpdateAllImagePreviews

; #FUNCTION# ====================================================================================================================
; Name...........: _UpdateSingleImagePreview
; Description....: Updates a single image preview slot. Shows the placeholder if the image doesn't exist.
; Parameters.....: $iIndex - The index of the image slot to update.
; ===============================================================================================================================
Func _UpdateSingleImagePreview($iIndex)
    If FileExists($g_asImagePaths[$iIndex]) Then
        GUICtrlSetImage($g_aidPic[$iIndex], $g_asImagePaths[$iIndex])
    Else
        If FileExists($g_sPlaceholderPath) Then
            GUICtrlSetImage($g_aidPic[$iIndex], $g_sPlaceholderPath)
        Else
            GUICtrlSetImage($g_aidPic[$iIndex], "shell32.dll", 22)
        EndIf
    EndIf
EndFunc   ;==>_UpdateSingleImagePreview

; #FUNCTION# ====================================================================================================================
; Name...........: _SelectAll
; Description....: Checks or unchecks all image target checkboxes.
; Parameters.....: $bState - True to check all, False to uncheck all.
; ===============================================================================================================================
Func _SelectAll($bState)
    Local $iCheckState = $GUI_UNCHECKED
    If $bState Then $iCheckState = $GUI_CHECKED

    For $i = 0 To $MAX_IMAGES - 1
        GUICtrlSetState($g_aidChkSearch[$i], $iCheckState)
    Next
EndFunc   ;==>_SelectAll

; #FUNCTION# ====================================================================================================================
; Name...........: _UpdateStatus
; Description....: Sets the text of the status bar.
; Parameters.....: $sMessage - The message to display.
; ===============================================================================================================================
Func _UpdateStatus($sMessage)
    _GUICtrlStatusBar_SetText($g_hStatusBar, $sMessage)
EndFunc   ;==>_UpdateStatus

; #FUNCTION# ====================================================================================================================
; Name...........: _Exit
; Description....: Exits the script cleanly.
; ===============================================================================================================================
Func _Exit()
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>_Exit

 

Download ImageSearchUDF here:

Dll source code download here:

daovantrong/ImageSearchDLL: A DLL for finding an image on the screen

 

Regards,
 

Posted

thanks

If there is more than one identical shape with the same background in the image, it will only find one in the search results, and often the one defined in the "target image" selected by the mouse.

Posted
4 hours ago, Parsix said:

thanks

If there is more than one identical shape with the same background in the image, it will only find one in the search results, and often the one defined in the "target image" selected by the mouse.

it is distributed as source code, adjust the function call argument to the UDF passed to the dll to get multiple results (by default it will stop searching when it finds the first 1 image)

Regards,
 

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
×
×
  • Create New...