分享

_ControlGetHandleByPos() UDF (v1.0)

 dinghj 2019-04-16

No the reason I was getting the zero return for some controls was that your script did not seem to handle Edit 2 etc they were being renamed as Edit 1

I have modified the script slightly and this seemed to work for me

; ==============================================================================
;
; Function Name:    _ControlGetHandleByPos()
; Description:    Retrieves the internal handle of a control that matches a
;                  given position.
; Parameter(s):  $sTitle -- the title of the window containing the control
;                  $sText  -- the text of the window containing the control
;                  $iX   -- the X coordinate of the control
;                  $IY   -- the Y coordinate of the control
; Requirement(s):   None
; Return Value(s):  On success -- returns the control's handle
;                  On failure -- return and sets @error:
;                                  1 -- could not find window
;                                  2 -- could not find control
; Author(s):        Alex Peters
;
; ==============================================================================

Func _ControlGetHandleByPos($sTitle, $sText, $iX, $iY)

    Local $hWin, $hControl
    Local $iControls, $iLoop, $iUniqueControls
    Local $sClassList, $sClass, $sClassID
    Local $aiControlPos, $avUniqueControls[1][2]

; Determine that the window exists
    $hWin = WinGetHandle($sTitle, $sText)
    If @error Then
        SetError(2)
        Return 0
    EndIf

; Determine the control classes and total number of controls
    $sClassList = WinGetClassList($hWin)
    $iControls = StringLen($sClassList) - StringLen(StringReplace($sClassList, @LF, ""))
    ReDim $avUniqueControls[$iControls][2]

    $iUniqueControls = 0
    While $sClassList
        $sClass = StringLeft($sClassList, StringInStr($sClassList, @LF) - 1)
        $sClassList = StringMid($sClassList, StringLen($sClass) + 2)
        $sClassID = ""
        For $iLoop = 0 To $iUniqueControls - 1
            If $avUniqueControls[$iLoop][0] = $sClass Then;If there is another button then make button 2 loop
                $avUniqueControls[$iLoop][1] = $avUniqueControls[$iLoop][1] + 1;Starts at 0 then increments 1 each time a match is made giving button 2 etc
                $sClassID = $sClass & $avUniqueControls[$iLoop][1];Makes up the correct class ID from above
                ExitLoop
            EndIf
        Next

        If $sClassID = "" Then;This numbers the first controls eg Edit 1
            $avUniqueControls[$iUniqueControls][0] = $sClass
            $avUniqueControls[$iUniqueControls][1] = 1
            $iUniqueControls = $iUniqueControls + 1
            $sClassID = $sClass & "1"
        EndIf
    ; Determine the position of the control in question
        $hControl = ControlGetHandle($hWin, "", $sClassID)
        $aiControlPos = ControlGetPos($hWin, "", $hControl)
        
        If ($aiControlPos[0] = $iX And $aiControlPos[1] = $iY) Then _ 
                Return $hControl
    WEnd

; If we reach this point then no matching control was found
    SetError(1)
    Return 0

EndFunc

The If Not section did not seem to be working for me so I changed it to

If $sClassID = ""
and this seems to work.         

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多