【 在 wfat (wfat) 的大作中提到: 】 : 我在VB中用 shell 函数调用了一个外部的应用程序,请问经过 n 分钟后, : 如何知道它是否正在运行? 用什么方法可以把它关闭? VB 自带有函数还是要 : 调用WINAPI(请问是用哪一个?其各参数的作用又是如何?).
'在模块中加入如下代码 Option Explicit Public glPid As Long Public glHelpHandle As Long Public colHelpHandle As New Collection Public Const WM_CLOSE = &H10 Public ProcIsRunning As Boolean Public ProcTaskID As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, B yVal lParam As Long) As Long
Public Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lpData As Long) As Long Dim lParent As Long Dim lThreadId As Long Dim lProcessId As Long ' ' This callback function is called by Windows (from the EnumWindows ' API call) for EVERY top-level window that exists. It populates a ' collection with the handles of all parent windows owned by the ' process that we started. ' fEnumWindowsCallBack = 1 lThreadId = GetWindowThreadProcessId(hwnd, lProcessId)
If glPid = lProcessId Then lParent = GetParent(hwnd) If lParent = 0 Then colHelpHandle.Add hwnd End If End If End Function
Public Function fEnumWindows() As Boolean Dim hwnd As Long ' ' The EnumWindows function enumerates all top-level windows ' by passing the handle of each window, in turn, to an ' application-defined callback function. EnumWindows ' continues until the last top-level window is enumerated or ' the callback function returns FALSE. ' Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd) End Function Public Sub KillProcessor(ProcID As Long) Dim i As Long ' ' Enumerate all parent windows for the process. ' glPid = ProcID Call fEnumWindows ' ' Send a close command to each parent window. ' The app may issue a close confirmation dialog ' depending on how it handles the WM_CLOSE message. ' For i = 1 To colHelpHandle.Count glHelpHandle = colHelpHandle.Item(i) Call SendMessage(glHelpHandle, WM_CLOSE, 0&, 0&) Next End Sub Public Sub CloseProc(ProcTaskID As Long) If ProcIsRunning Then KillProcessor ProcTaskID ProcIsRunning = False End If End Sub
Public Sub OpenProc(ProcName As String) If ProcIsRunning = False Then ProcIsRunning = True ProcTaskID = Shell(ProcName, vbMaximizedFocus) End If End Sub
'在窗口代码中
'调用 OpenProc "C:\windows\notepad.exe" '关闭 CloseProc ProcTaskID
-- ※ 来源:.网易虚拟社区北京站 http://bj.netease.com.[FROM: 210.72.253.109]
|
|