找回密码
 加入
搜索
查看: 2984|回复: 10

[AU3基础] [已解决]请教如何获取光标所在的坐标

  [复制链接]
发表于 2011-5-17 07:52:03 | 显示全部楼层 |阅读模式
本帖最后由 kxing 于 2011-5-17 18:01 编辑

如题,如何返回光标当前所在的坐标呢?
多谢!
发表于 2011-5-17 08:14:29 | 显示全部楼层
本帖最后由 boyhong 于 2011-5-17 08:15 编辑

$pos = MouseGetPos()
MsgBox(0, "鼠标坐标 X,Y:", $pos[0] & "," & $pos[1])
 楼主| 发表于 2011-5-17 09:11:46 | 显示全部楼层
光标,不是鼠标。。。
发表于 2011-5-17 10:30:12 | 显示全部楼层
API GetCaretPos 能获取光标顶部坐标.

但是我试了几次都没调用成功.借贴问一下以下代码错在哪里了
If Not WinExists("无标题") Then Run("notepad.exe")
WinWait("无标题")
WinActivate("无标题")

Local $Point = DllStructCreate("int var1;int var2")
Local $ptr   = DllStructGetPtr($Point)
DllCall("user32.dll", "int", "GetCaretPos", "ptr", $ptr)
If @error Then MsgBox(0, "", @error)

ToolTip(2&@CRLF& DllStructGetData($Point, 1) &@CRLF& DllStructGetData($Point, 2), 0, 0)
Sleep(3000)
发表于 2011-5-17 11:36:14 | 显示全部楼层
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#Region ### START Koda GUI section ### Form=c:\documents and settings\administrator\my documents\form1.kxf
$Form1 = GUICreate("test", 409, 260, 434, 170)
$Label1 = GUICtrlCreateLabel("X:", 40, 32, 86, 17)
$Input1 = GUICtrlCreateInput("", 120, 32, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
 
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
 
        EndSwitch
WEnd
 
 
 
Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
        Local Const $EN_CHANGE = 0x300
 
        Local $nNotifyCode = BitShift($wParam, 16)
        Local $nID = BitAND($wParam, 0xFFFF)
        Local $hCtrl = $lParam
        If $nNotifyCode = $EN_CHANGE Then
                If GUICtrlRead($Input1) > 169 Then
                        $caretpos = _Caret_GetPos()
                                                $pos=WinGetPos($Form1)
                        ToolTip("输入数据太大", $pos[0] + 120 + $caretpos[0], $pos[1] + 32 + $caretpos[1] + 21 * 2, "出错", 3, 3)
                EndIf
        EndIf
EndFunc   ;==>MY_WM_COMMAND
 
Func _Caret_GetPos()
        Local $POINT = DllStructCreate("long;long")
        Local $Return[2]        
        DllCall("User32.dll", "int", "GetCaretPos", "ptr", DllStructGetPtr($POINT))
        $Return[0] = DllStructGetData($POINT, 1)
        $Return[1] = DllStructGetData($POINT, 2)
        Return $Return
EndFunc   ;==>_Caret_GetPos
发表于 2011-5-17 11:43:00 | 显示全部楼层
If Not WinExists("无标题") Then Run("notepad.exe")
WinWait("无标题")
WinActivate("无标题")
send("Test CarePos")
for $i=1 to 100
 $a = WinGetCaretPos()
If Not @error Then         
        ToolTip("插入符坐标X="&$a[0]&@crlf&"插入符坐标Y="&$a[1], $a[0], $a[1])
EndIf
sleep(100)
Next
发表于 2011-5-17 12:06:35 | 显示全部楼层
回复 6# 3mile

请问 GetCaretPos 是否只能用于查找自身程序窗口的光标坐标?
If Not WinExists("无标题") Then Run("notepad.exe")
WinWait("无标题")
WinActivate("无标题")
Send("Test GetCarePos")
for $i=1 to 100
        $a = fn_TestCarePos()
        If Not @error Then
                ToolTip("插入符坐标X="&$a[0]&@crlf&"插入符坐标Y="&$a[1], $a[0], $a[1])
                Sleep(3000)
                Exit
        EndIf
        Sleep(100)
Next
MsgBox(48, "tips", "找不到光标坐标!")
Exit

Func fn_TestCarePos()
        Local $aResult[2]
        Local $Point = DllStructCreate("int var1;int var2")
        DllCall("user32.dll", "int", "GetCaretPos", "ptr", DllStructGetPtr($Point))
        $aResult[0] = DllStructGetData($Point, 1)
        $aResult[1] = DllStructGetData($Point, 2)
        
        If $aResult[0] = 0 And $aResult[1] = 0 Then Return SetError(1, 0, 0)
        Return $aResult
EndFunc
 楼主| 发表于 2011-5-17 12:42:13 | 显示全部楼层
这个似乎不适合,我是用在listview控件里面的。
目的是要获取光标选中的item,从而获取到该item的坐标。
发表于 2011-5-17 12:43:54 | 显示全部楼层
GetCaretPos是取本线程中的光标位置,不能取其它进程中的光标。
发表于 2011-5-17 12:54:43 | 显示全部楼层
回复 8# kxing
但GetGUIThreadInfo可以取得.
#include <winapiex.au3>
HotKeySet("ESC","_exit")
Global $PID
If Not WinExists("无标题") Then Run("notepad.exe")
WinWait("无标题")
WinActivate("无标题")
Send("Test GetCarePos")
AdlibRegister("care")

While 1
        Sleep(100)
WEnd

Func care()
        $hWnd = WinGetHandle('无标题')
        $Info = _WinAPI_GetGUIThreadInfo(_WinAPI_GetWindowThreadProcessId($hWnd, $PID))
        ToolTip("x="&$info[7]&@crlf&"y="&$info[8])
EndFunc

func _exit()
        Exit
EndFunc
 楼主| 发表于 2011-5-17 18:01:30 | 显示全部楼层
多谢多谢!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-10-2 03:28 , Processed in 0.081612 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表