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

发布于 2024-09-03 03:56:02 字数 60 浏览 5 评论 0原文

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

I want to know how can we open a file dialog from withing a class module in vb6. I know how to do in forms, but I have to open it from within a class module.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘然心甜 2024-09-10 03:56:02

看看下面的 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

Have a look at the API below:

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
    "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

Here's a sample of it in use:

    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
意中人 2024-09-10 03:56:02

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

大多数时候,这种需求源于一个破碎的范式。类不应该有 UI。当您真正需要这个时,您的类可能应该是 UserControl...并且问题就消失了。

Yes, you can call the API to raise this dialog.

Most of the time though this sort of need stems from a broken paradigm. A Class should not have a UI. When you have a real need for this your Class should probably be a UserControl... and the problem goes away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文