专注

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在AutoIt的帮助文档中, 提到可以用 ControlCommand函数判断checkbox是否选中,有个参数叫“IsChecked”, 但在实际的使用中,至少对.net的checkbox控件无效,网上有人给出了解决方法,原文地址:

http://www.autoitscript.com/forum/topic/61295-ischecked-not-working-with-controlcommand/

他自定义了一个方法,根据选中时控件的颜色变化来判断状态,这是一种新的思路。

Func CheckboxIsChecked($strWindowTitle, $strWindowText, $strCheckboxId)
;===============================================================================
; Function Name: CheckboxIsChecked()
;   Description: Determines checkbox state using difference in color luminosity.
;               A pixel within the checkmark, which significantly changes its
;               luminosity based on the IsChecked value, is compared to a pixel
;               that has luminosity that's relatively unaffected by IsChecked 
;               state. The absolute difference can accommodate a variety of
;               user color schemes.
;               The location of the two pixels is relative to the upper left 
;               corner of the control. These pixels were selected to provide 
;               large contrast in both WinXP and Vista, with the checkbox both
;               enabled and disabled. Other versions of Windows may require
;               other pixels (e.g., older Windows uses an 'x' rather than tick)
;               or a different threshold; use a Select Case @OSVersion to 
;               choose the best pixel pairs.
;               Procedure:
;                 1. get starting position (upper left corner of checkbox)
;                    which is defined as (0,0)
;                 2. get colors of two pixels: checkmark at (8,6) and baseline
;                    at (8,11)
;                 3. place in RGB array to facilitate Min/Max calc
;                 4. calculate luminousity of each pixel (Max+Min)/2
;                 5. if luminousity difference > threshold then IsChecked=True
;        Entry: $strWindowTitle is control's owner window
;               $strWindowText is window text
;               $strCheckboxId is ID of control to be examined
;      Returns: 1 if IsChecked=True, otherwise 0
;      Created: 2008-01-09
;       Author: Ed Allwein, Active Sensing, Inc.  www.BuyPLM.com
;===============================================================================
    Local Enum $rgblRed=0, $rgblGreen=1, $rgblBlue=2, $rgblLum=3

    Const $maskRed = 0x0FF0000
    Const $maskGreen = 0x0FF00
    Const $maskBlue = 0x0FF
    Const $intThreshold = 26; good UI practice suggests at least 10% diff on scale of 0-255
    
    Local $rectControl[4]
    $rectControl = ControlGetPos($strWindowTitle, $strWindowText, $strCheckboxId)

    Local $posX = 0 ; X offset from upper left corner of checkbox
    Local $posY = 0 ; Y offset from upper left corner of checkbox

; baseline pixel luminosity should be relatively stable regardless of checkmark luminosity
    $posX = 8   ; baseline X offset
    $posY = 11  ; baseline Y offset
    Local $rgblBaseline[4]
    $rgblBaseline = GetColorAsRGBL($rectControl[0]+$posX,$rectControl[1]+$posY)

; checkmark pixel should alternate between 2 luminosities (one of which is similar to baseline)
    $posX = 8   ; checkmark X offset
    $posY = 6   ; checkmark Y offset
    Local $rgblCheckmark[4]
    $rgblCheckmark = GetColorAsRGBL($rectControl[0]+$posX,$rectControl[1]+$posY)

    If Abs($rgblBaseline[$rgblLum] - $rgblCheckmark[$rgblLum]) > $intThreshold Then
        Return 1    ;notable luminousity difference between checkmark and baseline
    Else
        Return 0
    EndIf
 EndFunc
 
 Func GetColorAsRGBL($posX,$posY)
;===============================================================================
; Function Name: GetColorAsRGBL()
;   Description: returns an array containing (R,G,B,Lum) values. Lum is useful
;               for seeking relative intensity change regardless of RGB value
;        Entry: $posX,$posY are the (x,y) coordinates of the pixel
;         Exit: $colorRGBL is (R,G,B,Lum) array of integers in range of 0-255
;      Created: 2008-01-09
;       Author: Ed Allwein, Active Sensing, Inc.  www.BuyPLM.com
;===============================================================================
    Local Enum $rgblRed=0, $rgblGreen=1, $rgblBlue=2, $rgblLum=3
    Local Const $maskRed = 0x0FF0000
    Local Const $maskGreen = 0x0FF00
    Local Const $maskBlue = 0x0FF
    
    Local $colorRGB[3]  ; Lum is omitted during Min/Max tests 
    Local $colorRGBL[4]
    Local $intColorValue = 0

    $intColorValue = PixelGetColor($posX,$posY) 
    $colorRGB[$rgblRed] = BitShift(BitAND($intColorValue, $maskRed),16)
    $colorRGBL[$rgblRed] = $colorRGB[$rgblRed]
    $colorRGB[$rgblGreen] = BitShift(BitAND($intColorValue, $maskGreen),8)
    $colorRGBL[$rgblGreen] = $colorRGB[$rgblGreen]
    $colorRGB[$rgblBlue] = BitAND($intColorValue, $maskBlue)
    $colorRGBL[$rgblBlue] = $colorRGB[$rgblBlue]
; calculate luminosity of pixel
    $colorRGBL[$rgblLum] = Round((_ArrayMax($colorRGB, 1) + _ArrayMin($colorRGB, 1)) / 2)

    Return $colorRGBL
EndFunc
posted on 2011-12-27 16:49  中金黄金  阅读(1133)  评论(0编辑  收藏  举报