冷汗……
凭我的能力可能不能用进度条监视进度,但我能同步执行……
使用 ShellExecuteEx 函数(在 SHELL32.DLL 里面)。这个函数的原型如下:
Declare Function ShellExecuteEx Lib "shell32" (ByRef ShellExecInfo As SHELLEXECUTEINFO) As Long
C++ 语言格式(经过我的改写):
#define SEE_MASK_NOASYNC 0x100000
SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "xxx.exe"; //can be a file as well
ShExecInfo.lpParameters = "";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
从 C++ 移过来是这样的:
Const SEE_MASK_NOASYNC = &H100000
Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Integer
hInstApp As Long
lpIDList As Any
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hMonitor As Long
End Type
Dim ShExecInfo As SHELLEXECUTEINFO
With ShExecInfo
.cbSize = Len(SHELLEXECUTEINFO)
.fMask = SEE_MASK_NOASYNC 'fMask 的取值见第二个参考网址
.hwnd = Me.hWnd
.lpVerb = "OPEN" 'lpVerb 的取值见第二个参考网址
.lpFile = "{FILENAME}"; '指定文件名,可以是任意存在的文件
.lpParameters = ""
.lpDirectory = ""
.nShow = 1 '显示出来
.hInstApp = 0
End With
ShellExecuteEx(ShExecInfo)
调用完成后进程的句柄由 hProcess 返回,可使用 TerminateProcess 关闭。
有关这个函数,参考网址中有很详细的说明,在此不再赘述。
调用之后这个进程就是同步的了,进程结束后才会执行之后的语句。
再回答补充的问题。VB 的 VBRUN、VB、VBA 库(msvbvm60.dll)里面也就 Shell 函数可以打开文件了。注意一下,Shell 调用的还是 WinExec 函数(见 kernel32.dll):
Declare Function WinExec Lib "kernel32" Alias "WinExec" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long。
是不是很亲切呢?对比一下 Shell 就知道了。同样返回 PID,除了参数传递不同(ByVal 和 ByRef),没有不一样的,包括 nCmdShow,和 Shell 的第二个参数取值都一样。
用“原装”的不行,就来“外援”:ShellExecute 和 ShellExecuteEx。这两个函数其实参考网址里面已经讲得很详细了,网上也有很多用法之类的东西。
'放一个模块里(StillRun.bas)
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
'检测是否还在运行
Public Function StillRun(ByVal ProgramID) As Boolean
Dim lHProgram As Long
Dim lReturn As Long
Dim hProgram As Long
hProgram = 0
hProgram = OpenProcess(0, False, ProgramID)
If Not hProgram = 0 Then
StillRun = True
Else
StillRun = False
End If
CloseHandle hProgram
End Function
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'窗体代码:
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Private Sub Command1_Click()
Dim pID As Double
pID = Shell("cmd /c c:\temp.bat", 0)
While StillRun(pID) = True '返回True表示还在运行
DoEvents '加上这句不会太卡
Wend
MsgBox "已完成"
End Sub
好久没用了,不知道行不行,你试试把。