Jump to content

Capturing state of Checkbox using GetPixelColor


Recommended Posts

I am new to autoit, Need help in debugging the code written by another QA to understand and make it work as expected
The code tries to find the state of checkbox inside a listbox control named "Board Meal type allowed" (Attached screenshot) and return output accordingly. The listbox has 2 checkboxes Period 1mt and period 2mt.

The checkbox controls cannot be identifed directly and a function Checkcheckbox is written to identify the checkbox color using coordinates to identify the position and its Pixel Color.
Inside the function, the postion of the listbox is captured in $bPos and position of the checkbox is captured in $aPos variable
$x variable adds x cordinates of $aPos and $bPos and + 7 to it
$Y variable adds y cordinates of $aPos and $bPos and + 10 to it
Finally PixelGetColor($X,$Y) is fetched and its value is compared with decimal value 16777215 to identify the checkbox state

Since i am new i have tough time understanding below points in the code

1. Can anybody help in understanding the logic on how $x and $y variable is arrived and what possibly the reason value 7 and 10 are added before using PixelGetColor in CheckCheckBoxFunction
2. is there anyway i can get the coordinates using AutoIt window info from sciTE so that i can confirm the PixelGetColor($X,$Y) exactly points to the checkbox
3. The code always returns value PixelGetColor as 15790320 no matter the checkbox is checked or unchecked causing the script to return value "[CheckPosOptionGene"ralPosOptions] Board Meal Type - "& $parameterFieldValue &" not as expected
is it because the PixelGetColor($X,$Y) is pointing to someother place rather than the checkbox element.

 

I am kind of stuck here and any clue on above 3 points would be much helpful

 

 

Func CheckPosOptionGeneralPosOptions($parameterFieldName, $parameterFieldValue, $parameterFieldValueTwo)

    $Result = 1

    Switch $parameterFieldName

        Case "Board Meal Type Allowed"
            $handle = ControlGetHandle($TitleBBTSMain,"","[CLASS:TCheckListBox; INSTANCE:1]")
            ConsoleWrite("Board Meal Type Allowed - ControlGetHandle value : " & $handle & @CRLF)

            $Check = CheckCheckBoxList($handle,$parameterFieldValue)
            ConsoleWrite("Board Meal Type Allowed -parameterFieldValue variable value : " & $parameterFieldValue & @CRLF)
            ConsoleWrite("Board Meal Type Allowed -CheckCheckBoxList value : " & $Check & @CRLF  & @CRLF)

            If $Check <> $parameterFieldValue2 Then
                Return "[CheckPosOptionGeneralPosOptions] Board Meal Type - "& $parameterFieldValue &" Checkbox state not as expected"
            EndIf
            Sleep($SleepSmall)
                ControlClick($TitleBBTSMain,"","[CLASS:TbbDBEdit; INSTANCE:1]")
                Sleep($SleepSmall)
                Send("^a")
                Sleep($SleepSmall)
                Send("10000")

        Case Else
            Return  "-EditPosAccountManagementCenter Field for changing not found"
    EndSwitch
    Return $Result

EndFunc   ;==>EditPosAccountManagementCenter


Func CheckCheckBoxList($handle,$parameterItemName)

    $Select = _GUICtrlListBox_FindString($handle,$parameterItemName)

    If $Select < 0 Then
        Return "'" & $parameterItemName & "' not found in CheckBoxList"
    EndIf

    _GUICtrlListBox_SelectString($handle,$parameterItemName)

    $aPos = _GUICtrlListBox_GetItemRect($handle,$Select)

    $bPos = WinGetPos($handle)

    ConsoleWrite("$aPos[0] value : " & $aPos[0] & @CRLF & "$bPos[0] Value : " & $bPos[0] & @CRLF)
    ConsoleWrite("$aPos[1] value : " & $aPos[1] & @CRLF & "$bPos[1] Value : " & $bPos[1] & @CRLF)

    $X = $bPos[0] + $aPos[0]+7
    $Y= $bPos[1] + $aPos[1]+10
    ConsoleWrite("$x value : " & $X & @CRLF & "$y Value : " & $Y & @CRLF)

    $Color = PixelGetColor($X,$Y)
    ConsoleWrite("$Color value : " & $Color & @CRLF)

    If $Color <> 16777215 Then
        Return 1
    Else
        Return 0
    EndIf

EndFunc



 $test = CheckPosOptionGeneralPosOptions("Board Meal Type Allowed","Period 1 mt",0)
 ConsoleWrite("$test:" & $test & @CRLF )

$test2 = CheckPosOptionGeneralPosOptions("Board Meal Type Allowed","Period 2 mt",1)
 ConsoleWrite("$test2:" & $test2 & @CRLF )

Output of the above code

Board Meal Type Allowed - ControlGetHandle value : 0x006804D4
$aPos[0] value : 0
$bPos[0] Value : 288
$aPos[1] value : 0
$bPos[1] Value : 480
$x value : 295
$y Value : 490
$Color value : 15790320
Board Meal Type Allowed -parameterFieldValue variable value : period 1 mt
Board Meal Type Allowed -CheckCheckBoxList value : 1

 $test : CheckPosOptionGeneralPosOptions] Board Meal Type - Period 1 mt Checkbox state not as expected

Board Meal Type Allowed - ControlGetHandle value : 0x006804D4
$aPos[0] value : 0
$bPos[0] Value : 288
$aPos[1] value : 13
$bPos[1] Value : 480
$x value : 295
$y Value : 503
$Color value : 15790320
Board Meal Type Allowed -parameterFieldValue variable value : period 2 mt
Board Meal Type Allowed -CheckCheckBoxList value : 1

 $test2 : [CheckPosOptionGeneralPosOptions] Board Meal Type - Period 2 mt Checkbox state not as expected

Expected output

 $test : 0 
 $test2 : 1 

 

image.png.a3d1a6ab29cfc6a09f866b1d0f654e79.png

Edited by Press
Link to comment
Share on other sites

1- script is checking for white (16777215).  If it is white it means the box is unchecked, otherwise it is checked.  It adds 7 and 10 to look at the bottom of the check mark.

2- you can use au3info.exe tool to check the exactness of the coordinates. Set menu Options/Coord Mode to the Opt ("PixelCoordMode")  of your script and unfreeze the tool,  go to mouse tab, click on the window, move your mouse, it will show coordinates.

3- Yes, probably it is looking at the wrong place maybe because opt may not be set correctly. 15790320 is 0xF0F0F0 so kind of greyish.

Edited by Nine
Link to comment
Share on other sites

Hi Nine,

Thanks a lot for your response. Bear with my question as i a new.

Can you explain a little bit on how to set Opt("PixelCoordMode", 1) set in Script ?

If i add the above line in CheckCheckBox function , then run the script, how to use au3info.exe tool mouse tab to find the coordinates? or its something that i need to set in au3info.exe tool  options menu and open the application to figure out the mouse coordinates? 

Edited by Press
Link to comment
Share on other sites

Hi Nine,

I Tried adding the line  Opt("PixelCoordMode", )  in CheckCheckbox Function and gave a run

Trial 1 -  set Opt("PixelCoordMode", 1returns same color value -  15790320 [ greyish for all checkboxes even if checked or unchecked]

Trial 2 -  set Opt("PixelCoordMode", 0returns color value as shown below

Period 1 mt checkbox

Color value : 16777215 [ white as expected since its unchecked]

Coordinates $x value : 295 $y Value : 490

 Period 2 mt checkbox

Color value : 15790320 [ greyish as expected since its checked]

Coordinates $x value : 295 $y Value : 503

Period 3 mt checkbox

Color value : 15658734 [ greyish as expected since its checked]

Coordinates $x value : 295 $y Value : 516

Period 4/ 5 mt  and Sick Tray / Standard checkboxes

Color value : 16777215 [ white not expected since its checked]

Tried as per your suggestion to locate the coordinates through au3info and seems the coordinates for all checkboxes used were correct but still the script fails due to mismatch value in color code returned by   

Coordinates located by au3info  for Period 1 mt checkbox is shown below

The color always point as 0xFFFFFF for all checkboxes but  PixelGetColor returns diff values using opt PixelCoordMode 0 or 1 values for the same coordinates

Capture2.PNG.bb17ca19848fa8ed888833c98a93eaab.PNG

Edited by Press
Link to comment
Share on other sites

I cannot debug your script since I do not have access to the based application.  I would suggest you get familiar first with opt pixel coord function and PixelGetColor using your own GUI.  Since it can get you afraid starting a whole new GUI, I will help you out.  But you will need to acquire some skills to complete my example.  Once you have played with the example along with au3info.exe tool, I am pretty sure you will be able to solve your initial issue.  Remember, read carefully help file to understand the various flavor of each function,  Help file is your best friend, not me ;)

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>

;Opt("PixelCoordMode", ??)
Opt("MustDeclareVars", 1)

Global $hGUI = GUICreate("My Test GUI", 500, 500)
Global $iLw1 = GUICtrlCreateListView("List", 10, 10, 480, 250, Default, $LVS_EX_CHECKBOXES)

For $i = 0 To 9
  GUICtrlCreateListViewItem("Item " & $i, $iLw1)
Next
_GUICtrlListView_SetItemChecked($iLw1, 1)
_GUICtrlListView_SetItemChecked($iLw1, 3)
_GUICtrlListView_SetItemChecked($iLw1, 5)
_GUICtrlListView_SetItemChecked($iLw1, 9)
_GUICtrlListView_SetColumnWidth($iLw1, 0, $LVSCW_AUTOSIZE)

GUISetState()

IsCheckedPixel()

While 1
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
  EndSwitch
WEnd

Func IsCheckedPixel()
  ;Const $nCheckColor = 0x??????
  ;Const $x = ??, $y = ??
  Local $tRect
  For $i = 0 To 9
    $tRect = _GUICtrlListView_GetItemRectEx ($iLw1, $i, 0)
    ConsoleWrite ($tRect.left & "/" & $tRect.top & "/" & $tRect.right & "/" & $tRect.bottom & @CRLF)
    ;ConsoleWrite ("isChecked = " & Hex(PixelGetColor ($tRect.left+$x, $tRect.top+$y)) & @CRLF)
  Next
EndFunc   ;==>IsCheckedPixel

Now you will have to find the various value required to complete the example.  All the lines containing a value for you to find is commented out.

Spend some times to understand how it works, how au3info tool works, put it altogether.

Happy learning.

Link to comment
Share on other sites

stop.

First - DO NOT EVER use pixel searching to check unless you have no choice. Always try to hook directly into the control. Pixel searching is the WORST way to automate for it is usually unstable. Resizing a window, a change in the graphics, a change in the software you are automating - this can all break your script.

When using the info tool, click on the summary tab and look at everything it is sending you. Post that back here so if wee can see if you can hook in the old way. If the tool can't read it, then we need to use this:

 

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...