噢, 上面的1997是我测试用的你可以删除
GetWindowText是少数几个可以跨进程的函数之1
多个test可以分别
Private Sub Form_Load()
old = SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf newfunc)
old = SetWindowLong(Text2.hWnd, GWL_WNDPROC, AddressOf newfunc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, old)
Call SetWindowLong(Text2.hWnd, GWL_WNDPROC, old)
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_WNDPROC = (-4)
Private Sub Form_Load()
old = SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf newfunc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, old)
End Sub
module:
Option Explicit
Public old 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
Public Function newfunc(ByVal hWnd As Long, ByVal umsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If umsg = &HD Then
Debug.Print hWnd, wParam, lParam
newfunc = 0
Exit Function
ElseIf umsg = 1997 Then
Debug.Print 1997
End If
newfunc = CallWindowProc(old, hWnd, umsg, wParam, lParam)
End Function
摘自MSDN
If the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to be sent to the specified window or control. If the target window is owned by another process and has a caption, GetWindowText retrieves the window caption text. If the window does not have a caption, the return value is a null string. This behavior is by design. It allows applications to call GetWindowText without hanging if the process that owns the target window is hung. However, if the target window is hung and it belongs to the calling application, GetWindowText will hang the calling application.
To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.