⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfiledialog.cls

📁 一个简易的c++的编辑器
💻 CLS
字号:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CFileDialog"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'CFileDialog
'打开,保存对话框
'鬼龙之舞
'2005-02-17
'==========================================================================
'[方法]
' ShowOpen
'显示打开对话框
'如果选择了文件,则返回True,选择的文件名在FileName
'如果是多个选择,则文件名在FileNames(Index)

' ShowSave
'保存对话框,同上

'[属性]
'AllowMultiSelect
'设定是否可以多重选择,如果调用了ShowSave,则这个值被重设为False

' FileName
'返回打开或保存选择的文件名

' FileNames(Index)
'返回打开选择的多个文件名,Index=1 to Count

' Filter
'定过滤列表,每个由"|"分开,如 所有文件|*.*|文本文件|*.txt

' MaxFile
'文件名的最大长度,这个值主要是在打开多个文件时才用到,如果调用ShowSave,这个值被重设为255

' Count
'取得选择多个文件时的个数

' InitDir
'起始位置

' Parent
'设定父窗体,对话将以这个窗体为相对位置出现
'==========================================================================
Option Explicit
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public Enum OFN_FLAGS
    OFN_ALLOWMULTISELECT = &H200
    OFN_CREATEPROMPT = &H2000
    OFN_DONTADDTORECENT = &H2000000
    OFN_ENABLEHOOK = &H20
    OFN_ENABLEINCLUDENOTIFY = &H400000
    OFN_ENABLESIZING = &H800000
    OFN_EXPLORER = &H80000
    OFN_EXTENSIONDIFFERENT = &H400
    OFN_EX_NOPLACESBAR = &H1
    OFN_FILEMUSTEXIST = &H1000
    OFN_HIDEREADONLY = &H4
    OFN_LONGNAMES = &H200000
    OFN_NOCHANGEDIR = &H8
    OFN_NODEREFERENCELINKS = &H100000
    OFN_NOLONGNAMES = &H40000
    OFN_NONETWORKBUTTON = &H20000
    OFN_NOREADONLYRETURN = &H8000
    OFN_NOTESTFILECREATE = &H10000
    OFN_NOVALIDATE = &H100
    OFN_OVERWRITEPROMPT = &H2
    OFN_PATHMUSTEXIST = &H800
    OFN_READONLY = &H1
End Enum
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 OFN_FLAGS
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type


Private m_FileName As String
Private m_FileNames() As String
Private m_ofn As OPENFILENAME
Private m_Count As Long
Private m_hOwnerwnd As Long
Public Function ShowOpen() As Boolean
    With m_ofn
    If GetOpenFileName(m_ofn) Then
        ShowOpen = True
        If AllowMultiSelect Then
            Dim i As Long
            i = InStr(.lpstrFile, vbNullChar & vbNullChar) - 1
            .lpstrFile = Left(.lpstrFile, i)
            m_FileNames = Split(.lpstrFile, vbNullChar)
            m_Count = UBound(m_FileNames)
            For i = 1 To m_Count
                m_FileNames(i) = PathAppend(m_FileNames(0), m_FileNames(i))
            Next
            If m_Count = 0 Then
                m_Count = 1
                ReDim Preserve m_FileNames(1)
                m_FileNames(1) = m_FileNames(0)
            End If
        Else
            m_FileName = Left(.lpstrFile, InStr(.lpstrFile, vbNullChar) - 1)
        End If
    End If
    End With
End Function

Private Function PathAppend(strPath As String, strFileName As String) As String
    If Right(strPath, 1) <> "\" Then
        PathAppend = strPath + "\" + strFileName
    Else
        PathAppend = strPath + strFileName
    End If
End Function
Public Function ShowSave(Optional ByVal hOwnerwnd As Long) As Boolean
    With m_ofn
        Me.MaxFile = 255
        Me.AllowMultiSelect = False
        If GetSaveFileName(m_ofn) Then
            ShowSave = True
            m_FileName = Left(.lpstrFile, InStr(.lpstrFile, vbNullChar) - 1)
        End If
    End With
End Function

Public Property Get Flags() As OFN_FLAGS
    Flags = m_ofn.Flags
End Property
Public Property Let Flags(ByVal vNewValue As OFN_FLAGS)
    m_ofn.Flags = vNewValue
End Property


Public Property Let Filter(ByVal vNewValue As String)
    m_ofn.lpstrFilter = Replace(vNewValue, "|", vbNullChar) & vbNullChar & vbNullChar
End Property
Public Property Get Filter() As String
   Filter = Replace(m_ofn.lpstrFilter, vbNullChar, "|")
End Property

Public Property Get FileName() As String
    FileName = m_FileName
End Property


Public Property Get MaxFile() As Long
    MaxFile = m_ofn.nMaxFile
End Property
Public Property Let MaxFile(ByVal vNewValue As Long)
    m_ofn.nMaxFile = vNewValue
    m_ofn.lpstrFile = String(vNewValue, vbNullChar)
End Property

Public Property Let AllowMultiSelect(ByVal bAllow As Boolean)
    If bAllow Then
        m_ofn.Flags = m_ofn.Flags Or OFN_ALLOWMULTISELECT
    Else
        m_ofn.Flags = m_ofn.Flags And Not OFN_ALLOWMULTISELECT
    End If
End Property
Public Property Get AllowMultiSelect() As Boolean
    AllowMultiSelect = (m_ofn.Flags And OFN_ALLOWMULTISELECT) = OFN_ALLOWMULTISELECT
End Property

Public Property Let HideReadOnly(ByVal bAllow As Boolean)
    If bAllow Then
        m_ofn.Flags = m_ofn.Flags Or OFN_HIDEREADONLY
    Else
        m_ofn.Flags = m_ofn.Flags And Not OFN_HIDEREADONLY
    End If
End Property
Public Property Get HideReadOnly() As Boolean
    HideReadOnly = (m_ofn.Flags And OFN_HIDEREADONLY) = OFN_HIDEREADONLY
End Property

Public Property Let Title(ByVal vNewValue As String)
    m_ofn.lpstrTitle = vNewValue
End Property
Public Property Get Title() As String
    Title = m_ofn.lpstrTitle
End Property

Public Property Get FileNames(ByVal nIndex As Long) As String
    FileNames = m_FileNames(nIndex)
End Property

Public Property Get DefaultExt() As String
    DefaultExt = m_ofn.lpstrDefExt
End Property
Public Property Let DefaultExt(ByVal vNewValue As String)
    m_ofn.lpstrDefExt = vNewValue
End Property

Public Property Get FilterIndex() As Long
    FilterIndex = m_ofn.nFilterIndex
End Property
Public Property Let FilterIndex(ByVal vNewValue As Long)
    m_ofn.nFilterIndex = vNewValue
End Property

Public Property Get InitDir() As String
    InitDir = m_ofn.lpstrInitialDir
End Property
Public Property Let InitDir(ByVal vNewValue As String)
    m_ofn.lpstrInitialDir = vNewValue
End Property

Private Sub Class_Initialize()
    m_ofn.lStructSize = Len(m_ofn)
    m_ofn.Flags = OFN_EXPLORER Or OFN_PATHMUSTEXIST Or OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT
    MaxFile = 255
    m_ofn.hInstance = App.hInstance
    Filter = "所有文件|*.*"
End Sub

Public Property Get Count() As Long
    Count = m_Count
End Property
Public Property Let Parent(vNewValue As Form)
    m_ofn.hwndOwner = vNewValue.hwnd
End Property


'示例代码
'    Dim c As New CFileDialog
'    c.AllowMultiSelect = True
'    c.Filter = "所有文件|*.*|程序|*.exe|文本文件|*.txt"
'    c.MaxFile = 255 * 100
'    c.Title = "打开多个文件"
'    If c.ShowOpen Then
'        Dim i As Long
'        For i = 1 To c.Count
'            Debug.Print c.FileNames(i)
'        Next
'    End If
'
'    c.Title = "保存文件"
'    If c.ShowSave Then Debug.Print c.FileName

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -