Open File Dlg with multiple selections

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Open File Dlg with multiple selections

Post by bcohio2001 »

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
EDIT: Forgot to check for only one file selected.
Last edited by bcohio2001 on Jun 30, 2019 22:22, edited 1 time in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Open File Dlg with multiple selections

Post by D.J.Peters »

Thank you for sharing.
A Windows only API call (the most of us known) isn't a real FreeBASIC trick nor tip
and should be moved by a forum mod in the Windows only section.

Joshy
TheRaven
Posts: 10
Joined: Mar 09, 2019 18:42

Re: Open File Dlg with multiple selections

Post by TheRaven »

instead of relocation of the thread, the topic could branch into the discussion of modularity addressing what's needed to make the source portable to Linux & BSD (even MacOS) while maintaining source integrity (making it very BASIC like).
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Open File Dlg with multiple selections

Post by nimdays »

The single selection is fine, however the multiple selection doesn't show the names after the dialog is closed
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: Open File Dlg with multiple selections

Post by bcohio2001 »

Fixed.

Line 78 should be:

Code: Select all

For x = 1 To FileCount - 1
Post Reply