/**窗体代码**/Option ExplicitPrivate Sub cmdGetClass_Click() '---------------------------------------------------------------------------------------------------------- 'This sub locates information about a window when you select 'it from the list box. '----------------------------------------------------------------------------------------------------------Dim lngHand As Long Dim strName As String * 255 Dim wndClass As wndClass Dim lngProcID As Long Dim rctTemp As RECT'Locate the selected window and get its handle. lngHand = FindWindow(vbNullString, txtTitle.Text)'Using the handle obtained from FindWindow, get all class information 'about the selected window. GetClassName lngHand, strName, Len(strName)'If the name in the text box doesn't match a system window tell the user. 'Otherwise, get the process id and the window size info. 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'Load the labels with the info retrieved. lblProcessID = "ProcessID: " & lngProcID lblTop = "Top: " & rctTemp.Top lblBottom = "Bottom: " & rctTemp.Bottom lblLeft = "Left: " & rctTemp.Left lblRight = "Right: " & rctTemp.RightEnd Sub Private Sub cmdRefresh_Click()'Clear the list box and reload it with the current windows. lstOpenWindows.Clear lblCount.Caption = GetOpenWindowNames & " open Windows."End Sub Private Sub cmdActivate_Click() '---------------------------------------------------------------------------------------------------------- 'This sub activates the selected window. '----------------------------------------------------------------------------------------------------------'Variable to hold the handle to the window. Dim lngHand As Long'Find the window by class or title. If Trim$(lblClassName.Caption) = "" Then lngHand = FindWindow(vbNullChar, Trim$(txtTitle.Text)) Else lngHand = FindWindow(Right$(lblClassName.Caption, (Len(lblClassName) - 12)), lstOpenWindows.Text) End If'Activate the selected window. 'Some windows are hidden so it will make the application the input app. BringWindowToTop lngHandEnd SubPrivate Sub cmdRefreshClass_Click()Call cmdGetClass_ClickEnd SubPrivate Sub Form_Load()'Make our app the top most window in the system. SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE'Highlight the text in txtTitle. txtTitle.SelLength = Len(txtTitle.Text)lblCount.Caption = GetOpenWindowNames & " open Windows."End Sub Private Sub lblClassName_Click()End SubPrivate Sub lstOpenWindows_Click()'Call the hidden cmdGetClass button. txtTitle.Text = lstOpenWindows.Text Call cmdGetClass_ClickEnd Sub
Private Sub tmrWinClass_Timer()'Check every 100 ms for the current application int the 'system and load it's info to our form. Dim lngHand As Long Dim strName As String * 255lngHand = GetForegroundWindowGetWindowText lngHand, strName, Len(strName) lblCurrent.Caption = strNameGetClassName lngHand, strName, Len(strName) lblClass.Caption = strName End Sub/*******模块***************/Option ExplicitType 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 Type'C language TypeDef to hold the size information for a given window. Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type 'API Declares Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _ lpdwProcessId As Long) As LongDeclare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongDeclare Function GetClassLong Lib "user32" Alias "GetClassLongA" (ByVal hwnd As Long, _ ByVal nIndex As Long) As LongDeclare 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 LongDeclare Function GetDesktopWindow Lib "user32" () As LongDeclare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As LongDeclare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, _ ByVal lpString As String, ByVal cch As Long) _ As LongDeclare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, _ ByVal dwNewLong As Long) As LongDeclare 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 LongDeclare Function GetActiveWindow Lib "user32" () As LongDeclare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As LongDeclare Function GetForegroundWindow Lib "user32" () As LongDeclare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _ ByVal wMsg As Long, ByVal wParam As Long, _ lParam As Any) As LongDeclare Function GetClassInfo Lib "user32" Alias "GetClassInfoA" (ByVal hInstance As Long, _ ByVal lpClassName As String, _ lpWndClass As wndClass) As Long'---------------------------------------------------------------------------------------------------------- Public 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 = 2 '----------------------------------------------------------------------------------------------------------Public Function GetOpenWindowNames() As Long '---------------------------------------------------------------------------------------------------------- 'Name: Function GetOpenWindowNames() ' 'Purpose: To retrieve all open windows in the system. ' 'Parameters: N/A ' 'Return: NONE '----------------------------------------------------------------------------------------------------------'Declare local variables Dim lngDeskTopHandle As Long 'Used to hold the value of the Desktop handle. Dim lngHand As Long 'Used to hold each windows handle as it loops. Dim strName As String * 255 'Fixed length string passed to GetWindowText API call. Dim lngWindowCount As Long 'Counter used to return the numberof open windows in the system.'Get the handle for the desktop. lngDeskTopHandle = GetDesktopWindow()'Get the first child of the desktop window. '(Note: The desktop is the parent of all windows in the system. lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)'set the window counter to 1. lngWindowCount = 1'Loop while there are still open windows. Do While lngHand <> 0
'Get the title of the next window in the window list. GetWindowText lngHand, strName, Len(strName)
'Get the sibling of the current window. lngHand = GetWindow(lngHand, GW_HWNDNEXT)
'Make sure the window has a title; and if it does add it to the list. If Left$(strName, 1) <> vbNullChar Then frmClassFinder.lstOpenWindows.AddItem Left$(strName, InStr(1, strName, vbNullChar)) lngWindowCount = lngWindowCount + 1 End If Loop'Return the number of windows opened. GetOpenWindowNames = lngWindowCountEnd Function
做得像进程管理器一样! Option ExplicitPrivate Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Const MAX_PATH As Integer = 260 Private Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long th32ParentProcessID As Long pcPriClassBase As Long dwFlags As Long szExeFile As String * MAX_PATH End Type Const TH32CS_SNAPheaplist = &H1 Const TH32CS_SNAPPROCESS = &H2 Const TH32CS_SNAPthread = &H4 Const TH32CS_SNAPmodule = &H8 Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule Private Sub Command1_Click() Dim i As Long, lPid As Long Dim Proc As PROCESSENTRY32 Dim hSnapShot As Long ListView1.ListItems.Clear '清空ListView hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄 Proc.dwSize = Len(Proc) lPid = ProcessFirst(hSnapShot, Proc) '获取第一个进程的PROCESSENTRY32结构信息数据 i = 0 Do While lPid <> 0 '当返回值非零时继续获取下一个进程 ListView1.ListItems.Add , "a" & i, Hex(Proc.th32ProcessID) '将进程ID添加到ListView1第一列 ListView1.ListItems("a" & i).SubItems(1) = Proc.szExeFile '将进程名添加到ListView1第二列 i = i + 1 lPid = ProcessNext(hSnapShot, Proc) '循环获取下一个进程的PROCESSENTRY32结构信息数据 Loop CloseHandle hSnapShot '关闭进程“快照”句柄 End SubPrivate Sub Command2_Click() Dim lPHand As Long, TMBack As Long If ListView1.SelectedItem.Text <> "" Then If MsgBox("确实要结束进程[" & ListView1.SelectedItem.SubItems(1) & "]吗?", vbYesNo) = vbYes Then lPHand = Val("&H" & ListView1.SelectedItem.Text) lPHand = OpenProcess(1&, True, lPHand) '获取进程句柄 TMBack = TerminateProcess(lPHand, 0&) '关闭进程 If TMBack <> 0 Then MsgBox ListView1.SelectedItem.SubItems(1) & "已经被终止!" Else MsgBox ListView1.SelectedItem.SubItems(1) & "不能被终止!" End If CloseHandle lPHand Command1_Click '刷新进程列表 End If End If End SubPrivate Sub Form_Load() Me.Caption = "进程管理器" Command1.Caption = "刷新" Command2.Caption = "结束进程" ListView1.ColumnHeaders.Clear ListView1.ColumnHeaders.Add , "a", "进程ID", 600 ListView1.ColumnHeaders.Add , "b", "进程名", 4000 ListView1.View = lvwReport Command1_Click '刷新进程列表 End Sub
'----------------------------------------------------------------------------------------------------------
'This sub locates information about a window when you select
'it from the list box.
'----------------------------------------------------------------------------------------------------------Dim lngHand As Long
Dim strName As String * 255
Dim wndClass As wndClass
Dim lngProcID As Long
Dim rctTemp As RECT'Locate the selected window and get its handle.
lngHand = FindWindow(vbNullString, txtTitle.Text)'Using the handle obtained from FindWindow, get all class information
'about the selected window.
GetClassName lngHand, strName, Len(strName)'If the name in the text box doesn't match a system window tell the user.
'Otherwise, get the process id and the window size info.
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'Load the labels with the info retrieved.
lblProcessID = "ProcessID: " & lngProcID
lblTop = "Top: " & rctTemp.Top
lblBottom = "Bottom: " & rctTemp.Bottom
lblLeft = "Left: " & rctTemp.Left
lblRight = "Right: " & rctTemp.RightEnd Sub
Private Sub cmdRefresh_Click()'Clear the list box and reload it with the current windows.
lstOpenWindows.Clear
lblCount.Caption = GetOpenWindowNames & " open Windows."End Sub
Private Sub cmdActivate_Click()
'----------------------------------------------------------------------------------------------------------
'This sub activates the selected window.
'----------------------------------------------------------------------------------------------------------'Variable to hold the handle to the window.
Dim lngHand As Long'Find the window by class or title.
If Trim$(lblClassName.Caption) = "" Then
lngHand = FindWindow(vbNullChar, Trim$(txtTitle.Text))
Else
lngHand = FindWindow(Right$(lblClassName.Caption, (Len(lblClassName) - 12)), lstOpenWindows.Text)
End If'Activate the selected window.
'Some windows are hidden so it will make the application the input app.
BringWindowToTop lngHandEnd SubPrivate Sub cmdRefreshClass_Click()Call cmdGetClass_ClickEnd SubPrivate Sub Form_Load()'Make our app the top most window in the system.
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE'Highlight the text in txtTitle.
txtTitle.SelLength = Len(txtTitle.Text)lblCount.Caption = GetOpenWindowNames & " open Windows."End Sub
Private Sub lblClassName_Click()End SubPrivate Sub lstOpenWindows_Click()'Call the hidden cmdGetClass button.
txtTitle.Text = lstOpenWindows.Text
Call cmdGetClass_ClickEnd Sub
'system and load it's info to our form.
Dim lngHand As Long
Dim strName As String * 255lngHand = GetForegroundWindowGetWindowText lngHand, strName, Len(strName)
lblCurrent.Caption = strNameGetClassName lngHand, strName, Len(strName)
lblClass.Caption = strName
End Sub/*******模块***************/Option ExplicitType 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 Type'C language TypeDef to hold the size information for a given window.
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
'API Declares
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _
lpdwProcessId As Long) As LongDeclare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongDeclare Function GetClassLong Lib "user32" Alias "GetClassLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long) As LongDeclare 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 LongDeclare Function GetDesktopWindow Lib "user32" () As LongDeclare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As LongDeclare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String, ByVal cch As Long) _
As LongDeclare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As LongDeclare 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 LongDeclare Function GetActiveWindow Lib "user32" () As LongDeclare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As LongDeclare Function GetForegroundWindow Lib "user32" () As LongDeclare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As LongDeclare Function GetClassInfo Lib "user32" Alias "GetClassInfoA" (ByVal hInstance As Long, _
ByVal lpClassName As String, _
lpWndClass As wndClass) As Long'----------------------------------------------------------------------------------------------------------
Public 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 = 2
'----------------------------------------------------------------------------------------------------------Public Function GetOpenWindowNames() As Long
'----------------------------------------------------------------------------------------------------------
'Name: Function GetOpenWindowNames()
'
'Purpose: To retrieve all open windows in the system.
'
'Parameters: N/A
'
'Return: NONE
'----------------------------------------------------------------------------------------------------------'Declare local variables
Dim lngDeskTopHandle As Long 'Used to hold the value of the Desktop handle.
Dim lngHand As Long 'Used to hold each windows handle as it loops.
Dim strName As String * 255 'Fixed length string passed to GetWindowText API call.
Dim lngWindowCount As Long 'Counter used to return the numberof open windows in the system.'Get the handle for the desktop.
lngDeskTopHandle = GetDesktopWindow()'Get the first child of the desktop window.
'(Note: The desktop is the parent of all windows in the system.
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)'set the window counter to 1.
lngWindowCount = 1'Loop while there are still open windows.
Do While lngHand <> 0
'Get the title of the next window in the window list.
GetWindowText lngHand, strName, Len(strName)
'Get the sibling of the current window.
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
'Make sure the window has a title; and if it does add it to the list.
If Left$(strName, 1) <> vbNullChar Then
frmClassFinder.lstOpenWindows.AddItem Left$(strName, InStr(1, strName, vbNullChar))
lngWindowCount = lngWindowCount + 1
End If
Loop'Return the number of windows opened.
GetOpenWindowNames = lngWindowCountEnd Function
Option ExplicitPrivate Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Const TH32CS_SNAPheaplist = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPthread = &H4
Const TH32CS_SNAPmodule = &H8
Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
Private Sub Command1_Click()
Dim i As Long, lPid As Long
Dim Proc As PROCESSENTRY32
Dim hSnapShot As Long
ListView1.ListItems.Clear '清空ListView
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
Proc.dwSize = Len(Proc)
lPid = ProcessFirst(hSnapShot, Proc) '获取第一个进程的PROCESSENTRY32结构信息数据
i = 0
Do While lPid <> 0 '当返回值非零时继续获取下一个进程
ListView1.ListItems.Add , "a" & i, Hex(Proc.th32ProcessID) '将进程ID添加到ListView1第一列
ListView1.ListItems("a" & i).SubItems(1) = Proc.szExeFile '将进程名添加到ListView1第二列
i = i + 1
lPid = ProcessNext(hSnapShot, Proc) '循环获取下一个进程的PROCESSENTRY32结构信息数据
Loop
CloseHandle hSnapShot '关闭进程“快照”句柄
End SubPrivate Sub Command2_Click()
Dim lPHand As Long, TMBack As Long
If ListView1.SelectedItem.Text <> "" Then
If MsgBox("确实要结束进程[" & ListView1.SelectedItem.SubItems(1) & "]吗?", vbYesNo) = vbYes Then
lPHand = Val("&H" & ListView1.SelectedItem.Text)
lPHand = OpenProcess(1&, True, lPHand) '获取进程句柄
TMBack = TerminateProcess(lPHand, 0&) '关闭进程
If TMBack <> 0 Then
MsgBox ListView1.SelectedItem.SubItems(1) & "已经被终止!"
Else
MsgBox ListView1.SelectedItem.SubItems(1) & "不能被终止!"
End If
CloseHandle lPHand
Command1_Click '刷新进程列表
End If
End If
End SubPrivate Sub Form_Load()
Me.Caption = "进程管理器"
Command1.Caption = "刷新"
Command2.Caption = "结束进程"
ListView1.ColumnHeaders.Clear
ListView1.ColumnHeaders.Add , "a", "进程ID", 600
ListView1.ColumnHeaders.Add , "b", "进程名", 4000
ListView1.View = lvwReport
Command1_Click '刷新进程列表
End Sub