我上面两个例子结合就是这样
窗体代码改为
Option Explicit
Private sAppName As String, sAppPath As String
Private sFilename As String
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOWNORMAL = 1
Private Sub cmdCheck_Click()
'check if application is running
If IsTaskRunning(sAppName) Then
MsgBox "Application '" & sAppName & "' is running!"
Else
MsgBox "Application '" & sAppName & "' is not running!"
End If
End Sub
Private Sub cmdClose_Click()
'close application
Call EndTask(sAppName)
End Sub
Private Sub cmdStart_Click()
ShellExecute Me.hwnd, vbNullString, "c:\label.txt", vbNullString, "C:\", SW_SHOWNORMAL
End Sub
Private Sub Form_Load()
sFilename = "c:\label.txt"
sAppName = Mid(sFilename, InStrRev(sFilename, "\") + 1) & " - 记事本"
End Sub
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOWNORMAL = 1
Private Sub Form_Load()
ShellExecute Me.hwnd, vbNullString, "c:\label.txt", vbNullString, "C:\", SW_SHOWNORMAL
End Sub
我刚刚回复一个类似的
窗体(三个按钮,运行,检测,关闭)代码:
Option Explicit
Private sAppName As String, sAppPath As String
Private Sub cmdCheck_Click()
'check if application is running
If IsTaskRunning(sAppName) Then
MsgBox "Application '" & sAppName & "' is running!"
Else
MsgBox "Application '" & sAppName & "' is not running!"
End If
End Sub
Private Sub cmdClose_Click()
'close application
Call EndTask(sAppName)
End Sub
Private Sub cmdStart_Click()
Shell sAppPath, vbMinimizedFocus
End Sub
Private Sub Form_Load()
sAppName = "Adobe Photoshop"
sAppPath = "D:\program files\Adobe\Photoshop 6.0\Photoshp.exe"
End Sub
模块代码:
Option Explicit
'API's Function Declarations
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As Any, _
ByVal lpWindowName As String) As Long
'API Constants
Public Const GWL_STYLE = -16
Public Const WS_DISABLED = &H8000000
Public Const WM_CANCELMODE = &H1F
Public Const WM_CLOSE = &H10
Public Function IsTaskRunning(sWindowName As String) As Boolean
Dim hwnd As Long, hWndOffline As Long
On Error GoTo IsTaskRunning_Eh
'get handle of the application
'if handle is 0 the application is currently not running
hwnd = FindWindow(0&, sWindowName)
If hwnd = 0 Then
IsTaskRunning = False
Exit Function
Else
IsTaskRunning = True
End If
IsTaskRunning_Exit:
Exit Function
IsTaskRunning_Eh:
Call ShowError(sWindowName, "IsTaskRunning")
End Function
Public Function EndTask(sWindowName As String) As Integer
Dim X As Long, ReturnVal As Long, TargetHwnd As Long
'find handle of the application
TargetHwnd = FindWindow(0&, sWindowName)
If TargetHwnd = 0 Then Exit Function
If IsWindow(TargetHwnd) = False Then
GoTo EndTaskFail
Else
'close application
If Not (GetWindowLong(TargetHwnd, GWL_STYLE) And WS_DISABLED) Then
X = PostMessage(TargetHwnd, WM_CLOSE, 0, 0&)
DoEvents
End If
End If
我刚刚回复一个类似的
窗体(三个按钮,运行,检测,关闭)代码:
Option Explicit
Private sAppName As String, sAppPath As String
Private Sub cmdCheck_Click()
'check if application is running
If IsTaskRunning(sAppName) Then
MsgBox "Application '" & sAppName & "' is running!"
Else
MsgBox "Application '" & sAppName & "' is not running!"
End If
End Sub
Private Sub cmdClose_Click()
'close application
Call EndTask(sAppName)
End Sub
Private Sub cmdStart_Click()
Shell sAppPath, vbMinimizedFocus
End Sub
Private Sub Form_Load()
sAppName = "Adobe Photoshop"
sAppPath = "D:\program files\Adobe\Photoshop 6.0\Photoshp.exe"
End Sub
模块代码:
Option Explicit
'API's Function Declarations
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As Any, _
ByVal lpWindowName As String) As Long
'API Constants
Public Const GWL_STYLE = -16
Public Const WS_DISABLED = &H8000000
Public Const WM_CANCELMODE = &H1F
Public Const WM_CLOSE = &H10
Public Function IsTaskRunning(sWindowName As String) As Boolean
Dim hwnd As Long, hWndOffline As Long
On Error GoTo IsTaskRunning_Eh
'get handle of the application
'if handle is 0 the application is currently not running
hwnd = FindWindow(0&, sWindowName)
If hwnd = 0 Then
IsTaskRunning = False
Exit Function
Else
IsTaskRunning = True
End If
IsTaskRunning_Exit:
Exit Function
IsTaskRunning_Eh:
Call ShowError(sWindowName, "IsTaskRunning")
End Function
Public Function EndTask(sWindowName As String) As Integer
Dim X As Long, ReturnVal As Long, TargetHwnd As Long
'find handle of the application
TargetHwnd = FindWindow(0&, sWindowName)
If TargetHwnd = 0 Then Exit Function
If IsWindow(TargetHwnd) = False Then
GoTo EndTaskFail
Else
'close application
If Not (GetWindowLong(TargetHwnd, GWL_STYLE) And WS_DISABLED) Then
X = PostMessage(TargetHwnd, WM_CLOSE, 0, 0&)
DoEvents
End If
End If