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

前端之家收集整理的这篇文章主要介绍了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,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,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

猜你在找的VB相关文章