如何用vb关闭一个应用程序
4个回答
展开全部
你可以使用API函数FindWindow和PostMessage来寻找一个窗口并且关闭它。下面的范例演示如何关闭一个标题为"Calculator"的窗口。
'下面的代码放到模块中
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 'FindWindowAPI函数
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long 'PostMessageAPI函数
Public Const WM_CLOSE = &H10 '常用变量定义
'程序代码
Dim winHwnd As Long '定义一个长整形变量winHwnd
Dim RetVal As Long '定义一个长整形变量RetVal
winHwnd = FindWindow(vbNullString, "Calculator") 'API函数查找"Calculator"这个窗口
Debug.Print winHwnd '显示这个窗口句柄
If winHwnd <> 0 Then '如不为0,表示找到窗口
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&) '向这个窗口发送关闭信息
If RetVal = 0 Then '如果返回信息为0.表示失败,未发送成功
MsgBox "Error posting message." '提示发送失败
End If
Else
MsgBox "The Calculator is not open." '提示没有找到打开的程序
End If
展开全部
你可以使用API函数FindWindow和PostMessage来寻找一个窗口并且关闭它。下面的范例演示如何关闭一个标题为"Calculator"的窗口。
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "Calculator")
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 "The Calculator is not open."
End If
For this code to work, you must have declared the API functions in a module in your project. You must put the following in the declarations section of the module.
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
该文章转载自无忧考网:http://www.51test.net
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "Calculator")
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 "The Calculator is not open."
End If
For this code to work, you must have declared the API functions in a module in your project. You must put the following in the declarations section of the module.
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
该文章转载自无忧考网:http://www.51test.net
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Shell "cmd.exe /c taskkill /f /im 进程名"
示例:Shell "cmd.exe /c taskkill /f /im 记事本.exe"
示例:Shell "cmd.exe /c taskkill /f /im 记事本.exe"
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用API
OpenProcess和TerminateProcess
OpenProcess和TerminateProcess
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询