在vb6中的类模块中打开文件对话框

时间:2010-05-26 05:41:37

标签: vb6 fileopendialog

我想知道如何在vb6中的类模块中打开文件对话框。我知道如何在表单中进行操作,但我必须在类模块中打开它。

2 个答案:

答案 0 :(得分:1)

请看下面的API:

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
    "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) 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
        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 Function FileOpenDialog()
        Dim sFilter As String

        OpenFile.lStructSize = Len(OpenFile)
        sFilter = "Text Files (*.txt)" & Chr(0) & "*.TXT" & Chr(0)
        OpenFile.lpstrFilter = sFilter
        OpenFile.nFilterIndex = 1
        OpenFile.lpstrFile = String(257, 0)
        OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
        OpenFile.lpstrFileTitle = OpenFile.lpstrFile
        OpenFile.nMaxFileTitle = OpenFile.nMaxFile
        OpenFile.lpstrInitialDir = "C:\"
        OpenFile.lpstrTitle = "Select File"
        OpenFile.flags = 0

        lReturn = GetOpenFileName(OpenFile)


    End Function

答案 1 :(得分:1)

是的,您可以调用API来引发此对话框。

大多数时候,这种需求源于一种破碎的范式。一个类不应该有UI。当你真正需要这个时,你的班级应该是一个UserControl ......问题就会消失。