Jump to content

how to draw rectangle round PixelSearch area

Recommended Posts

I am having to manually adjust the coordinates used for a PixelSearch by experimentation and have been wondering if there is some way I can display a rectangle round the area I am searching so I can speed up my manual process which I have to do if the app window is moved for any reason.

My most recent reason for the move was getting a new bigger screen. :)

I have done some searching but not found anything suitable that would only be active when I have set the TestCmd variable to 1 to indicate I am testing the Cmd segment of my script which is an automation of a game to do some resource farming.

Hope someone can help me.

Link to post
Share on other sites

I setup a function to do the screen capture and the control overlay passing the same coordinates as are being used for the pixel search.

The control overlay is showing on the window where I want it in that I can see the title text but there is nothing in the part of the control where I expected the bitmap to appear.

Is it possible to set the background of the rectangle that would contain the bitmap to transparent so the actual window would show through?

I would not need a bitmap then.


The bitmap captured did not contain the area of the window I expected by that I mean not the same as where the control is seen. 

I have managed to manually find the correct offset from the parameter values past to the function and can see in the captured bitmap the part of the window I want to search and see in the control.

It appears to me that the screen capture is using different coordinates (screen?) to the control coordinates (window?) but I am not sure about that and what coordinates the pixel search function is using.

Can you help me with that? 

Link to post
Share on other sites

Ok, changed the script but still a problem with correlation between coordinates of search, capture and control.

The function I am using is coded like this:

Func MarkSearchArea($X1,$Y1,$X2,$Y2)
      $sBMP_Path = @ScriptDir & "\Rect.bmp"
      _DebugOut($sBMP_Path & " " & $X1 & " " & $Y1 & " " & $X2 & " " & $Y2)
      _ScreenCapture_CaptureWnd($sBMP_Path,$WinHandle, $X1-200, $Y1-100, $X2+200, $Y2+100, False)
      $hBitmap_GUI = GUICreate("Search Area", $X2-$X1+1,$Y2-$Y1+10,$X1,$Y1-11,$SS_SIMPLE)
      $hPic = GUICtrlCreatePic($sBMP_Path,0,0,$X2-$X1+1,$Y2-$Y1+1,$SS_WHITEFRAME)

I am passing the same coordinate values to this function as those being used for the PixelSearch

The bitmap shows the area I want is towards the right of the area and below it.

How do I line these things up?

Link to post
Share on other sites

Well, some progress at least. I can now see the saved screen capture in the control client area. I added GUISetBkColor(0xE0FFFF) before the createpic.

I am still playing with the difference between GUI, Capture and Search coordinates but at least I can see what is being captured whilst inside the running script..

Link to post
Share on other sites

Maybe in this example some thoughts to consider

and this is what i use in uiawrappers (see examples section)

; Draw rectangle on screen.
Func _UIA_DrawRect($tLeft, $tRight, $tTop, $tBottom, $color = 0xFF, $PenWidth = 4)
    Local $hDC, $hPen, $obj_orig, $x1, $x2, $y1, $y2
    $x1 = $tLeft
    $x2 = $tRight
    $y1 = $tTop
    $y2 = $tBottom
    $hDC = _WinAPI_GetWindowDC(0) ; DC of entire screen (desktop)
    $hPen = _WinAPI_CreatePen($PS_SOLID, $PenWidth, $color)
    $obj_orig = _WinAPI_SelectObject($hDC, $hPen)

    _WinAPI_DrawLine($hDC, $x1, $y1, $x2, $y1) ; horizontal to right
    _WinAPI_DrawLine($hDC, $x2, $y1, $x2, $y2) ; vertical down on right
    _WinAPI_DrawLine($hDC, $x2, $y2, $x1, $y2) ; horizontal to left right
    _WinAPI_DrawLine($hDC, $x1, $y2, $x1, $y1) ; vertical up on left

    ; clear resources
    _WinAPI_SelectObject($hDC, $obj_orig)
    _WinAPI_ReleaseDC(0, $hDC)
EndFunc   ;==>_UIA_DrawRect


Link to post
Share on other sites

I have resolved one issue which was to do with my understanding of which coordinate system a function was using.

Simply that I needed to read the documentation fully.

Now I have done that and as a side effect of my investigation I now have a mouse locator that I can call and it shows me the mouse location wher I want to see it. Now I can adjust the locations I want things to be clicked and the work on that part is ongoing with no further need for responses to this chat line.

I will mention before closing that I did find an anomaly which is a surprise.

My debug log showed my window being 1536 by 949 but when I took a screenshot via Alt Printscreen the BMP it produced was, according to  my paint.net, 2284 by 1414. 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Dave1
      Hello everyone,
      I have several signals on the screen that need to be monitored while a program is running. These signals are scattered around an image which is maximized on 4  equal resolution screens - the AutoIT Window Info does not recognize any objects on the program meaning that I'm left with the PixelSearch() and PixelCheckSum() functions to monitor the signals:
      The signals are as small as a 5x5 pixel area and their coordinates are known.
      I've been digging around for a while now in this forum about the PixelSearch() and PixelCheckSum()  and found some interesting and useful ideas for the use of them. I also came across some other UDF functions like MultiMon(), FastFind(), TtColXY() and the ImageSearch2015 scripts that might be useful for the final output. I don't know if hovering the mouse by using TtColXY() and output its ToolTip() information onto the log file will be faster than using PixelSearch() and/or PixelCheckSum() in a loop for all signals' coordinates.
      The colours of the signals are below:
      red - 0x00FF00 (opaque red)
      green - 0x00FF00 (opaque green)
      yellow - 0xFFFF00 (opaque yellow)
      black - 0x000000 (opaque black, default colour)
      The desired output is to monitor and record/log the changes and status of each inside a .txt file or a .csv with the below format: 
      Local Machine Time        Signal,      Change,               delta-t
      14:32:07                  Signal1     Green - Yellow         DELTA-t1
      14:34:02                  Signal1     Yellow - Red           DELTA-t1
      14:35:14                  Signal2     Yellow - Red           DELTA-t2
      Below is the code I came up with. 
      #include <AutoItConstants.au3> #include <MsgBoxConstants.au3> #include <misc.au3> #include <Date.au3> #include <Array.au3> #include <File.au3> #include <ScreenCapture.au3> #include <WinAPI.au3> #include <WinAPIHObj.au3> Global $program_name, $program_open, $Wname, $tCurrent Global $button_xy[2] = [150, 175] Global $iniColour, $ColourCheck, $NewCheck Global $Red, $Green, $Blue, $Nil Global $sFilePath = @ScriptDir & "\Signals_status.txt" Global $SignalID[10] = ["Signal_1", "Signal_2", "Signal_3", "Signal_4", "Signal_5", "Signal_6", "Signal_7", "Signal_8", "Signal_9", "Signal_10"] $program_name = "Signals.exe" $Wname = "Training Task 3" $program_open = ShellExecute($program_name, @ScriptDir) WinWait($Wname) $iniColour = "0x" & Hex(PixelGetColor($button_xy[0], $button_xy[1]), 6) ;~ $iniChecksum = "0x" & Hex(PixelChecksum($button_xy[0], $button_xy[0] - 5, $button_xy[1], $button_xy[1] + 5), 6) $ColourCheck = "0x" & Hex(PixelChecksum($button_xy[0], $button_xy[0] - 5, $button_xy[1], $button_xy[1] + 5, 1, $Wname), 6) ConsoleWrite(_ColourID($iniColour) & " // " & _ColourID($ColourCheck) & @CRLF) ;~ $ColourCheck = "0x " & Hex(PixelGetColor($button_xy[0], $button_xy[1]), 6) $iniColour = $ColourCheck ;$iniChecksum = $currentCheck Do ;~ While 1 _WindowOnTop() _Close_Notepad() $NewCheck = "0x" & Hex(PixelChecksum($button_xy[0], $button_xy[0] - 5, $button_xy[1], $button_xy[1] + 5, 1, $Wname), 6) $tCurrent = _NowCalc() $LogFile = FileOpen($sFilePath, 2) FileWriteLine($LogFile, "Local Machine Time " & "Signal, " & "Change, " & "delta-t") ;~ $ColourCheck = "0x" & Hex(PixelChecksum($button_xy[0], $button_xy[0] - 5, $button_xy[1], $button_xy[1] + 5, 1, $Wname), 6) If $ColourCheck <> $NewCheck Then ;If there's a colour change from the current colour $ColourCheck = $NewCheck Local $tChange, $NewCheckID, $ColourCheckID, $sLogMsg $tChange = _NowCalc() ConsoleWrite("Colour changed!" & @CRLF) $ColourCheckID = _ColourID($ColourCheck) $NewCheckID = _ColourID($NewCheck) $iTimeDiffh = _DateDiff('h', $tChange, $tCurrent) ; time difference in hours $iTimeDiffm = _DateDiff('n', $tChange, $tCurrent) ; time difference in minutes $iTimeDiffs = _DateDiff('s', $tChange, $tCurrent) ; time difference in seconds $durationCheckSum = $iTimeDiffh & ":" & $iTimeDiffm & ":" & $iTimeDiffs ; Timestamp of the signal until PixelCheckSum $sLogMsg = " " & $SignalID[0] & " " & $ColourCheckID & " - " & $NewCheckID & " " & $durationCheckSum _FileWriteLog($LogFile, $sLogMsg) ElseIf $ColourCheck = $NewCheck Then Local $Colour_check = _ColourID("0x" & Hex(PixelChecksum($button_xy[0], $button_xy[0] - 5, $button_xy[1], $button_xy[1] + 5, 1, $Wname), 6)) ConsoleWrite($tCurrent & " " & $Colour_check & @CRLF) EndIf FileClose($sFilePath) Until Not ProcessExists($program_name) And Not WinExists($Wname) _IsProgramOpen() Func _Close_Notepad() $notepad_open = ProcessExists("notepad.exe") ? ProcessClose("notepad.exe") : ProcessClose("notepad.exe") $notepad_open = WinActive("[CLASS:Notepad]") ? WinClose("[CLASS:Notepad]") : ProcessClose("notepad.exe") EndFunc ;==>_Close_Notepad Func _ColourID($sColour) $Red = Int("0x" & StringRegExpReplace($sColour, "(..)(..)(..)(..)", "\2")) $Green = Int("0x" & StringRegExpReplace($sColour, "(..)(..)(..)(..)", "\3")) $Blue = Int("0x" & StringRegExpReplace($sColour, "(..)(..)(..)(..)", "\4")) If $Green > $Blue And $Red > $Blue And $Green >= 0xB0 And $Red >= 0xB0 Then $sCol = "Yellow" ElseIf $Blue > 0xE0 And $Green > 0xE0 And $Red > 0xE0 Then $sCol = "White" ElseIf $Blue > 0x50 And $Blue = $Green And $Blue = $Red Then $sCol = "Grey" ElseIf $Red > $Green And $Red > $Blue And $Red > 0x70 Then $sCol = "Red" ElseIf $Green > $Red And $Green >= $Blue And $Green > 0x70 Then $sCol = "Green" ElseIf $Blue > $Red And $Blue > $Green And $Blue > 0x70 Then $sCol = "Blue" Else $sCol = "Nil" EndIf Return $sCol EndFunc ;==>_ColourID Func _WindowOnTop() WinActivate($Wname) WinSetOnTop($Wname, "", $WINDOWS_ONTOP) Opt("MouseCoordMode", 0) EndFunc ;==>_WindowOnTop Func _IsProgramOpen() If Not ProcessExists($program_name) And Not WinExists($Wname) Then Break(1) EndIf EndFunc ;==>_IsProgramOpen When the program is running, the window opens in the centre of the screen however, the PixelChecksum function is not looking at the correct area. In addition:
      1 - I'm not sure about how to put the message into the log file.
      2 - I tested this script but it is not recording the message into the log file.
      3 - Is there other way to calculate the duration in these lines?
      $iTimeDiffh = _DateDiff('h', $tChange, $tCurrent) ; time difference in hours $iTimeDiffm = _DateDiff('n', $tChange, $tCurrent) ; time difference in minutes $iTimeDiffs = _DateDiff('s', $tChange, $tCurrent) ; time difference in seconds $durationCheckSum = $iTimeDiffh & ":" & $iTimeDiffm & ":" & $iTimeDiffs ; Timestamp of the signal until PixelCheckSum $sLogMsg = " " & $SignalID[0] & " " & $ColourCheckID & " - " & $NewCheckID & " " & $durationCheckSum _FileWriteLog($LogFile, $sLogMsg) Thanks in advance!
  • Create New...