mark 获取焦点坐标
全局代码
#Region "得到光标在屏幕上的位置"
<DllImport("user32")> _
Public Function GetCaretPos(ByRef lpPoint As Point) As Boolean
End Function
<DllImport("user32.dll")> _
Public Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll")> _
Public Function GetFocus() As IntPtr
End Function
<DllImport("user32.dll")> _
Public Function AttachThreadInput(idAttach As IntPtr, idAttachTo As IntPtr, fAttach As Integer) As IntPtr
End Function
<DllImport("user32.dll")> _
Public Function GetWindowThreadProcessId(hWnd As IntPtr, ProcessId As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll")> _
Public Function GetCurrentThreadId() As IntPtr
End Function
<DllImport("user32.dll")> _
Public Sub ClientToScreen(hWnd As IntPtr, ByRef p As Point)
End Sub
Public Function CaretPos() As Point
Dim ptr As IntPtr = GetForegroundWindow()
Dim p As New Point()
'得到Caret在屏幕上的位置
If ptr.ToInt32() <> 0 Then
Dim targetThreadID As IntPtr = GetWindowThreadProcessId(ptr, IntPtr.Zero)
Dim localThreadID As IntPtr = GetCurrentThreadId()
'If localThreadID <> targetThreadID Then
AttachThreadInput(localThreadID, targetThreadID, 1)
ptr = GetFocus()
If ptr.ToInt32() <> 0 Then
GetCaretPos(p)
ClientToScreen(ptr, p)
End If
AttachThreadInput(localThreadID, targetThreadID, 0)
'End If
End If
Return p
End Function
#End Region
调用代码
Dim p = CaretPos()
msgbox(p.x & " " & p.y)