vb如何获取当前活动窗口的标题名称
展开全部
'添加窗体Form1,卷标Label1,定时器Timer1,然后添加如下代码:
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private 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 Long
Private Sub Form_Load()
Label1.AutoSize = True
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
hWnd1 = GetForegroundWindow() '得到活动窗口的句柄
Label1.Caption = getCaption(hWnd1)
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
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private 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 Long
Private Sub Form_Load()
Label1.AutoSize = True
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
hWnd1 = GetForegroundWindow() '得到活动窗口的句柄
Label1.Caption = getCaption(hWnd1)
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |