我在VB6.0中写了一段公共对话框的程式,可以显示打开文件的控件,但不能打开所选中的问件,请各位大师指点一下,好吗?

解决方案 »

  1.   


    '文件对话框只能获得所选择文件的路径并不能打开文件。
    '测试以下代码,它能打开windows能识别的文件。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 LongPrivate Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
            "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
     
    Private Type OPENFILENAME
         lStructSize As Long
         hwndOwner As Long
         hInstance As Long
         lpstrFilter As String
         lpstrCustomFilter As String
         nMaxCustFilter As Long
         nFilterIndex As Long
         lpstrFile As String
         nMaxFile As Long
         lpstrFileTitle As String
         nMaxFileTitle As Long
         lpstrInitialDir As String
         lpstrTitle As String
         FLAGS As Long
         nFileOffset As Integer
         nFileExtension As Integer
         lpstrDefExt As String
         lCustData As Long
         lpfnHook As Long
         lpTemplateName As String
    End Type
    Private Sub Command1_Click()
        Dim ofn As OPENFILENAME
        Dim rtn As String
        
        ofn.lStructSize = Len(ofn)
        ofn.hwndOwner = Me.hwnd
        ofn.hInstance = App.hInstance
        ofn.lpstrFilter = "TEXT (*.TXT)" & Chr$(0) & "*.TXT" & Chr$(0)
        ofn.lpstrFile = Space(254)
        ofn.nMaxFile = 255
        ofn.lpstrFileTitle = Space(254)
        ofn.nMaxFileTitle = 255
        ofn.lpstrInitialDir = App.Path
        ofn.lpstrTitle = "打开文件"
        ofn.FLAGS = 6148
        
        rtn = GetOpenFileName(ofn)
        
        If rtn >= 1 Then
           ShellExecute Me.hwnd, "open", ofn.lpstrFileTitle, vbNullString, _
                     Left(ofn.lpstrFile, InStrRev(ofn.lpstrFile, "\") - 1), 1    End IfEnd Sub