EnumWindows and EnumChildWindowshttp://www.mvps.org/vbnet/下API Index里找,有例程http://www.vbgood.com/有申明
我有个列举运行程序的例子,希望对你有帮助: 模块: Option ExplicitDeclare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Declare Function GetClassLong Lib "user32" Alias "GetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 GetDesktopWindow Lib "user32" () As Long Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd 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 SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long 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 Declare Function GetActiveWindow Lib "user32" () As Long Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long Declare Function GetForegroundWindow Lib "user32" () As Long Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Declare Function GetClassInfo Lib "user32" Alias "GetClassInfoA" (ByVal hInstance As Long, ByVal lpClassName As String, lpWndClass As wndClass) As LongType wndClass style As Long lpfnwndproc As Long cbClsextra As Long cbWndExtra2 As Long hInstance As Long hIcon As Long hCursor As Long hbrBackground As Long lpszMenuName As String lpszClassName As String End TypeType RECT Left As Long Top As Long Right As Long Bottom As Long End TypePublic Const WM_ACTIVATE = &H6 Public Const SWP_NOMOVE = &H2 Public Const SWP_NOSIZE = &H1 Public Const HWND_TOPMOST = -1 Public Const GW_CHILD = 5 Public Const GW_HWNDNEXT = 2Public Function GetOpenWindowNames() As Long Dim lngDeskTopHandle As Long Dim lngHand As Long Dim strName As String * 255 Dim lngWindowCount As Long
' 取得窗口的标题 GetWindowText lngHand, strName, Len(strName) lngHand = GetWindow(lngHand, GW_HWNDNEXT) If Left$(strName, 1) <> vbNullChar Then frmClassFinder.lstOpenWindows.AddItem Left$(strName, InStr(1, strName, vbNullChar)) lngWindowCount = lngWindowCount + 1 End If Loop
GetOpenWindowNames = lngWindowCount End Function 窗体: Option Explicit' 获得运行的程序 Private Sub cmdGetClass() Dim lngHand As Long Dim strName As String * 255 Dim wndClass As wndClass Dim lngProcID As Long Dim rctTemp As RECT
模块:
Option ExplicitDeclare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function GetClassLong Lib "user32" Alias "GetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 GetDesktopWindow Lib "user32" () As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd 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 SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
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
Declare Function GetActiveWindow Lib "user32" () As Long
Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetForegroundWindow Lib "user32" () As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetClassInfo Lib "user32" Alias "GetClassInfoA" (ByVal hInstance As Long, ByVal lpClassName As String, lpWndClass As wndClass) As LongType wndClass
style As Long
lpfnwndproc As Long
cbClsextra As Long
cbWndExtra2 As Long
hInstance As Long
hIcon As Long
hCursor As Long
hbrBackground As Long
lpszMenuName As String
lpszClassName As String
End TypeType RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePublic Const WM_ACTIVATE = &H6
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const HWND_TOPMOST = -1
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2Public Function GetOpenWindowNames() As Long
Dim lngDeskTopHandle As Long
Dim lngHand As Long
Dim strName As String * 255
Dim lngWindowCount As Long
' 获得桌面窗口的句柄
lngDeskTopHandle = GetDesktopWindow()
' 获得桌面窗口的子窗口句柄
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
lngWindowCount = 1
' 循环列举
Do While lngHand <> 0
' 取得窗口的标题
GetWindowText lngHand, strName, Len(strName)
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
If Left$(strName, 1) <> vbNullChar Then
frmClassFinder.lstOpenWindows.AddItem Left$(strName, InStr(1, strName, vbNullChar))
lngWindowCount = lngWindowCount + 1
End If
Loop
GetOpenWindowNames = lngWindowCount
End Function
窗体:
Option Explicit' 获得运行的程序
Private Sub cmdGetClass()
Dim lngHand As Long
Dim strName As String * 255
Dim wndClass As wndClass
Dim lngProcID As Long
Dim rctTemp As RECT
' 定位选中的窗口并获得其句柄
lngHand = FindWindow(vbNullString, txtTitle.Text)
' 获得窗口的信息
GetClassName lngHand, strName, Len(strName)
' 获得进程ID与窗口尺寸信息
If Left$(strName, 1) = vbNullChar Then
lblClassName.Caption = "Window Not Found!!!"
Else
lblClassName.Caption = "Class Name: " & strName
GetWindowThreadProcessId lngHand, lngProcID
GetWindowRect lngHand, rctTemp
End If
lblProcessID = "ProcessID: " & lngProcID
lblTop = "Top: " & rctTemp.Top
lblBottom = "Bottom: " & rctTemp.Bottom
lblLeft = "Left: " & rctTemp.Left
lblRight = "Right: " & rctTemp.RightEnd Sub' 刷新信息
Private Sub cmdRefresh_Click()
lstOpenWindows.Clear
cmdGetClass
lblCount.Caption = GetOpenWindowNames & " open Windows."
End Sub' 激活选中的窗口
Private Sub cmdActivate_Click()
Dim lngHand As Long ' 找寻窗口
If Trim$(lblClassName.Caption) = "" Then
lngHand = FindWindow(vbNullChar, Trim$(txtTitle.Text))
Else
lngHand = FindWindow(Right$(lblClassName.Caption, (Len(lblClassName) - 12)), lstOpenWindows.Text)
End If
BringWindowToTop lngHand
End Sub' 初始化
Private Sub Form_Load()
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
txtTitle.SelLength = Len(txtTitle.Text)
lblCount.Caption = GetOpenWindowNames & " open Windows."
End SubPrivate Sub lstOpenWindows_Click()
txtTitle.Text = lstOpenWindows.Text
cmdGetClass
End Sub' 定时属性标签中的信息
Private Sub tmrWinClass_Timer()
Dim lngHand As Long
Dim strName As String * 255
lngHand = GetForegroundWindow
GetWindowText lngHand, strName, Len(strName)
lblCurrent.Caption = strName
GetClassName lngHand, strName, Len(strName)
lblClass.Caption = strName
End Sub