Private Declare Function GetForegroundWindow Lib "user32" () As LongPrivate Declare Function GetFocus Lib "user32" () As LongPrivate Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal Hwnd As Long) As Long
Dim hWnd1 As LongPrivate Sub Command1_Click()End SubPrivate Sub Form_Load()
Label1.AutoSize = True
Timer1.Interval = 500
End SubPrivate Sub Timer1_Timer()
If Me.Hwnd <> GetForegroundWindow Then hWnd1 = GetForegroundWindow() '得到活动窗口的句柄
 Label1.Caption = getCaption(hWnd1)
 End If
 
End Sub
'根据窗口句柄得到该窗口的标题
Function getCaption(Hwnd As Long)
 Dim hWndlength As Long, hWndTitle As String, A As Long
 hWndlength = GetWindowTextLength(Hwnd)
 hWndTitle = String$(hWndlength, 0)
 A = GetWindowText(Hwnd, hWndTitle, (hWndlength + 1))
 getCaption = hWndTitle
End Function
'获得当前焦点所在控件句柄
Public Function GetHwnd() As Long
Dim Hwnd As Long
Dim PID As Long
Dim TID As Long
Dim hWndFocus As Long
Hwnd = GetForegroundWindow
If Hwnd Then
TID = GetWindowThreadProcessId(Hwnd, PID)
AttachThreadInput App.ThreadID, TID, True
GetHwnd = GetFocus
AttachThreadInput App.ThreadID, TID, False
End If
End Function
按钮的代码没填写,我不会写
我想用一个timer判断当前窗体是不是form1,如果是的话则不记录句柄,如果不是则记录句柄。然后点击按钮发送按键到句柄记录的控件。各位大大要给力啊!!!