VB 获取当前焦点所在的控件句柄
Option Explicit
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetFocus Lib "user32" () As Long
'----------------------------------------------------
'获取当前控件句柄
'----------------------------------------------------
'备注:
'----------------------------------------------------
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