Code: Select all
#Include "windows.bi"
#Include "crt.bi"
#Include "win\commdlg.bi"
Function FileSel(hWnd As HWND, DefaultPath As ZString Ptr, SelFiles() As String, FTypes As String="Any File (*.*)|*.*") As Long
Dim ofn As OPENFILENAME
Dim filename As ZString * MAX_PATH+1
Dim strFilter As ZString Ptr
Dim As Long strPos, lenFilter
'
' remember filter arguments length
lenFilter = Len(FTypes)
' copy from Filter argument into allocated strFilter
strFilter = malloc(lenFilter + 2)
StrCpy(strFilter, FTypes)
' needs to be double null terminated
strFilter[lenFilter + 1] = 0
' swap '|' for '\0'
For strPos = 0 To lenFilter - 1
If strFilter[strPos] = Asc("|") Then
strFilter[strPos] = 0
EndIf
Next strPos
With ofn
.lStructSize = SizeOf( OPENFILENAME )
.hwndOwner = hWnd
.hInstance = GetModuleHandle( NULL )
.lpstrFilter = strFilter
.lpstrCustomFilter = NULL
.nMaxCustFilter = 0
.nFilterIndex = 1
.lpstrFile = @filename
.nMaxFile = SizeOf( filename )
.lpstrFileTitle = NULL
.nMaxFileTitle = 0
.lpstrInitialDir = DefaultPath
.lpstrTitle = @"Select File(s)"
.Flags = OFN_EXPLORER Or OFN_FILEMUSTEXIST Or OFN_PATHMUSTEXIST Or OFN_NOCHANGEDIR Or OFN_ALLOWMULTISELECT
.nFileOffset = 0
.nFileExtension = 0
.lpstrDefExt = NULL
.lCustData = 0
.lpfnHook = NULL
.lpTemplateName = NULL
End With
'
lenFilter = 0 'array size
If GetOpenFileName( @ofn ) = FALSE Then
ReDim SelFiles(0) 'no selections
Else
strPos = 0 'position to look
Do
ReDim Preserve SelFiles(lenFilter)
While filename[strPos] > 0
SelFiles(lenFilter) += Chr(filename[strPos])
strPos += 1
Wend
lenFilter += 1
strPos += 1
Loop Until filename[strPos] = 0
EndIf
' free memory from our derived filter
free(strFilter)
Return lenFilter
End Function
'test for FileSel()
Dim As Long FileCount, x
Dim As String FilesSel(Any)
FileCount = FileSel(NULL, ExePath+"\", FilesSel())
If FileCount = 0 Then
Print "No files selected"
ElseIf FileCount = 1 Then
'only one file selected (returned as full path)
Print "File: "; FilesSel(0)
Else
Print "Folder: "; FilesSel(0)
For x = 1 To FileCount - 1
Print FilesSel(x) 'file name only
Next
EndIf
GetKey