Dim ProcessId As Long Private Const PROCESS_QUERY_INFORMATION = &H400 '关闭由SHELL函数打开的文件 Private Const PROCESS_TERMINATE = &H1 Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Sub Command1_Click() ProcessId = Shell("notepad.exe", vbNormalFocus) End SubPrivate Sub Form_Unload(Cancel As Integer) Dim hProcess As Long hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, ProcessId) Call TerminateProcess(hProcess, 3838) End Sub
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongConst WM_CLOSE = &H10winHwnd = FindWindow(vbNullString, yourapp.Caption) 'your application's caption If winHwnd <> 0 The SetForegroundWindow winHwnd Call PostMessage(winHwnd, WM_CLOSE, 0&, 0&) End If
或者 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Const WM_CLOSE = &H10Private Sub Command1_Click() ProcessId = Shell("notepad.exe", vbNormalFocus) End SubPrivate Sub Form_Unload(Cancel As Integer) Dim hwnd As Long hwnd = FindWindow(vbNullString, "无标题 - 记事本") SendMessage hwnd, WM_CLOSE, 0, 0 End Sub
简单点:模块里面:Option Explicit Declare Function EnumWindows Lib "user32" (ByVal wndenmprc As Long, ByVal lParam 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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Const WM_CLOSE = &H10 Private Target As StringPublic Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long Dim buf As String * 256 Dim title As String Dim length As Long length = GetWindowText(app_hWnd, buf, Len(buf)) title = Left$(buf, length) If InStr(title, Target) <> 0 Then SendMessage app_hWnd, WM_CLOSE, 0, 0 End If EnumCallback = 1 End FunctionPublic Sub TerminateTask(app_name As String) Target = app_name EnumWindows AddressOf EnumCallback, 0 End Sub
窗体里面:private sub command1_click TerminateTask "要关闭的程序的标题" end sub
Private Const PROCESS_QUERY_INFORMATION = &H400 '关闭由SHELL函数打开的文件
Private Const PROCESS_TERMINATE = &H1
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Sub Command1_Click()
ProcessId = Shell("notepad.exe", vbNormalFocus)
End SubPrivate Sub Form_Unload(Cancel As Integer)
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, ProcessId)
Call TerminateProcess(hProcess, 3838)
End Sub
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongConst WM_CLOSE = &H10winHwnd = FindWindow(vbNullString, yourapp.Caption) 'your application's caption
If winHwnd <> 0 The
SetForegroundWindow winHwnd
Call PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
End If
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10Private Sub Command1_Click()
ProcessId = Shell("notepad.exe", vbNormalFocus)
End SubPrivate Sub Form_Unload(Cancel As Integer)
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "无标题 - 记事本")
SendMessage hwnd, WM_CLOSE, 0, 0
End Sub
Declare Function EnumWindows Lib "user32" (ByVal wndenmprc As Long, ByVal lParam 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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_CLOSE = &H10
Private Target As StringPublic Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long
length = GetWindowText(app_hWnd, buf, Len(buf))
title = Left$(buf, length)
If InStr(title, Target) <> 0 Then
SendMessage app_hWnd, WM_CLOSE, 0, 0
End If
EnumCallback = 1
End FunctionPublic Sub TerminateTask(app_name As String)
Target = app_name
EnumWindows AddressOf EnumCallback, 0
End Sub
TerminateTask "要关闭的程序的标题"
end sub