Dim RetVal RetVal = Shell("C:\WINDOWS\CALC.EXE", 1) ' 完成Calculator。
你试试看:Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function GetCurrentProcess Lib "kernel32" () As Long Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long Const GW_HWNDNEXT = 2 Dim mWnd As Long Dim target_pid As Long Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Function InstanceToWnd(ByVal target_pid As Long) As Long Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long 'Find the first window test_hwnd = FindWindow(ByVal 0&, ByVal 0&) Do While test_hwnd <> 0 'Check if the window isn't a child If GetParent(test_hwnd) = 0 Then 'Get the window's thread test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid) If test_pid = target_pid Then InstanceToWnd = test_hwnd Exit Do End If End If 'retrieve the next window test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT) Loop End Function Private Sub Command1_Click() target_pid = Shell("calc.exe", 0) mWnd = InstanceToWnd(target_pid) MsgBox mWnd End SubPrivate Sub Command2_Click() ShowWindow mWnd, 3 'vbNormalFocus End SubPrivate Sub Form_Load() Command1.Caption = "shell" Command2.Caption = "show window" End Sub
Shell Function Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero.SyntaxShell(pathname[,windowstyle])The Shell function syntax has these named arguments:Part Description pathname Required; Variant (String). Name of the program to execute and any required arguments or command-line switches; may include directory or folder and drive. windowstyle Optional. Variant (Integer) corresponding to the style of the window in which the program is to be run. If windowstyle is omitted, the program is started minimized with focus. The windowstyle named argument has these values:Constant Value Description vbHide 0 Window is hidden and focus is passed to the hidden window. vbNormalFocus 1 Window has focus and is restored to its original size and position. vbMinimizedFocus 2 Window is displayed as an icon with focus. vbMaximizedFocus 3 Window is maximized with focus. vbNormalNoFocus 4 Window is restored to its most recent size and position. The currently active window remains active. vbMinimizedNoFocus 6 Window is displayed as an icon. The currently active window remains active. ResIf the Shell function successfully executes the named file, it returns the task ID of the started program. The task ID is a unique number that identifies the running program. If the Shell function can't start the named program, an error occurs.Note By default, the Shell function runs other programs asynchronously. This means that a program started with Shell might not finish executing before the statements following the Shell function are executed. --------------------------------------------------------------------------------
RetVal = Shell("C:\WINDOWS\CALC.EXE", 1) ' 完成Calculator。
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Const GW_HWNDNEXT = 2
Dim mWnd As Long
Dim target_pid As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
Private Sub Command1_Click()
target_pid = Shell("calc.exe", 0)
mWnd = InstanceToWnd(target_pid)
MsgBox mWnd
End SubPrivate Sub Command2_Click()
ShowWindow mWnd, 3 'vbNormalFocus
End SubPrivate Sub Form_Load()
Command1.Caption = "shell"
Command2.Caption = "show window"
End Sub
Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero.SyntaxShell(pathname[,windowstyle])The Shell function syntax has these named arguments:Part Description
pathname Required; Variant (String). Name of the program to execute and any required arguments or command-line switches; may include directory or folder and drive.
windowstyle Optional. Variant (Integer) corresponding to the style of the window in which the program is to be run. If windowstyle is omitted, the program is started minimized with focus.
The windowstyle named argument has these values:Constant Value Description
vbHide 0 Window is hidden and focus is passed to the hidden window.
vbNormalFocus 1 Window has focus and is restored to its original size and position.
vbMinimizedFocus 2 Window is displayed as an icon with focus.
vbMaximizedFocus 3 Window is maximized with focus.
vbNormalNoFocus 4 Window is restored to its most recent size and position. The currently active window remains active.
vbMinimizedNoFocus 6 Window is displayed as an icon. The currently active window remains active.
ResIf the Shell function successfully executes the named file, it returns the task ID of the started program. The task ID is a unique number that identifies the running program. If the Shell function can't start the named program, an error occurs.Note By default, the Shell function runs other programs asynchronously. This means that a program started with Shell might not finish executing before the statements following the Shell function are executed.
--------------------------------------------------------------------------------