Jump to content

Pixelsearch


SlimShady
 Share

Recommended Posts

While I was making a Tetris clone (currently I named it MyTetris), I stumbled on a problem with PixelSearch.

After some tests I know more of the return values of PixelSearch and ControlGetPos.

  • Test1:

    I made a test script that shows the position and the size of a control using ControlGetPos with a titlebar

  • I added a button
  • I put the info of the control ControlGetPos found, in a message box

    Test2: same as Test1, now without a titlebar.

    Results:

    The info the function returned, matches with the size and positions I gave to the control.

    Test3:

    I made a test script that shows where it matches a certain color using PixelSearch

  • I added an empty label
  • gave the label a white background color
  • I put the info PixelSearch returned in a message box
  • with a title bar

    Test4:

    Same as Test3, now without a titlebar

    Results:

    PixelSearch returned different positions of the label/white color

I don't know if something is not working properly or maybe this is just the way Windows works.

Before I started testing I was disappointed that in my original script, the PixelSearch function didn't work well.

Because it was the first time I started using the function.

For your information, here's the PixelSearch test script.

#include <GUIConstants.au3>
AutoItSetOption("TrayIconHide", 1)
AutoItSetOption("PixelCoordMode", 0)
AutoItSetOption("ExpandVarStrings", 1)
AutoItSetOption("WinTitleMatchMode", 4)

;Initialize variables
Global $style1
Global $IniFile
Global $GUIWidth
Global $GUIHeight

$GUIWidth = 250
$GUIHeight = 250
$IniFile = @ScriptDir & "\" & StringTrimRight(@ScriptName, 3) & "ini"

;Only a close button:
;$style1 = BitOR($WS_POPUP, $WS_CAPTION, $WS_SYSMENU)
;GUICreate("New GUI", $GUIWidth, $GUIHeight, -1, -1, $style1)

;GUI with title bar
$Window = GUICreate("New GUI", $GUIWidth, $GUIHeight)

;GUI without titlebar
;$Window = GUICreate("New GUI", $GUIWidth, $GUIHeight, -1, -1, $WS_POPUP)

If @error Then
   MsgBox(16, "AutoIt Error", "Window could not be created!")
   ConsoleWrite("---------- AutoIt Error ----------" & @LF)
   ConsoleWrite('"Window could not be created!"')
   Exit 1
EndIf

$Quit_btn = GUICtrlCreateButton("Test", 0, 0, 70, 20)
$Test_label = GUICtrlCreateLabel("", 100, 100, 100, 10)
   GUICtrlSetBKColor(-1, 0xffffff)

GUISetState()

Global $Pressed = 0
While 1
   $Msg = GUIGetMsg()
   Select
   
   Case $msg = $GUI_EVENT_CLOSE
      GUIDelete()
      Exit

   Case $msg = $Quit_btn
      If $Pressed = 0 Then
         $Pressed = 1
         GUICtrlSetData($Quit_btn, "Quit")
         TestThis()
      ElseIf $Pressed = 1 Then
         GUIDelete()
         Exit
      EndIf

   EndSelect

WEnd

Func TestThis()
   $Col = PixelSearch(25, 25, 250, 150, 0xffffff)
   If IsArray($Col) Then
      $x = $Col[0]
      $y = $Col[1]
      MsgBox(0, "Test", "Matched the white color at:@CRLF@x: $x$ y: $y$")
   Else
      MsgBox(0, "Test", "White color is not found!")
   EndIf
EndFunc
Link to comment
Share on other sites

PixelSearch() returns Window coordinates. These are relative to the upper left corner of the window, titlebar or not. ControlGetPos() returns client coordinates. These always start in the upper left corner of the clieant area. This area is the area just inside the border and below the titlebar (If present).

Link to comment
Share on other sites

I came to that conclusion. I'm using in my script a combination of ControlGetPos and PixelSearch.

So I had to adjust my script a little.

The difference between the two are:

3 pixels from the left and 22 from the top.

<{POST_SNAPBACK}>

I would strongly discourage you from hard-coding those values. It is best to calculate those values at run-time because a different Windows theme may change those sizes. I believe Larry explained how to calculate those numbers in a thread not that long ago.
Link to comment
Share on other sites

  • Administrators

I would strongly discourage you from hard-coding those values.  It is best to calculate those values at run-time because a different Windows theme may change those sizes.  I believe Larry explained how to calculate those numbers in a thread not that long ago.

As I think I said somewhere else I think I'm going to go through all the functions that use/get window positions and add a new mode that returns client area coords.
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...