[已解决]请教如何获取光标所在的坐标
本帖最后由 kxing 于 2011-5-17 18:01 编辑如题,如何返回光标当前所在的坐标呢?
多谢! 本帖最后由 boyhong 于 2011-5-17 08:15 编辑
$pos = MouseGetPos()
MsgBox(0, "鼠标坐标 X,Y:", $pos & "," & $pos) 光标,不是鼠标。。。 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) #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 + 120 + $caretpos, $pos + 32 + $caretpos + 21 * 2, "出错", 3, 3)
EndIf
EndIf
EndFunc ;==>MY_WM_COMMAND
Func _Caret_GetPos()
Local $POINT = DllStructCreate("long;long")
Local $Return
DllCall("User32.dll", "int", "GetCaretPos", "ptr", DllStructGetPtr($POINT))
$Return = DllStructGetData($POINT, 1)
$Return = DllStructGetData($POINT, 2)
Return $Return
EndFunc ;==>_Caret_GetPos 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&@crlf&"插入符坐标Y="&$a, $a, $a)
EndIf
sleep(100)
Next 回复 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&@crlf&"插入符坐标Y="&$a, $a, $a)
Sleep(3000)
Exit
EndIf
Sleep(100)
Next
MsgBox(48, "tips", "找不到光标坐标!")
Exit
Func fn_TestCarePos()
Local $aResult
Local $Point = DllStructCreate("int var1;int var2")
DllCall("user32.dll", "int", "GetCaretPos", "ptr", DllStructGetPtr($Point))
$aResult = DllStructGetData($Point, 1)
$aResult = DllStructGetData($Point, 2)
If $aResult = 0 And $aResult = 0 Then Return SetError(1, 0, 0)
Return $aResult
EndFunc
这个似乎不适合,我是用在listview控件里面的。
目的是要获取光标选中的item,从而获取到该item的坐标。 GetCaretPos是取本线程中的光标位置,不能取其它进程中的光标。 回复 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&@crlf&"y="&$info)
EndFunc
func _exit()
Exit
EndFunc
多谢多谢!
页:
[1]