Attribute VB_name = "Module1" Option Explicit Public 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 Declare Function Getopenfilename lib "comdlg32.dll" Alias "GetopenfilenameA" (pOpenfilename As OPENfilename) As Long Public sfilename() As String '用于打开文件时的数组 Function Openfiles(ByVal hwnd As Long,ByVal sTitle As String,ByVal sstyle As String,ByVal uFlag As Long,Optional lMaxfileNum As Long) As String '说明: 'hwnd 指窗体的HWND值 'sTitle 指对话框的标题 'sTytle 指打开文件的格式,如:*.exe或者*.*,多个可以使用分号隔离 'uFlag 指是否可以选择多个文件,1可以,其他就只能选一个 'lMaxfileNum 文件个数,长度的数值,一般是255,如果文件多,可以使用65535 Dim ofn As OPENfilename Const OFN_ALLOWMulTISELECT = &H200 Const OFN_EXPLORER = &H80000 Const OFN_fileMUSTEXIST = &H1000 If Len(Trim(Str(lMaxfileNum))) = 0 Then lMaxfileNum = 255 uFlag = IIf(uFlag = 1,OFN_ALLOWMulTISELECT Or OFN_EXPLORER Or OFN_fileMUSTEXIST,OFN_EXPLORER Or OFN_fileMUSTEXIST) With ofn .lStructSize = Len(ofn) .hwndOwner = hwnd .hInstance = App.hInstance .lpstrfile = Space$(lMaxfileNum - 1) .nMaxfile = lMaxfileNum .lpstrfileTitle = Space$(lMaxfileNum - 1) .nMaxfileTitle = lMaxfileNum .lpstrTitle = sTitle .lpstrFilter = "All Surported files" + Chr$(0) + sstyle + Chr$(0) .flags = uFlag End With Dim lRet As Long lRet = Getopenfilename(ofn) Openfiles = IIf(lRet > 0,ofn.lpstrfile,"") End Function Function Getfilenames(ByVal tmpString As String) As Integer Dim filenames() As String filenames() = Split(tmpString,vbNullChar) If UBound(filenames()) < 3 Then ReDim sfilename(0) sfilename(0) = filenames(0) '如果只是选了一个 Getfilenames = 0 Else Dim m As Integer Getfilenames = UBound(filenames) - 3 ReDim sfilename(0 To Getfilenames) For m = 0 To Getfilenames sfilename(m) = IIf(Right(filenames(0),1) = "/",filenames(0) + filenames(m + 1),filenames(0) + "/" + filenames(m + 1)) Next End If End Function '以下是在窗体调用 Sub main() Dim tString As String tString = Openfiles(0,"打开","*.*",1,255) If tString <> "" Then Dim ifileNum As Integer,i As Integer ifileNum = Getfilenames(tString) For i = 0 To ifileNum DeBUG.Print sfilename(i) Next End If End Sub |
评论列表(0条)