如何像微調精靈一樣按次序執行程式?

有網友寄信來問,怎樣才可以按次序執行不同的程式,就像微調精靈一樣呢?(見上圖)其實這是用了Shell和等待的方法,解設我們將等待的時間設定成無限的話,就有以上的效果了!以下是程式碼:(準備一個Command1)

Option Explicit
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFF ' Infinite timeout

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds 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 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Sub Command1_Click()
ShellAndWait "notepad", vbNormalFocus
ShellAndWait "CALC", vbNormalFocus
MsgBox "Finished!"
End Sub

Private Sub ShellAndWait(Path As String, WindowStyle As VbAppWinStyle)
Dim ProcessID As Long
Dim ProcessHandle As Long
ProcessID = Shell(Path, WindowStyle)
ProcessHandle = OpenProcess(SYNCHRONIZE, 0, ProcessID)
WaitForSingleObject ProcessHandle, INFINITE
CloseHandle ProcessHandle
DoEvents
End Sub


上一頁