加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 22:52:21 所属栏目:大数据 来源:网络整理
导读:(窗体中一个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 LongPrivate Declar
(窗体中一个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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读