目的,跨进程获取焦点控件的句柄,GetFocus只能获取本进程的焦点控件的句柄,其它进程获得焦点时总是返回0,网上的办法是,运用子类化技术,在目标进程中调用GetFocus并返回,原理是没有问题,关键是当焦点窗口属于其他进程时,GetWindowLong总是返回0,是不是说,SetWindowLong也是不支持跨进程的呢,那VC6得SPY++又是如何实现的呢,请各路高手帮忙想个办法,以下是代码:
Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetFocus Lib "user32" () As Long
Public preProc As Long, Mess As LongPublic Function WinProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
On Error Resume Next
If Msg = Mess Then
    WinProc = GetFocus
Else
    WinProc = CallWindowProc(preProc, hwnd, Msg, wParam, lParam)
End If
End FunctionPublic Function GetFocusEx() As Long
Dim h As Long
h = GetForegroundWindow
preProc = SetWindowLong(h, -4, AddressOf WinProc)
GetFocusEx = SendMessage(h, Mess, 0, 0)
SetWindowLong h, -4, preProc
End FunctionPublic Sub Main()
Mess = RegisterWindowMessage("GetFocusMess")
End Sub