04-25-2005, 04:26 PM
This function allows to select multiple files.
Function OpenSaveDialog2
Example:
Function OpenSaveDialog2
;/
function# save ~&s [~filter] [$defext] [~&initdir] [$title] [nodereferencelinks] [ARRAY(str)&multi] ;;save: 0 open, 1 save; filter example: "Text files[]*.txt[]All Files[]*.*[]"
;Shows "Open" or "Save As" dialog and stores full path into
;str variable s. Returns 1 on Open or Save; 0 on Cancel.
;save – dialog type: 0 – "Open", 1 – "Save As".
;s – str variable that receives full path.
;filter – file types displayed in dialog. Consists of
;,description/pattern string pairs. Each string must end
;,with new line characters or null character.
;defext – default extension.
;initdir – str variable that sets initial directory
;,and receives selected file directory.
;;;;If initdir is not used, initial directory will be current
;;;;directory, and current directory may be changed. If initdir
;;;;is used, current directory will not be changed.
;title – dialog box title.
;nodereferencelinks - get path of shortcut file, not its target.
;multi - if used, Open dialog allows to select multiple files.
;;;;multi is populated with full paths. s can be 0.
;EXAMPLES
;str s
;if OpenSaveDialog2(0 &s "Text files[]*.txt[]All Files[]*.*[]" "txt")
;,out s
;ARRAY(str) a
;if(OpenSaveDialog2(0 0 "" "" 0 "" 0 a))
,;for(int'i 0 a.len) out a[i]
;def OFN_READONLY 0x1
def OFN_OVERWRITEPROMPT 0x2
def OFN_HIDEREADONLY 0x4
def OFN_NOCHANGEDIR 0x8
;def OFN_SHOWHELP 0x10
;def OFN_ENABLEHOOK 0x20
;def OFN_ENABLETEMPLATE 0x40
;def OFN_ENABLETEMPLATEHANDLE 0x80
;def OFN_NOVALIDATE 0x100
def OFN_ALLOWMULTISELECT 0x200
;def OFN_EXTENSIONDIFFERENT 0x400
;def OFN_PATHMUSTEXIST 0x800
;def OFN_FILEMUSTEXIST 0x1000
;def OFN_CREATEPROMPT 0x2000
;def OFN_SHAREAWARE 0x4000
def OFN_NOREADONLYRETURN 0x8000
;def OFN_NOTESTFILECREATE 0x10000
;def OFN_NONETWORKBUTTON 0x20000
;def OFN_NOLONGNAMES 0x40000
def OFN_EXPLORER 0x80000
def OFN_NODEREFERENCELINKS 0x100000
;def OFN_LONGNAMES 0x200000
;def OFN_SHAREFALLTHROUGH 2
;def OFN_SHARENOWARN 1
;def OFN_SHAREWARN 0
def FNERR_BUFFERTOOSMALL 0x3003
type OPENFILENAME lStructSize hwndOwner hInstance $lpstrFilter $lpstrCustomFilter nMaxCustFilter nFilterIndex $lpstrFile nMaxFile $lpstrFileTitle nMaxFileTitle $lpstrInitialDir $lpstrTitle flags @nFileOffset @nFileExtension $lpstrDefExt lCustData lpfnHook $lpTemplateName
dll comdlg32
,#GetOpenFileName OPENFILENAME*pOpenfilename
,#GetSaveFileName OPENFILENAME*pOpenfilename
,#CommDlgExtendedError
int multisel
if(&multi) multi.redim; if(!save) multisel=1
if(!&s or multisel) &s=&_s
s.all(iif(multisel 10000 MAX_PATH) 1)
OPENFILENAME op.lStructSize = sizeof(OPENFILENAME)
op.lpstrFile = s
op.nMaxFile = s.nc
op.hwndOwner=win
op.lpstrDefExt=defext
op.lpstrTitle=title
op.flags=OFN_HIDEREADONLY
if(nodereferencelinks) op.flags | OFN_NODEREFERENCELINKS
if(save) op.flags | OFN_OVERWRITEPROMPT | OFN_NOREADONLYRETURN
else if(multisel) op.flags | OFN_ALLOWMULTISELECT|OFN_EXPLORER
if(&initdir)
,initdir.searchpath(initdir "" 1); op.lpstrInitialDir=initdir
,str curdir.fix(GetCurrentDirectory(MAX_PATH curdir.all(MAX_PATH)))
;If as separators used newline characters, replace them to null characters:
rep
,int i=find(filter "[]" i)
,if(i>=0) filter[i]=0; i+1; filter.remove(i 1); else break
op.lpstrFilter = filter
if(save) i=GetSaveFileName(&op)
else i=GetOpenFileName(&op)
if(!i) s=""; ret
if(multisel)
,if(s[op.nFileOffset-1]) multi[multi.redim(1)]=s.lpstr ;;single
,else
,,int j k
,,for j op.nFileOffset s.nc
,,,k=len(s+j); if(!k) break
,,,multi[multi.redim(-1)].fromn(s op.nFileOffset-1 "\" 1 s+j k)
,,,j+k
else s.fix()
if(&initdir)
,initdir.fix(GetCurrentDirectory(MAX_PATH initdir.all(MAX_PATH)))
,if(curdir.len) SetCurrentDirectory(curdir)
ret 1Example:
