Please refer the docs: FileSelectFile for parameter usage... Also, refer docs: Gui +OwnDialogs
The functionality is similar to the built-in command except for the last parameter Filter, which will support a multiline filter.
A Filter string should be pipe delimited and a double pipe will preselect the item before it. (Just like we do for a ListBox)
If the first character of Filter string is a Pipe, then items will be shown without extension types,
i.e., AHK files (*.ahk) will be displayed as AHK files.. yet the filter would work.
There are a few additional options which has been documented in second post.
Here is an example call:
Code: Select all
#NoEnv
#SingleInstance, Force
Menu, Tray, Icon, user32.dll, 5
Gui +OwnDialogs
Filter := "Scripts (*.ahk)|Interpreter (AutoHotkey*.exe)||Help (AutoHotkey.chm)|GPL (License.txt)|All (*.*)"
MsgBox % FileSelectFile("m35", A_AhkPath, "AutoHotkey files", Filter)
; Paste FileSelectFile() below
If the first character of Filter string is pipe,
for eg. |Scripts (*.ahk)|Interpreter (AutoHotkey*.exe)||Help (AutoHotkey.chm)|GPL (License.txt)|All (*.*)
then the drop down would look like this:
The function:
Code: Select all
FileSelectFile( Options:="", RootDir:="", Title:="", Filter:="" ) { ; v0.72 by SKAN on D39R/D39U
Local ; @ tiny.cc/fileselectfile
Option := StrSplit(Options, "|", A_Space) ; Options parameter
AltWnd := Round(Option[4]), xFlags := Round(Option[3]), DefExt := Trim(Option[2], ". ")
Main := Option[1], o := SubStr(Main, 1, 1), f := Round( SubStr(Main, (o="S" || o="M" ? 2 : 1)) )
nFlags := ( (OFN_ENABLEXPLORER :=0x80000) | (f&1 ? (OFN_FILEMUSTEXIST :=0x001000) : 0)
| (f&2 ? (OFN_PATHMUSTEXIST :=0x00800) : 0) | (f&8 ? (OFN_CREATEPROMPT :=0x002000) : 0)
| (f&16 ? (OFN_OVERWRITEPROMPT :=0x00002) : 0) | (f&32 ? (OFN_NODEREFERENCELINKS:=0x100000) : 0)
| (o="M" ? (OFN_ALLOWMULTISELECT:=0x00200) : 0) | xFlags )
q := ( (nFlags & (OFN_OVERWRITEPROMPT := 0x00002)) && !(nFlags & (OFN_CREATEPROMPT := 0x002000)) )
GetFileName := ( o="S" || q ) ? "comdlg32.dll\GetSaveFileName" : "comdlg32.dll\GetOpenFileName"
RootDir .= InStr(FileExist(RootDir), "D", 1) ? "\" : "" ; RootDir parameter
SplitPath, RootDir, FileName, InitialDir
FileName := (InitialDir="::" ? "" : FileName) ; ignore CLSID
nMaxFile := VarSetCapacity(File, o="M" ? 65536:1024, 0), StrPut(FileName, &File)
Title := StrLen(Title) ? Title : "Select File - " . A_ScriptName ; Title parameter
Filter := ( ( df:=(Asc( f := Filter)=124) ) ? LTrim(f,"|") : f ) ; Filter parameter
n := InStr(Filter, "||",, 0)
StrReplace( StrReplace( SubStr((f := Filter), 1, n), "||", "|" ), "|", "|", nFilterIndex )
Loop, Parse, % ( Filter ? Filter : "All files (*.*)|Text Documents (*.txt)" ), |, % " " . f := ""
If ( (n := InStr( (l := A_LoopField), "(",, 0))
&& (v := StrReplace( Trim(SubStr(l, n), "( )"), " ")) )
f .= Format(df ? "{1:}|{2:}|" : "{1:}({2:})|{2:}|", SubStr(l, 1, n-1), v)
Filter := (f . "|"), x := (A_IsUnicode ? 2 : 1), Null := 0
While ( f := InStr(Filter, "|",, 0) )
NumPut(Null, Filter, (f*x)-x, "Char")
DetectHiddenWindows, % ("On", DHW := A_DetectHiddenWindows) ; Setting owner for the dialog
SetWinDelay, % (0, SWD := A_WinDelay)
SplashImage, 8:, x0 y0 w0 h0 B Hide,,, OwnDialogTest
WinWait, OwnDialogTest ahk_class AutoHotkey2,, 10
hOwner := DllCall("GetWindow", "Ptr",WinExist(), "Int",GW_OWNER:=4 ,"Ptr")
SplashImage, 8:Off
hOwner := (hOwner!=A_ScriptHwnd) ? hOwner : WinExist("ahk_id" . AltWnd)
SetWinDelay, %SWD%
DetectHiddenWindows, %DHW%
P8 := (A_PtrSize=8), VarSetCapacity(OFN, P8 ? 168 : 96, 0) ; Creating OPENFILENAME Struct
NumPut(P8 ? 136 : 76, OFN, "Int")
NumPut(hOwner, OFN, P8 ? 08 : 04, "Ptr"), NumPut(&Filter, OFN, P8 ? 24 : 12, "Ptr")
NumPut(nFilterIndex, OFN, P8 ? 44 : 24, "Int"), NumPut(&File, OFN, P8 ? 48 : 28, "Ptr")
NumPut(nMaxFile, OFN, P8 ? 56 : 32, "Int"), NumPut(&InitialDir, OFN, P8 ? 80 : 44, "Ptr")
NumPut(&Title, OFN, P8 ? 88 : 48, "Ptr"), NumPut(nFlags, OFN, P8 ? 96 : 52, "Int")
NumPut((StrLen(DefExt) ? &DefExt : 0), OFN, P8 ? 104 : 60, "Int")
f := (hOwner ? DllCall("SetForegroundWindow", "Ptr",hOwner) : 0) ; Calling the function
If !DllCall(GetFileName, "Ptr",&OFN)
Return ("", ErrorLevel := 1)
OutputVar := "", f := &File ; Extracting file(s) list
While ( Line := StrGet(f) )
OutputVar .= Line . "`n", f += ( (StrLen(Line)+1) * x )
DllCall("SetLastError", "Int",NumGet(OFN, P8 ? 44 : 24, "UInt"))
Return (Rtrim(OutputVar, "`n"), ErrorLevel := 0)
}
0.72 : A_LastError will contain the index of selected Filter. (Valid only if ErrorLevel is 0). (More)
0.69 : FileSelectFile() Options enhanced to be consistent with FileSelectFile command.
0.66 : Support for Gui +OwnDialogs. Added a feature to suppress extension types from showing in dropdown list.
0.63 : Fixed a glitch that prevented Dialog from showing in 64bit AHK.
0.60 : First release