Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Public Const WM_CLOSE = &H10
2.在Form上拉兩個按鍵,並COPY下面程式
Private Sub Command1_Click()
Rem 開啟小算盤按鈕
Shell "C:\WINNT\system32\calc.exe" '程式的路徑
End Sub
Private Sub Command2_Click()
Rem 關閉小算盤按鈕
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "小算盤") '欲關閉的程式的名稱
Debug.Print winHwnd
If winHwnd <> 0 Then
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
MsgBox "Error posting message."
End If
Else
MsgBox "並未開啟小算盤程式."
End If
End Sub作者: mark15jill 時間: 2018-8-16 11:52
Dim app As String
Dim objWMI As Object, objProcess As Object, objList As Object
Dim intError As Integer
app = "calc.exe" '這裡填入要關閉的執行檔名稱
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objList = objWMI.ExecQuery("select * from win32_process where name='" & app & "'")
For Each objProcess In objList
intError = objProcess.Terminate
If intError <> 0 Then Exit For
Next
Set objWMI = Nothing
Set objList = Nothing
Set objProcess = Nothing