怎样判断程序正在运行,怎样关闭它。
1个回答
展开全部
关闭正在运行的程序 你可以使用API函数FindWindow和PostMessage去寻找指定的窗口,并关闭它。 下面的例子教给你怎样找到并关掉一个Caption为“Caluclator”的程序。 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 "置入消息错误! " End If Else MsgBox "Calculator没有打开! " End If 为了让以上的代码工作,你必须在模块文件中什么以下API函数: 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 判断一个32位程序是否结束 Microsoft 提供了少量的函数,如:WaitForSingleObject和 WaitForMultipleObjects 来完成该功能。尽管如此,这些函数并不是时时都有效。举例来说,如果你调用WaitForSingleObject函数监测Internet Explorer的运行,同时你又选择了“帮助”菜单中的“教程”项,结果将会是系统死锁,你不得不终止WaitForSingleObject函数的运行。造成这种结果的 原因是你点选了“教程”后,会返回给Internet Explorer一个地址,告诉浏览器转到下一个网址。这种情况通常发生在一个程序是由调用它的父程序中的一个过程或对象所启动,反过来却要等待父程序的结束。 本文的目的就是要解决这个问题。 Declare Declare Function OpenProcess Lib "kernel32 " (ByVal dwDesiredaccess&, ByVal bInherithandle&, ByVal dwProcessid&) As Long Declare Function GetExitCodeProcess Lib "kernel32 " (ByVal hProcess As Long, lpexitcode As Long) As Long ' Required Constants Const STILL_ACTIVE = &H103 Const PROCESS_QUERY_INFORMATION = &H400 Code Sub ShellWait(cCommandLine As String) Dim hShell As Long Dim hProc As Long Dim lExit As Long hShell = Shell(cCommandLine, vbNormalFocus) hProc = OpenProcess(PROCESS_QUERY_INFORMATION, False, hShell) Do GetExitCodeProcess hProc, lExit DoEvents Loop While lExit = STILL_ACTIVE End Sub 调用 ShellWait,控制权将不会交给一个过程,直到调用该过程的程序结束 查看GetExitCodeProcess的用法 我还要关闭一个MessageBox,有是与否两个选项。我要发一个“是”的消息如何做。FindWindow VB声明 Declare Function FindWindow Lib "user32 " Alias "FindWindowA " (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 说明 寻找窗口列表中第一个符合指定条件的顶级窗口(在vb里使用:FindWindow最常见的一个用途是获得ThunderRTMain类的隐藏窗口的句柄;该类是所有运行中vb执行程序的一部分。获得句柄后,可用api函数GetWindowText取得这个窗口的名称;该名也是应用程序的标题) 返回值 Long,找到窗口的句柄。如未找到相符窗口,则返回零。会设置GetLastError 参数表 参数 类型及说明 lpClassName String,指向包含了窗口类名的空中止(C语言)字串的指针;或设为零,表示接收任何类 lpWindowName String,指向包含了窗口文本(或标签)的空中止(C语言)字串的指针;或设为零,表示接收任何窗口标题 注解 很少要求同时按类与窗口名搜索。为向自己不准备参数传递一个零,最简便的办法是传递vbNullString常数 示例 Dim hw&, cnt& Dim rttitle As String * 256 hw& = FindWindow( "ThunderRT5Main ", vbNullString) ' ThunderRTMain under VB4 cnt = GetWindowText(hw&, rttitle, 255) MsgBox Left$(rttitle, cnt), 0, "RTMain title " FindWindowEx VB声明 Declare Function FindWindowEx Lib "user32 " Alias "FindWindowExA " (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long 说明 在窗口列表中寻找与指定条件相符的第一个子窗口 返回值 Long,找到的窗口的句柄。如未找到相符窗口,则返回零。零表示忽略 结束消息框 Private Const WM_CLOSE = &H10 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 Declare Function EnableWindow Lib "user32 " (ByVal hwnd As Long, ByVal fEnable As Long) As Long Private Declare Function GetParent Lib "user32 " (ByVal hwnd As Long) As Long Private Sub Command1_Click() Dim hwnd1 As Long Dim hwnd2 As Long ' hwnd1 = FindWindow(vbNullString, "Form2 ") hwnd2 = FindWindow(vbNullString, "Iloveyou ") 'Msgbox hwnd1 = GetParent(hwnd2) 'The Parent SendMessage hwnd2, WM_CLOSE, 0&, 0& EnableWindow hwnd1, 1 'Active the Parent End Sub --------------------------------------- Const WM_CLOSE = &H10 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 Sub command_click() Dim hwnd As Long Msgbox "xx ",, "aa " '弹出msgbox hwnd = FindWindow(vbNullString, "aa ") 'Msgbox的标题为aa SendMessage hwnd, WM_CLOSE, 0&, 0& '将它关闭 End Sub
汉坤(北京)流体控制技术有限公司
2023-06-12 广告
2023-06-12 广告
根据大数据评选,2022年电动执行器十大品牌名单有:1. 秀辉。2. 丹冠。3. 方威。4. 天津宝恒。5. 常辅股份。6. 新峰。7. 德博科技。8. 摩控。9. 科威纳。10. deyo。以上十大品牌是电动执行器行业的知名品牌,排名不分...
点击进入详情页
本回答由汉坤(北京)流体控制技术有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询