主题:[讨论]如何获得当前活动窗体
'以下VB代码,返回了windows中运行的程序。如何修改成返回运行的窗口名呢?谢谢!!!
'以下在.Bas
Option Explicit
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public coll As New Collection
'hWnd是Window传给我们的Window handle,而lParam是我们呼叫EnumWindows()时的第
'二个叁数值,在这个例子中,我们传0进来,所以lParam一直是0
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim S As String, pid As Long
If GetParent(hwnd) = 0 Then
'读取 hWnd 的视窗标题
S = String(80, 0)
Call GetWindowText(hwnd, S, 80)
S = Left(S, InStr(S, Chr(0)) - 1)
Call GetWindowThreadProcessId(hwnd, pid)
'当没有标题的hWnd之pid被加入Coll的Collection时,若pid重覆会有错,我们不管它
On Error Resume Next
If Len(S) = 0 Then
'没有标题,则记录Class Name
S = String(255, 0)
Call GetClassName(hwnd, S, 255)
S = Left(S, InStr(S, Chr(0)) - 1)
coll.Add "-!@" + S, Str(pid) 'key 为Pid
Else
'如果相同的pid记录两次,便会产生err, 而去执行errh的程序
On Error GoTo errh
If IsWindowVisible(hwnd) Then
coll.Add S, Str(pid)
End If
End If
End If
EnumWindowsProc = True ' 表示继续列举 hWnd
Exit Function
errh:
'如果先前coll 记录key=pid的 那个Item记录的是ClassName,则Item以Window
'的Title来取代
If Mid(coll.Item(Str(pid)), 1, 3) = "-!@" Then '表示先前以ClassName记录
coll.Remove (Str(pid))
coll.Add S, Str(pid)
End If
EnumWindowsProc = True ' 表示继续列举 hWnd
End Function
'以下在form,需一个Command1, 一个ListBox
Private Sub Command1_Click()
Dim co As Variant
List1.Clear
Call EnumWindows(AddressOf EnumWindowsProc, 0&)
For Each co In coll
If Mid(co, 1, 3) = "-!@" Then
co = "Class Name:" + Mid(co, 4)
End If
List1.AddItem co
Next
End Sub
'以下在.Bas
Option Explicit
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public coll As New Collection
'hWnd是Window传给我们的Window handle,而lParam是我们呼叫EnumWindows()时的第
'二个叁数值,在这个例子中,我们传0进来,所以lParam一直是0
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim S As String, pid As Long
If GetParent(hwnd) = 0 Then
'读取 hWnd 的视窗标题
S = String(80, 0)
Call GetWindowText(hwnd, S, 80)
S = Left(S, InStr(S, Chr(0)) - 1)
Call GetWindowThreadProcessId(hwnd, pid)
'当没有标题的hWnd之pid被加入Coll的Collection时,若pid重覆会有错,我们不管它
On Error Resume Next
If Len(S) = 0 Then
'没有标题,则记录Class Name
S = String(255, 0)
Call GetClassName(hwnd, S, 255)
S = Left(S, InStr(S, Chr(0)) - 1)
coll.Add "-!@" + S, Str(pid) 'key 为Pid
Else
'如果相同的pid记录两次,便会产生err, 而去执行errh的程序
On Error GoTo errh
If IsWindowVisible(hwnd) Then
coll.Add S, Str(pid)
End If
End If
End If
EnumWindowsProc = True ' 表示继续列举 hWnd
Exit Function
errh:
'如果先前coll 记录key=pid的 那个Item记录的是ClassName,则Item以Window
'的Title来取代
If Mid(coll.Item(Str(pid)), 1, 3) = "-!@" Then '表示先前以ClassName记录
coll.Remove (Str(pid))
coll.Add S, Str(pid)
End If
EnumWindowsProc = True ' 表示继续列举 hWnd
End Function
'以下在form,需一个Command1, 一个ListBox
Private Sub Command1_Click()
Dim co As Variant
List1.Clear
Call EnumWindows(AddressOf EnumWindowsProc, 0&)
For Each co In coll
If Mid(co, 1, 3) = "-!@" Then
co = "Class Name:" + Mid(co, 4)
End If
List1.AddItem co
Next
End Sub