'======================================
' 获取运行进程ID 声明
'======================================
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetWindow Lib "user32" ( _
ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) _
As Long
'================================
' 获取运行进程名称
'================================
Public 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 '对获取整个屏幕子窗口的ID是否为0判断
GetWindowText lngHand, strName, Len(strName) '取得一个窗体的标题(caption)文字,或者一个控件的内容(在vb里使用:使用vb窗体或控件的caption或text属性)
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
If Left$(strName, 1) <> vbNullChar Then
frmMain.lstOpenWindows.AddItem Left$(strName, InStr(1, strName, vbNullChar)) 'List内显示运行程序ID
lngWindowCount = lngWindowCount + 1 '运行程序数累加
End If
Loop
GetOpenWindowNames = lngWindowCount
End Function
'===========================
' 显示进程信息
'==========================
Private Sub lstOpenWindows_Click()
txtTitle.Text = lstOpenWindows.Text
Call getClassInfo '调用 获取进程信息 函数
End Sub
'==========================
' 获取进程信息
'==========================
Private Sub getClassInfo()
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)
If Left$(strName, 1) = vbNullChar Then
lblClassName.Caption = "Window Not Found!!!"
Else
lblClassName.Caption = "类名: " & strName
GetWindowThreadProcessId lngHand, lngProcID
GetWindowRect lngHand, rctTemp
End If
lblProcsId = "进程ID: " & lngProcID
End Sub