AutoIt 无法获取.net CheckBox 控件的状态

本文介绍了一种通过比较Checkbox选中时的颜色变化来判断其状态的新方法,该方法适用于AutoIt脚本中对.NET控件的Checkbox状态判断,避免了使用ControlCommand函数时可能遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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

转载于:https://www.cnblogs.com/zhaojin/archive/2011/12/27/AutoIt.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值