Main Menu

Windows File Dialog

Started by empty, April 23, 2005, 01:48:12 PM

Previous topic - Next topic

empty

Once finished, this will be a complete Windows Dialogs library.
The goal is to replace the current PBDialog library so that the external DLL is not required anymore. At this point I've only implemented the File Dialog. Here's a little demo.

WORKS ONLY WITH VERSION 1.068 BETA OR HIGHER!

PlayBASIC Code: [Select]
// Declarations ---------------------------------------------------------------
Constant OFN_ALLOWMULTISELECT = $00000200
Constant OFN_CREATEPROMPT = $00002000
Constant OFN_ENABLEHOOK = $00000020
Constant OFN_ENABLETEMPLATE = $00000040
Constant OFN_ENABLETEMPLATEHANDLE = $00000080
Constant OFN_EXPLORER = $00080000
Constant OFN_EXTENSIONDIFFERENT = $00000400
Constant OFN_FILEMUSTEXIST = $00001000
Constant OFN_HIDEREADONLY = $00000004
Constant OFN_LONGNAMES = $00200000
Constant OFN_NOCHANGEDIR = $00000008
Constant OFN_NODEREFERENCELINKS = $00100000
Constant OFN_NOLONGNAMES = $00040000
Constant OFN_NONETWORKBUTTON = $00020000
Constant OFN_NOREADONLYRETURN = $00008000
Constant OFN_NOTESTFILECREATE = $00010000
Constant OFN_NOVALIDATE = $00000100
Constant OFN_OVERWRITEPROMPT = $00000002
Constant OFN_PATHMUSTEXIST = $00000800
Constant OFN_READONLY = $00000001
Constant OFN_SHAREAWARE = $00004000
Constant OFN_SHOWHELP = $00000010

Type TFileDialog
Filter$, FilterIndex, File$
InitialDir$, Title$, Flags
DefExt$, SaveDlg
EndType

Type tagOPENFILENAME
StructSize, hwndOwner, hInstance, Filter$
CustomFilter, MaxCustFilter, FilterIndex
File, MaxFile, FileTitle$, MaxFileTitle
InitialDir$, Title$, Flags, FileOffset_FileExtension
DefExt$, CustData, Hook, TemplateName
EndType

Dim FileDlg_Struct As tagOPENFILENAME



// Execute Dialog -------------------------------------------------------------
Function FileDialog_Execute(FDLG.TFileDialog)
Local Result
Local i
Local BFile
Local Temp$ = FDLG.Filter$
FileDlg_Struct.Filter$ = ""
For i = 1 To Len(Temp$)
If Mid$(Temp$, i, 1) <> "|"
FileDlg_Struct.Filter$ = FileDlg_Struct.Filter$ + Mid$(Temp$, i, 1)
Else
FileDlg_Struct.Filter$ = FileDlg_Struct.Filter$ + Chr$(0)
EndIf
Next i
FileDlg_Struct.Filter$ = FileDlg_Struct.Filter$ + Chr$(0) + Chr$(0)

FileDlg_Struct.FilterIndex = FDLG.FilterIndex


BFile = GetFreeBank()
Temp$ = FDLG.File$
CreateBank BFile, 1024
FileDlg_Struct.File = GetBankPtr(BFile)
FileDlg_Struct.MaxFile = 1024
For i = 1 To Len(Temp$)
If Mid$(Temp$, i, 1) <> "|"
PokeBankByte BFile, i-1, Asc(Mid$(Temp$, i, 1))
Else
PokeBankByte BFile, i-1, 0
EndIf
Next i

FileDlg_Struct.FilterIndex = FDLG.FilterIndex
FileDlg_Struct.InitialDir$ = FDLG.InitialDir$
FileDlg_Struct.Title$ = FDLG.Title$
FileDlg_Struct.Flags = FDLG.Flags
FileDlg_Struct.DefExt$ = FDLG.DefExt$

Local Kernel32 = GetFreeDll()
LoadDll "Kernel32.dll", Kernel32
Local User32 = GetFreeDll()
LoadDll "User32.dll", User32
Local ComDlg32 = GetFreeDll()
LoadDll "ComDlg32.dll", ComDlg32

FileDlg_Struct.StructSize = 76
FileDlg_Struct.hwndOwner = CallDll(User32, "GetActiveWindow")
FileDlg_Struct.hInstance = CallDll(Kernel32, "GetModuleHandleA", 0)

If FDLG.SaveDlg = 1
Result = CallDll(ComDlg32, "GetSaveFileNameA", FileDlg_Struct.tagOPENFILENAME)
Else
Result = CallDll(ComDlg32, "GetOpenFileNameA", FileDlg_Struct.tagOPENFILENAME)
EndIf

DeleteDll Kernel32 : DeleteDll User32 : DeleteDll ComDlg32


If Result = 0
DeleteBank BFile
Exitfunction Result
EndIf

FDLG.FilterIndex = FileDlg_Struct.FilterIndex

i = 0
Temp$ = ""

While i < 1024
If PeekBankByte(BFile, i) <> 0
Temp$ = Temp$ + Chr$(PeekBankByte(BFile, i))
Else
If PeekBankByte(BFile, i+1) = 0 Then ExitWhile
Temp$ = Temp$ + ";"
EndIf
Inc i
EndWhile
FDLG.File$ = Temp$

DeleteBank BFile

EndFunction Result






// TESTING ----------------------------------------------------------
Dim OpenFile As TFileDialog
OpenFile.Filter$ = "Batch files (*.bat)|*.bat|All files (*.*)|*.*"
OpenFile.InitialDir$ = "C:\"
If FileDialog_Execute(OpenFile.TFileDialog) <> 0
Print "You selected the file: " + OpenFile.File$
Else
Print "You clicked on Cancel"
EndIf
Sync
WaitKey:WaitNoKey

Dim SaveFile As TFileDialog
SaveFile.Filter$ = "Text Files|*.*"
SaveFile.DefExt$ = "txt"
SaveFile.SaveDlg = 1
If FileDialog_Execute(SaveFile.TFileDialog) <> 0
Print "Saved as file: " + SaveFile.File$
Else
Login required to view complete source code



kevin

Empty,

 You can read Null terminated string with PeekBankString

 just give it a size of zero

empty

#2
I know, but when you select multiple files (flags OFN_ALLOWMULTISELECT + OFN_EXPLORER) the buffer that "File" points to contains a list of null terminated strings. The first one is the path, followed by all selected files. If I use PeekBankString the I just get the path. :)

kevin

Hmm, ahh the solution is simple then, disable multi select :)

empty

LOL
How come I didn't think of that? Would have saved me 8 lines of code. :)

Next will be the Font dialog.