'Lay handle window cua cua so window tiep theo
'Gia tri tra ve:
'Thanh cong = handle window
'That bai: = 0
#If Win64 Then
Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" _
(ByVal hwnd As LongPtr, _
ByVal wFlag As Long) As LongPtr
#Else
Declare Function GetNextWindow Lib "user32" Alias "GetWindow" _
(ByVal hwnd As Long, _
ByVal wFlag As Long) As Long
#End If
'Lay trang thai window la xem duoc hay la khong
'Gia tri tra ve:
'Thanh cong : <> 0
'That bai: = 0
#If Win64 Then
Declare PtrSafe Function IsWindowVisible Lib "user32" _
(ByVal hwnd As LongPtr) As LongPtr
#Else
Declare Function IsWindowVisible Lib "user32" _
(ByVal hwnd As Long) As Long
#End If
'Lay caption tilte cua window
'Gia tri tra ve:
'Thanh cong: so byte cua text da duoc copy
'That bai: = 0
#If Win64 Then
Declare PtrSafe Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As LongPtr, _
ByVal lpstring As String, _
ByVal cch As Long) As LongPtr
#Else
Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpstring As String, _
ByVal cch As Long) As Long
#End If
'Lay class name cua window
'Gia tri tra ve:
'Thanh cong: so byte da doc
'That bai: = 0
#If Win64 Then
Declare PtrSafe Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As LongPtr, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As LongPtr
#Else
Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
#End If
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Sub GetClassName_Sample()
#If Win64 Then
Dim hwnd As LongPtr
#Else
Dim hwnd As Long
#End If
Dim strClassName As String * 100
Dim strCaption As String * 60
hwnd = FindWindow(vbNullString, vbNullString)
Application.ScreenUpdating = False
ThisWorkbook.Sheets("Sheet2").Activate
Columns("A:B").Clear
Cells(1).Resize(, 2).Value = Array("Caption", "ClassName")
Do
If IsWindowVisible(hwnd) Then
GetWindowText hwnd, strCaption, Len(strCaption)
GetClassName hwnd, strClassName, Len(strClassName)
With Cells(Rows.Count, 1).End(xlUp).Offset(1)
.Value = Left(strCaption, InStr(strCaption, vbNullChar) - 1)
.Offset(, 1).Value = Left(strClassName, _
InStr(strClassName, vbNullChar) - 1)
End With
End If
hwnd = GetNextWindow(hwnd, GW_HWNDNEXT)
Loop Until hwnd = GetNextWindow(hwnd, GW_HWNDLAST)
Columns("A:B").AutoFit
Application.ScreenUpdating = True
End Sub