方法一:用FindWindow查找窗口 方法二:得到命令行的返回信息 方法三:(没有测试) Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Sub Command1_Click() Dim X Me.Caption = "开始运行" X = Shell("NotePad.EXE", 1) While IsRunning(X) DoEvents Wend Me.Caption = "结束运行" End Sub Function IsRunning(ByVal ProgramID) As Boolean ' 传入进程标识ID Dim hProgram As Long '被检测的程序进程句柄 hProgram = OpenProcess(0, False, ProgramID) If Not hProgram = 0 Then IsRunning = True Else IsRunning = False End If CloseHandle hProgram End Function
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Const PROCESS_QUERY_INFORMATION = &H400 Private Const STILL_ACTIVE = &H103 Dim strWinDir As String Dim pidNotepad As Long Dim hProcess As LongPrivate Sub Command1_Click() Dim lngExitCode As Long strWinDir = Environ("windir") pidNotepad = Shell(strWinDir & "\notepad.exe", vbNormalFocus) hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, pidNotepad) Do GetExitCodeProcess hProcess, lngExitCode DoEvents Loop While lngExitCode = STILL_ACTIVE MsgBox "Hello" End Sub 以上代码就可以判断外部程序是否运行完,在窗体上画一个按钮试下。
方法二:得到命令行的返回信息
方法三:(没有测试)
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Sub Command1_Click()
Dim X
Me.Caption = "开始运行"
X = Shell("NotePad.EXE", 1)
While IsRunning(X)
DoEvents
Wend
Me.Caption = "结束运行"
End Sub Function IsRunning(ByVal ProgramID) As Boolean ' 传入进程标识ID
Dim hProgram As Long '被检测的程序进程句柄
hProgram = OpenProcess(0, False, ProgramID)
If Not hProgram = 0 Then
IsRunning = True
Else
IsRunning = False
End If
CloseHandle hProgram
End Function
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const STILL_ACTIVE = &H103
Dim strWinDir As String
Dim pidNotepad As Long
Dim hProcess As LongPrivate Sub Command1_Click()
Dim lngExitCode As Long
strWinDir = Environ("windir")
pidNotepad = Shell(strWinDir & "\notepad.exe", vbNormalFocus)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, pidNotepad)
Do
GetExitCodeProcess hProcess, lngExitCode
DoEvents
Loop While lngExitCode = STILL_ACTIVE
MsgBox "Hello"
End Sub
以上代码就可以判断外部程序是否运行完,在窗体上画一个按钮试下。