vb 判断控件是否有焦点和控件类型

 
(窗体中一个timer,两个label)
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long

Private Type POINTAPI
    x As Long
    y As Long
End Type

Private Sub Form_Load()
    SetWindowPos Me.hwnd, -1, 0, 0, 240, 100, conSwpNoActivate Or conSwpShowWindow
End Sub

Private Sub Timer1_Timer()
    Dim xy As POINTAPI
    Dim ahwnd As Long
    GetCursorPos xy
    ahwnd = WindowFromPointXY(xy.x, xy.y)
    Label1.Caption = "鼠标所在的控件类型:" & ClassName(ahwnd)
    Label2.Caption = "获得焦点的控件类型:" & ClassName(GetHwnd)
End Sub

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

Private Function ClassName(nHwnd As Long) As String
    Dim str As String
    str = Space(255)
    GetClassName nHwnd, str, 255
    ClassName = str
End Function


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值