Skip to content

Dialog 对话框组件概述

📖 简介

cDialog 是一个 Windows 标准对话框封装类,提供打开文件、保存文件、选择文件夹等常用对话框功能,支持文件过滤、多选等特性。

✨ 主要特性

特性说明
打开文件对话框支持单选、多选、文件过滤
保存文件对话框支持覆盖提示、默认扩展名
选择文件夹对话框支持新样式对话框
预设过滤器文本、图片、文档、代码等预设过滤器
文件路径工具提取路径、文件名、扩展名等
打开文件使用 ShellExecute 打开选中文件

🚀 快速开始

打开文件

vb
Dim dlg As New cDialog

dlg.DialogTitle = "选择文件"
dlg.InitialDir = "C:\Documents"
dlg.AddFilter "文本文件", "*.txt"
dlg.AddFilter "所有文件", "*.*"

Dim filePath As String
filePath = dlg.ShowOpen

If filePath <> "" Then
    Debug.Print "选择的文件: " & filePath
End If

保存文件

vb
Dim dlg As New cDialog

dlg.DialogTitle = "保存文件"
dlg.DefaultExt = "txt"
dlg.FileName = "新建文档.txt"
dlg.AddFilter "文本文件", "*.txt"

Dim savePath As String
savePath = dlg.ShowSave

If savePath <> "" Then
    Debug.Print "保存到: " & savePath
End If

选择文件夹

vb
Dim dlg As New cDialog

dlg.DialogTitle = "选择文件夹"

Dim folderPath As String
folderPath = dlg.ShowBrowseForFolder

If folderPath <> "" Then
    Debug.Print "选择的文件夹: " & folderPath
End If

使用预设过滤器

vb
Dim dlg As New cDialog

' 使用图片过滤器
dlg.SetImageFilters
Dim imgFile As String
imgFile = dlg.ShowOpen

' 使用文档过滤器
dlg.SetDocumentFilters
Dim docFile As String
docFile = dlg.ShowOpen

使用实例

vb
'-----------------------------------------------
' 事件: btnBrowse_Click (浏览按钮)
' 描述: 打开文件选择对话框
'-----------------------------------------------
Private Sub btnBrowse_Click()
    With New cDialog
        .ClearFilter
        .AddFilter "所有文件 (*.*)", "*.*"
        .DialogTitle = "选择要上传的文件"
        .FileMustExist = True
        .PathMustExist = True

        With .SelectFiles()
            If .Count > 0 Then
                mSelectedFile = .Item(1)
                txtFilePath.Text = mSelectedFile
                lblStatus.Caption = "文件大小: " & VBMAN.Formater(FileLen(mSelectedFile)).ReturnFileSize()
                btnUpload.Enabled = True
            End If
        End With
    End With
End Sub

📁 文档导航


最后更新: 2026-05-17

VB6及其LOGO版权为微软公司所有