Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function FindWindowLong Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wcmd As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As LongPublic Const SWP_HIDEWINDOW = &H80 Private Const GW_HWNDNEXT = 2Public Sub ShellProgram(sFilePath As String) Dim dblRetVal As Double dim child_hwnd as Long Dim lRetVal As Long dblRetVal = Shell(sFilePath, vbMaximizedFocus) If dblRetVal = 0 Then Screen.MousePointer = 0 MsgBox "程序无法运行,请检查文件和运行环境是否正确!", vbOKOnly + vbExclamation, "提示" Exit Sub End If menuShell(5).Enabled = True menuShell(6).Enabled = True Screen.MousePointer = 0 child_hwnd = InstanceToWnd(dblRetVal) lRetVal = SetWindowPos(child_hwnd, HWND_TOP, 0, tbBarTop.Height / Screen.TwipsPerPixelY, Me.Width / Screen.TwipsPerPixelX - 5, (sbState.Top - tbBarTop.Height) / Screen.TwipsPerPixelY, SWP_SHOWWINDOW) End Sub Private Function InstanceToWnd(ByVal target_pid As Long) As Long Dim test_hwnd As Long Dim test_pid As Long Dim test_thread_id As Long ' Get the first window handle. test_hwnd = FindWindowLong(ByVal 0&, ByVal 0&) ' Loop until we find the target or we run out ' of windows. Do While test_hwnd <> 0 ' See if this window has a parent. If not, ' it is a top-level window. If GetParent(test_hwnd) = 0 Then ' This is a top-level window. See if ' it has the target instance handle. test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid) If test_pid = target_pid Then ' This is the target. InstanceToWnd = test_hwnd Exit Do End If End If test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT) Loop End Function
RetVal = Shell("C:\WINDOWS\CALC.EXE", 0)Shell 函数执行一个可执行文件,返回一个 Variant (Double),如果成功的话,代表这个程序的任务 ID,若不成功,则会返回 0。语法Shell(pathname[,windowstyle])Shell 函数的语法含有下面这些命名参数:部分 描述
pathname 必要参数。Variant (String),要执行的程序名,以及任何必需的参数或命令行变量,可能还包括目录或文件夹,以及驱动器。
Windowstyle 可选参数。Variant (Integer),表示在程序运行时窗口的样式。如果 windowstyle 省略,则程序是以具有焦点的最小化窗口来执行的。
windowstyle 命名参数有以下这些值:常量 值 描述
vbHide 0 窗口被隐藏,且焦点会移到隐式窗口。
VbNormalFocus 1 窗口具有焦点,且会还原到它原来的大小和位置。
VbMinimizedFocus 2 窗口会以一个具有焦点的图标来显示。
VbMaximizedFocus 3 窗口是一个具有焦点的最大化窗口。
VbNormalNoFocus 4 窗口会被还原到最近使用的大小和位置,而当前活动的窗口仍然保持活动。
VbMinimizedNoFocus 6 窗口会以一个图标来显示。而当前活动的的窗口仍然保持活动。
Private Declare Function FindWindowLong Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wcmd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As LongPublic Const SWP_HIDEWINDOW = &H80
Private Const GW_HWNDNEXT = 2Public Sub ShellProgram(sFilePath As String)
Dim dblRetVal As Double
dim child_hwnd as Long
Dim lRetVal As Long
dblRetVal = Shell(sFilePath, vbMaximizedFocus)
If dblRetVal = 0 Then
Screen.MousePointer = 0
MsgBox "程序无法运行,请检查文件和运行环境是否正确!", vbOKOnly + vbExclamation, "提示"
Exit Sub
End If
menuShell(5).Enabled = True
menuShell(6).Enabled = True
Screen.MousePointer = 0
child_hwnd = InstanceToWnd(dblRetVal)
lRetVal = SetWindowPos(child_hwnd, HWND_TOP, 0, tbBarTop.Height / Screen.TwipsPerPixelY, Me.Width / Screen.TwipsPerPixelX - 5, (sbState.Top - tbBarTop.Height) / Screen.TwipsPerPixelY, SWP_SHOWWINDOW)
End Sub
Private Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long
Dim test_pid As Long
Dim test_thread_id As Long
' Get the first window handle.
test_hwnd = FindWindowLong(ByVal 0&, ByVal 0&)
' Loop until we find the target or we run out
' of windows.
Do While test_hwnd <> 0
' See if this window has a parent. If not,
' it is a top-level window.
If GetParent(test_hwnd) = 0 Then
' This is a top-level window. See if
' it has the target instance handle.
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid) If test_pid = target_pid Then
' This is the target.
InstanceToWnd = test_hwnd
Exit Do
End If
End If
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function