CommonDialog 控件 简单问题
要求显示open对话框,可多选,长文件名,现返回文件名
代码如下:
ComDialogFax.Flags = cdlOFNAllowMultiselect Or cdlOFNExplorer Or cdlOFNLongNames
'ComDialogFax.Flags = cdlOFNLongNames
ComDialogFax.Filter = "All Excel Files (*.xls)|*.xls|All files (*.*)|*.*"
ComDialogFax.FilterIndex = 1
ComDialogFax.InitDir = "C:\"
ComDialogFax.filename = ""
ComDialogFax.CancelError = False
ComDialogFax.ShowOpen
MsgBox ComDialogFax.FileTitle
MsgBox ComDialogFax.filename
当选择的文件多于一个时
返回的fileTitle为空
filename为路径名,均没有正常返回所选择的文件名
此处如何返回多个文件名,如何提取出每个文件的文件名
问题点数:20、回复次数:6Top
1 楼James_zhjian(听箫小筑)回复于 2005-09-19 10:11:20 得分 0
MsgBox ComDialogFax.FileTitle 取不出来文件的名字
ComDialogFax.filename可以取得文件的名字,但是格式不太一样
c:\~file1.xls~file2.xls
~符号是另外一个特别的符号,我不认识,反正不太一样。在msgbox的时候这个符号之后的东西就显示不出来,只是谈出c:\ 那么怎么能取得文件名呢Top
2 楼fishzone(阿愚@脸上有疤)回复于 2005-09-19 10:15:15 得分 9
'用api
Option Explicit
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Declare Function GetCurrentThreadId Lib "KERNEL32" () As Long
Const GWL_HINSTANCE = (-6)
Const SWP_NOSIZE = &H1
Const SWP_NOZORDER = &H4
Const SWP_NOACTIVATE = &H10
Const HCBT_ACTIVATE = 5
Const WH_CBT = 5
Dim hHook As Long
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
Public Const OFN_ALLOWMULTISELECT = &H200
Public Const OFN_CREATEPROMPT = &H2000
'Public Const OFN_ENABLEHOOK = &H20
'Public Const OFN_ENABLETEMPLATE = &H40
'Public Const OFN_ENABLETEMPLATEHANDLE = &H80
Public Const OFN_EXPLORER = &H80000
'Public Const OFN_EXTENSIONDIFFERENT = &H400
Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_LONGNAMES = &H200000
'Public Const OFN_NOCHANGEDIR = &H8
'Public Const OFN_NODEREFERENCELINKS = &H100000
'Public Const OFN_NOLONGNAMES = &H40000
'Public Const OFN_NONETWORKBUTTON = &H20000
'Public Const OFN_NOREADONLYRETURN = &H8000
Public Const OFN_NOTESTFILECREATE = &H10000
'Public Const OFN_NOVALIDATE = &H100
'Public Const OFN_OVERWRITEPROMPT = &H2
'Public Const OFN_PATHMUSTEXIST = &H800
'Public Const OFN_READONLY = &H1
'Public Const OFN_SHAREAWARE = &H4000
'Public Const OFN_SHAREFALLTHROUGH = 2
'Public Const OFN_SHAREWARN = 0
'Public Const OFN_SHARENOWARN = 1
'Public Const OFN_SHOWHELP = &H10
'Public Const OFS_MAXPATHNAME = 256
'Public Const LF_FACESIZE = 32
'OFS_FILE_OPEN_FLAGS and OFS_FILE_SAVE_FLAGS below
'are mine to save long statements; they're not
'a standard Win32 type.
Public Const OFS_FILE_OPEN_FLAGS = OFN_EXPLORER Or OFN_LONGNAMES Or OFN_HIDEREADONLY Or OFN_ALLOWMULTISELECT Or OFN_CREATEPROMPT Or OFN_FILEMUSTEXIST Or OFN_NOTESTFILECREATE Or OFN_FILEMUSTEXIST Or OFN_NOTESTFILECREATE
Public Type OPENFILENAME
nStructSize As Long
hwndOwner As Long
hInstance As Long
sFilter As String
sCustomFilter As String
nCustFilterSize As Long
nFilterIndex As Long
sFile As String
nFileSize As Long
sFileTitle As String
nTitleSize As Long
sInitDir As String
sDlgTitle As String
flags As Long
nFileOffset As Integer
nFileExt As Integer
sDefFileExt As String
nCustDataSize As Long
fnHook As Long
sTemplateName As String
End Type
Public Type SelectedFile
nFilesSelected As Integer
sFiles() As String
sLastDirectory As String
bCanceled As Boolean
End Type
Public FileDialog As OPENFILENAME
Dim ParenthWnd As Long
'打开多个文件
Public Function ShowOpen(ByVal hWnd As Long, Optional ByVal centerForm As Boolean = True) As SelectedFile
Dim Ret As Long
Dim Count As Integer
Dim fileNameHolder As String
Dim LastCharacter As Integer
Dim NewCharacter As Integer
Dim tempFiles(1 To 200) As String
Dim hInst As Long
Dim Thread As Long
ParenthWnd = hWnd
FileDialog.nStructSize = Len(FileDialog)
FileDialog.hwndOwner = hWnd
FileDialog.sFileTitle = Space$(2048)
FileDialog.nTitleSize = Len(FileDialog.sFileTitle)
FileDialog.sFile = "" & Space$(2047) & Chr$(0)
FileDialog.nFileSize = Len(FileDialog.sFile)
If FileDialog.flags = 0 Then
FileDialog.flags = OFS_FILE_OPEN_FLAGS
End If
'Set up the CBT hook
hInst = GetWindowLong(hWnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
Ret = GetOpenFileName(FileDialog)
If Ret Then
If Trim(FileDialog.sFileTitle) = "" Then
LastCharacter = 0
Count = 0
While ShowOpen.nFilesSelected = 0
NewCharacter = InStr(LastCharacter + 1, FileDialog.sFile, Chr$(0), vbTextCompare)
If Count > 0 Then
tempFiles(Count) = Mid$(FileDialog.sFile, LastCharacter + 1, NewCharacter - LastCharacter - 1)
Else
ShowOpen.sLastDirectory = Mid$(FileDialog.sFile, LastCharacter + 1, NewCharacter - LastCharacter - 1)
End If
Count = Count + 1
If InStr(NewCharacter + 1, FileDialog.sFile, Chr$(0), vbTextCompare) = InStr(NewCharacter + 1, FileDialog.sFile, Chr$(0) & Chr$(0), vbTextCompare) Then
tempFiles(Count) = Mid$(FileDialog.sFile, NewCharacter + 1, InStr(NewCharacter + 1, FileDialog.sFile, Chr$(0) & Chr$(0), vbTextCompare) - NewCharacter - 1)
ShowOpen.nFilesSelected = Count
End If
LastCharacter = NewCharacter
Wend
ReDim ShowOpen.sFiles(1 To ShowOpen.nFilesSelected)
For Count = 1 To ShowOpen.nFilesSelected
ShowOpen.sFiles(Count) = tempFiles(Count)
Next
'************************************************************** i write this elseif only for the use of win2000
ElseIf Asc(Trim(FileDialog.sFileTitle)) = 0 Then
LastCharacter = 0
Count = 0
While ShowOpen.nFilesSelected = 0
NewCharacter = InStr(LastCharacter + 1, FileDialog.sFile, Chr$(0), vbTextCompare)
If Count > 0 Then
tempFiles(Count) = Mid$(FileDialog.sFile, LastCharacter + 1, NewCharacter - LastCharacter - 1)
Else
ShowOpen.sLastDirectory = Mid$(FileDialog.sFile, LastCharacter + 1, NewCharacter - LastCharacter - 1)
End If
Count = Count + 1
If InStr(NewCharacter + 1, FileDialog.sFile, Chr$(0), vbTextCompare) = InStr(NewCharacter + 1, FileDialog.sFile, Chr$(0) & Chr$(0), vbTextCompare) Then
tempFiles(Count) = Mid$(FileDialog.sFile, NewCharacter + 1, InStr(NewCharacter + 1, FileDialog.sFile, Chr$(0) & Chr$(0), vbTextCompare) - NewCharacter - 1)
ShowOpen.nFilesSelected = Count
End If
LastCharacter = NewCharacter
Wend
ReDim ShowOpen.sFiles(1 To ShowOpen.nFilesSelected)
For Count = 1 To ShowOpen.nFilesSelected
ShowOpen.sFiles(Count) = tempFiles(Count)
Next
'**************************************************************** end write
Else
ReDim ShowOpen.sFiles(1 To 1)
ShowOpen.nFilesSelected = 1
'begin i write this
Dim n As Integer
For n = 0 To Len(FileDialog.sFile)
If Mid$(FileDialog.sFile, Len(FileDialog.sFile) - n, 1) = "\" Then
ShowOpen.sLastDirectory = Mid$(FileDialog.sFile, 1, Len(FileDialog.sFile) - n)
Exit For
End If
Next n
ShowOpen.sFiles(1) = Mid$(FileDialog.sFile, Len(FileDialog.sFile) - n + 1, n)
'end write
'***********************orginal
'ShowOpen.sLastDirectory = Left$(FileDialog.sFile, FileDialog.nFileOffset)
'ShowOpen.sFiles(1) = Mid$(FileDialog.sFile, FileDialog.nFileOffset + 1, InStr(1, FileDialog.sFile, Chr$(0), vbTextCompare) - FileDialog.nFileOffset - 1)
'**************************
End If
ShowOpen.bCanceled = False
Exit Function
Else
ShowOpen.sLastDirectory = ""
ShowOpen.nFilesSelected = 0
ShowOpen.bCanceled = True
Erase ShowOpen.sFiles
Exit Function
End If
End Function
Top
3 楼weiweiplay(虚幻)回复于 2005-09-19 10:26:12 得分 2
ComDialogFax.FileName 可以取出,但是无法显示出来,其实返回的是
C:\file1.xlsfile2.xls
Top
4 楼wangdeshui(阿水)回复于 2005-09-19 10:42:05 得分 2
分解字符串Top
5 楼James_zhjian(听箫小筑)回复于 2005-09-19 10:51:50 得分 0
文件名传回的字符串中含有一个不知道是什么的字符,那个字符怎么分?
Top
6 楼weiweiplay(虚幻)回复于 2005-09-19 10:59:02 得分 7
Private Sub Command1_Click()
Dim i As Integer, title As String, FileNames As String
ComDialogFax.Flags = cdlOFNAllowMultiselect Or cdlOFNExplorer Or cdlOFNLongNames
ComDialogFax.Filter = "All Excel Files (*.xls)|*.xls|All files (*.*)|*.*"
ComDialogFax.FilterIndex = 1
ComDialogFax.InitDir = "C:\"
ComDialogFax.FileName = ""
ComDialogFax.CancelError = False
ComDialogFax.ShowOpen
i = InStrRev(ComDialogFax.FileName, "\") '
title = Left(ComDialogFax.FileName, i) ' Ŀ¼
FileNames = Mid(ComDialogFax.FileName, i + 1) ' all selectes files
Do While Len(FileNames) > 0
title = GetLeftWords(FileNames, Chr(0))
MsgBox title
Loop
End Sub
Function GetLeftWords(s As String, ByVal Ch As String) As String
Dim i As Long
i = InStr(s, Ch)
If i > 0 Then
GetLeftWords = Left(s, i - 1)
s = Mid(s, i + Len(Ch))
Else
GetLeftWords = s
s = vbNullString
End If
End Function
Top




