函数说明

WinGetCaretPos

返回顶层窗口的插入符的坐标位置。

WinGetCaretPos ( )

 

参数

 

返回值

成功: 返回一个含有下列信息的数组:
$array[0] = X 坐标
$array[1] = Y 坐标
失败: 把 @error 设为 1。

 

注意

若设置了 CaretCoordMode 使用绝对位置则 WinGetCaretPos 对那些具有多文档界面(MDI)的应用程序获得的坐标值可能是不准确的。请参考下面的示例。注意:有些程序可能会无论光标实际位置在哪都只会返回某固定数值!

 

相关

CaretCoordMode(选项)

 

示例


$a = WinGetCaretPos()
If Not @error Then ToolTip("使用第一种方法获得的位置", $a[0], $a[1])
sleep(2000)

$b = _CaretPos()
If Not @error Then ToolTip("使用第二种方法获得的位置", $b[0], $b[1])
sleep(2000)

; 要获取具有多文档界面的文本编辑器的光标位置,请参考下面这个更可靠的方法。
Func _CaretPos()
    Local $x_adjust =  5
    Local $y_adjust = 40

    Opt("CaretCoordMode", 0)              ;相对位置
    Local $c = WinGetCaretPos()           ;相对位置
    Local $w = WinGetPos("")              ;窗口位置
    Local $f = ControlGetFocus("","")     ;文本区域“句柄”
    Local $e = ControlGetPos("", "", $f)  ;文本区域位置

    Local $t[2]
    If IsArray($c) and IsArray($w) and IsArray($e) Then
        $t[0] = $c[0] + $w[0] + $e[0] + $x_adjust
        $t[1] = $c[1] + $w[1] + $e[1] + $y_adjust
        Return $t     ;光标在屏幕上的绝对位置
    Else
        SetError(1)
    EndIf
EndFunc