'-----------声明------------- Private Declare Function GetCaretPos Lib "user32" (lpPoint As POINTAPI) As Long Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long As RECT) As Long Private Declare Function GetFocus Lib "user32" () As LongPrivate Type POINTAPI x As Long y As Long End Type '---------代码----------- Dim K As POINTAPI, hwnd As Long GetCaretPos K hwnd = GetFocus() ClientToScreen hwnd, K 'GetWindowRect hwnd, R 'K.x = R.Left + K.x 'K.y = R.Top + K.y MsgBox K.x
我晕,多了一个“As RECT) As Long”忘记删掉了。 :D'-----------声明------------- Private Declare Function GetCaretPos Lib "user32" (lpPoint As POINTAPI) As Long Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long Private Declare Function GetFocus Lib "user32" () As LongPrivate Type POINTAPI x As Long y As Long End Type '---------代码----------- Dim K As POINTAPI, hwnd As Long GetCaretPos K hwnd = GetFocus() ClientToScreen hwnd, K 'GetWindowRect hwnd, R 'K.x = R.Left + K.x 'K.y = R.Top + K.y MsgBox K.x
不过GetFocus不能获取外部进程的窗口句柄啊,如果是光标在本程序外怎么办呢?
'------------声明------------- Private Declare Function GetCaretPos Lib "user32" (lpPoint As POINTAPI) As Long Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long Private Declare Function GetFocus Lib "user32" () As Long Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function AttachThreadInput Lib "user32.dll" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long Private Declare Function GetForegroundWindow Lib "user32.dll" () As LongPrivate Type POINTAPI x As Long y As Long End Type '---------代码----------- Dim K As POINTAPI, hwnd As Long, retVal As Long Dim idAttach As Long GetCaretPos K hwnd = GetForegroundWindow() retVal = GetWindowThreadProcessId(hwnd, idAttach) If idAttach = App.ThreadID Then hwnd = GetFocus() Else If AttachThreadInput(App.ThreadID, idAttach, 1) <> 0 Then hwnd = GetFocus() AttachThreadInput App.ThreadID, idAttach, 0 End If End If ClientToScreen hwnd, K Label1 = K.x
我晕,Sorry,这个不行。我看了一下MSDN,发现自己把AttachThreadInput函数搞错了改成这个试试'------------声明------------- Private Declare Function GetCaretPos Lib "user32" (lpPoint As POINTAPI) As Long Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long Private Declare Function GetFocus Lib "user32" () As Long Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function AttachThreadInput Lib "user32.dll" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long Private Declare Function GetForegroundWindow Lib "user32.dll" () As LongPrivate Type POINTAPI x As Long y As Long End Type '---------代码----------- Dim K As POINTAPI, idAttach As Long, hWnd As Long idAttach = GetWindowThreadProcessId(GetForegroundWindow(), 0&) If idAttach <> App.ThreadID Then AttachThreadInput idAttach, App.ThreadID, True hWnd = GetFocus() GetCaretPos K ClientToScreen hWnd, K AttachThreadInput Thread2, Thread1, False Msgbox K.x试试看,应该OK了 :D
Private Declare Function GetCaretPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
As RECT) As Long
Private Declare Function GetFocus Lib "user32" () As LongPrivate Type POINTAPI
x As Long
y As Long
End Type
'---------代码-----------
Dim K As POINTAPI, hwnd As Long
GetCaretPos K
hwnd = GetFocus()
ClientToScreen hwnd, K
'GetWindowRect hwnd, R
'K.x = R.Left + K.x
'K.y = R.Top + K.y
MsgBox K.x
Private Declare Function GetCaretPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function GetFocus Lib "user32" () As LongPrivate Type POINTAPI
x As Long
y As Long
End Type
'---------代码-----------
Dim K As POINTAPI, hwnd As Long
GetCaretPos K
hwnd = GetFocus()
ClientToScreen hwnd, K
'GetWindowRect hwnd, R
'K.x = R.Left + K.x
'K.y = R.Top + K.y
MsgBox K.x
Private Declare Function GetCaretPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AttachThreadInput Lib "user32.dll" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32.dll" () As LongPrivate Type POINTAPI
x As Long
y As Long
End Type
'---------代码-----------
Dim K As POINTAPI, hwnd As Long, retVal As Long
Dim idAttach As Long
GetCaretPos K
hwnd = GetForegroundWindow()
retVal = GetWindowThreadProcessId(hwnd, idAttach)
If idAttach = App.ThreadID Then
hwnd = GetFocus()
Else
If AttachThreadInput(App.ThreadID, idAttach, 1) <> 0 Then
hwnd = GetFocus()
AttachThreadInput App.ThreadID, idAttach, 0
End If
End If
ClientToScreen hwnd, K
Label1 = K.x
Private Declare Function GetCaretPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AttachThreadInput Lib "user32.dll" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32.dll" () As LongPrivate Type POINTAPI
x As Long
y As Long
End Type
'---------代码-----------
Dim K As POINTAPI, idAttach As Long, hWnd As Long
idAttach = GetWindowThreadProcessId(GetForegroundWindow(), 0&)
If idAttach <> App.ThreadID Then AttachThreadInput idAttach, App.ThreadID, True
hWnd = GetFocus()
GetCaretPos K
ClientToScreen hWnd, K
AttachThreadInput Thread2, Thread1, False
Msgbox K.x试试看,应该OK了 :D
改成AttachThreadInput idAttach, App.ThreadID, False我晕,我把Option Explicit删掉掉了,所以没发现……