'参考下面的代码,根据实际需求稍作更改即可
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Sub Command1_Click()
Dim hwnd As Long
Dim pid As Long
Dim hProcess As Long, lpExitCode As Long
hwnd = FindWindow(vbNullString, "未命名 - 画图") '通过窗口名称获取窗口句柄
If hwnd > 0 Then
Call GetWindowThreadProcessId(hwnd, pid) '获取进程标识符(pid)
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, pid) '获取进程句柄
If hProcess = 0 Then Exit Sub
If GetExitCodeProcess(hProcess, lpExitCode) = 0 Then
Call CloseHandle(hProcess)
Exit Sub
End If
Call TerminateProcess(hProcess, lpExitCode) '终止进程(关闭窗口)
Call CloseHandle(hProcess)
End If
End Sub