Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OpenSaveDialog: defExt - default extension
#1
How do I set the default extension to: *.txt, if the filter is:

"exe files[]*.exe[]txt files[]*.txt[]All Files[]*.*[]"

Macro Macro25
Code:
Copy      Help
str defExt
str s
if OpenSaveDialog(0 s "exe files[]*.exe[]txt files[]*.txt[]All Files[]*.*[]" defExt )
,out s

I tried the following, but it still keeps the .exe as default extension

Macro Macro25
Code:
Copy      Help
defExt="txt"
defExt=".txt"
defExt="*.txt"
defExt="txt files"

I assume that normally only folders and .exe are displayed when the opensave dialog is opened, if the below filter is used:
Code:
Copy      Help
"exe files[]*.exe[]txt files[]*.txt[]All Files[]*.*[]"
And with 'defExt' I can set another extension to be shown when the opensave dialog is opened.
Alternatively I could set the the 'txt' extension first in the filter, but I was wondering how to use 'defExt'
#2
defExt is used for when a user types a file name but forgets to put the extension type and clicks the open button or save button if you are using the save mode

lpstrDefExt
Type: LPCTSTR
The default extension. GetOpenFileName and GetSaveFileName append this extension to the file name if the user fails to type an extension. This string can be any length, but only the first three characters are appended. The string should not contain a period (.). If this member is NULL and the user fails to type an extension, no extension is appended.

Function Function1
Code:
Copy      Help
str defExt="txt"
str s
if OpenSaveDialog(0 s "txt files" defExt)
,out s

for more info
https://msdn.microsoft.com/en-us/library...85%29.aspx
#3
Ah ok!
Thank you!
#4
Added defFilter parameter. See EXAMPLE.

Function OpenSaveDialog3
Code:
Copy      Help
;/
function# save str&s [$filter] [$defExt] [str&initDir] [$title] [noDereferenceLinks] [ARRAY(str)&multi] [hwndOwner] [defFilter] ;;save: 0 open, 1 save;  filter example: "Text files[]*.txt[]All Files[]*.*[]".

;Shows "Open" or "Save As" dialog for selecting a file.
;Returns 1 on Open/Save, 0 on Cancel.

;save - dialog type: 0 - "Open", 1 - "Save As".
;s - str variable that receives full path.
;;;If not empty on input:
;;;;;Sets initial content of the file name field.
;;;;;If full path, also sets initial directory.
;;;;;;;QM 2.3.3. Supports $special folders$.
;filter - file types displayed in dialog. Consists of description/pattern string pairs. See examples.
;defExt - default extension. The dialog uses it when the user types a filename without extension and does not select a file type from filter.
;initDir - str variable that sets initial directory and receives selected file's directory.
;;;The function may change the current directory, but if initDir is used, restores it when the dialog is closed.
;title - dialog box title.
;noDereferenceLinks - get path of shortcut file, not its target.
;multi - if used, Open dialog allows to select multiple files, and multi is populated with full paths. s can be 0.
;hwndOwner (QM 2.3.0) - handle of owner window. It will be disabled.
;;;If omitted or 0, will use the active window, if it belongs to current thread. To create unowned dialog, use GetDesktopWindow for hwndOwner.
;defFilter - 0-based index of the file type in filter to select initially.

;REMARKS
;This function fills an <google>OPENFILENAME structure</google> and calls Windows API GetOpenFileName or GetSaveFileName.

;EXAMPLE
;str s
;if OpenSaveDialog3(0 s "Text files[]*.txt[]Image files[]*.bmp;*.gif[]All Files[]*.*[]" "" 0 0 0 0 0 2)
;,out s


int multisel i j k
if(&multi) multi=0; if(!save) multisel=1

BSTR b.alloc(iif(multisel 30000 300))
if(!&s) &s=_s
if(s.len) lstrcpynW b.pstr @s.expandpath b.len
s=""

OPENFILENAMEW op.lStructSize=sizeof(op)
if(!hwndOwner) hwndOwner=win; if(GetWindowThreadProcessId(hwndOwner 0)!=GetCurrentThreadId) hwndOwner=0
op.hwndOwner=hwndOwner
op.lpstrFile=b
op.nMaxFile=b.len
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
,op.lpstrInitialDir=@initDir
,str curdir=GetCurDir

str sf=filter
sf.findreplace("[]" "" 16) ;;if as separators used newline characters, replace them to null characters
sf.fromn(sf sf.len "" 1) ;;maybe forgot to append null or newline
op.lpstrFilter=@sf
op.nFilterIndex=defFilter+1

if(save) i=GetSaveFileNameW(&op)
else i=GetOpenFileNameW(&op)
0
if(!i) ret

if(multisel)
,if(b[op.nFileOffset-1]) multi[].ansi(b) ;;single
,else
,,str sp.ansi(b) sn
,,for j op.nFileOffset b.len
,,,k=lstrlenW(&b[j]); if(!k) break
,,,sn.ansi(&b[j])
,,,multi[].fromn(sp sp.len "\" 1 sn sn.len)
,,,j+k
else s.ansi(b)

if(&initDir)
,initDir=GetCurDir
,if(curdir.len) SetCurDir curdir; err

ret 1
#5
wow!
Thank you!


Forum Jump:


Users browsing this thread: 1 Guest(s)