VB程序员博客

VB程序开发

新手,请大家帮忙!!!!
想实现这样的功能:
1.对话框打开某个文件夹的某种类型的文件,当文件存在时在TEXT1中显示文件名。
2.当没有此种类型文件或不是需要的文件时,可以随便输个字符,在TEXT1中显示文件路径。
以下是程序。
*************************
Private Declare Function GetCurrentDirectory Lib "kernel32" Alias "GetCurrentDirectoryA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private 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
    iFilterIndex 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 Function ShowFileDialog() As String
    Dim ofn As OpenFilename
    ofn.lStructSize = Len(ofn)
    ofn.hwndOwner = hWnd
    ofn.lpstrFilter = "文件名称" + Chr$(0) + "TYSF.*" + Chr$(0)
    ofn.lpstrFile = String(256, 0)
    ofn.nMaxFile = 255
    ofn.lpstrTitle = "打开文件"
    ofn.Flags = &H800000 + &H1000 + &H8 + &H4
    GetOpenFileName ofn
    If Mid(ofn.lpstrFile, 1, 1) <> Chr(0) Then ShowFileDialog = ofn.lpstrFile
End Function

Private Sub Command1_Click()
    Dim FileName As String
    Dim sSave    As String
    Dim path As String
   
    FileName = ShowFileDialog
    If FileName <> "" Then
Text1.Text = FileName

End If
End Sub

也就是说,如果不选择任何文件,而在对话框随便输入一个文件名时,不出现找不到文件的提示而返回文件夹名称。
谢谢。。。

'用这个
Private 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

Public Function ShowOpenBox(hwnd As Long, Caption As String, Optional Filter As String = "所有文件(*.*)|*.*") As String
    Dim Opf As OPENFILENAME
    Dim rtn As Long, lng As Long

    With Opf
        .lStructSize = Len(Opf)
        .lpstrFile = Space(254)
        .nMaxFile = 255
        .lpstrFileTitle = Space(254)
        .nMaxFileTitle = 255
        .flags = 0
        .lpstrInitialDir = App.path
        .hInstance = App.hInstance
        .hwndOwner = hwnd
        .lpstrFilter = Replace(Filter, "|", Chr(0))
        .lpstrTitle = Caption
        rtn = GetOpenFileName(Opf)
        If rtn >= 1 Then
            ShowOpenBox = Trim(.lpstrFile)
        Else
            ShowOpenBox = ""
        End If
    End With
End Function

其实就是把ofn.Flags = &H800000 + &H1000 + &H8 + &H4改为
ofn.Flags =0就不会出现找不到文件的消息框


标签: