在VB中如何实现调用某个应用程序打开某个文件(不使用shell函数,可以使用户自己选择)?如何实现在VB中对于该程序的关闭(不采用手动关闭该应用程序的方法,比如点击VB界面中的某个按钮就可以实现)?

解决方案 »

  1.   

    我刚刚回复一个类似的
    窗体(三个按钮,运行,检测,关闭)代码:
    Option Explicit
    Private sAppName As String, sAppPath As StringPrivate 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 SubPrivate Sub cmdClose_Click()
        'close application
        
         Call EndTask(sAppName)
    End SubPrivate Sub cmdStart_Click()
        Shell sAppPath, vbMinimizedFocusEnd SubPrivate 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 LongPrivate Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long) As LongPrivate Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
        ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        ByVal lParam As Long) As LongPublic 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 = &H10Public 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 FunctionIsTaskRunning_Eh:
        Call ShowError(sWindowName, "IsTaskRunning")
    End FunctionPublic 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
        
        GoTo EndTaskSucceedEndTaskFail:
        ReturnVal = False
        MsgBox "EndTask: cannot terminate " & sWindowName & " task"
        GoTo EndTaskEndSubEndTaskSucceed:
        ReturnVal = TrueEndTaskEndSub:
        EndTask% = ReturnVal
    End FunctionPublic Function ShowError(sText As String, sProcName As String)
        'this function displays an error that occured
        
        Dim sMsg As String
        sMsg = "Error # " & Str(Err.Number) & " was generated by " _
             & Err.Source & vbCrLf & Err.Description
        MsgBox sMsg, vbCritical, sText & Space(1) & sProcName
        Exit FunctionEnd Function
      

  2.   

    我刚刚回复一个类似的
    窗体(三个按钮,运行,检测,关闭)代码:
    Option Explicit
    Private sAppName As String, sAppPath As StringPrivate 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 SubPrivate Sub cmdClose_Click()
        'close application
        
         Call EndTask(sAppName)
    End SubPrivate Sub cmdStart_Click()
        Shell sAppPath, vbMinimizedFocusEnd SubPrivate 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 LongPrivate Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long) As LongPrivate Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
        ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        ByVal lParam As Long) As LongPublic 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 = &H10Public 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 FunctionIsTaskRunning_Eh:
        Call ShowError(sWindowName, "IsTaskRunning")
    End FunctionPublic 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
        
        GoTo EndTaskSucceedEndTaskFail:
        ReturnVal = False
        MsgBox "EndTask: cannot terminate " & sWindowName & " task"
        GoTo EndTaskEndSubEndTaskSucceed:
        ReturnVal = TrueEndTaskEndSub:
        EndTask% = ReturnVal
    End FunctionPublic Function ShowError(sText As String, sProcName As String)
        'this function displays an error that occured
        
        Dim sMsg As String
        sMsg = "Error # " & Str(Err.Number) & " was generated by " _
             & Err.Source & vbCrLf & Err.Description
        MsgBox sMsg, vbCritical, sText & Space(1) & sProcName
        Exit FunctionEnd Function
      

  3.   

    不是shell我还知道用ShellExecute,ShellExecuteEx别的不知道了
    Option ExplicitPrivate 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
      

  4.   

    我上面两个例子结合就是这样
    窗体代码改为
    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 = 1Private 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 SubPrivate Sub cmdClose_Click()
        'close application
        
         Call EndTask(sAppName)
    End SubPrivate Sub cmdStart_Click()
        ShellExecute Me.hwnd, vbNullString, "c:\label.txt", vbNullString, "C:\", SW_SHOWNORMALEnd SubPrivate Sub Form_Load()
        sFilename = "c:\label.txt"
        sAppName = Mid(sFilename, InStrRev(sFilename, "\") + 1) & " - 记事本"
    End Sub模块不变