如何用VB关闭其他应用程序
我用Shell打开了一个其他的应用程序,请问如何在关闭窗口时关闭这个应用程序?
程序代码:
'下面的代码放到模块中 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