Jump to content

FileOpenDialog as Child


Sunaj
 Share

Recommended Posts

this may be better for the msgbox_button() function

;MsgBox(flag, "title", "text", [, timeout])
Func _MsgBox($flag=0, $title="", $text="", $parent="")
    DllCall("user32.dll", _        ; dll mother
            "int", "MessageBox", _ ; dll function
            "hwnd", $parent, _     ; maingui to bind msgbox to (if any)
            "str", $text , _       ; msgbox text
            "str", $title, _       ; msgbox title
            "int", $flag)          ; msgbox type
EndFunc
One further question: does anyone know how to specify the timeout parameter? :)
Link to comment
Share on other sites

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

how to specify the timeout parameter?

This parameter is not standard, it not exists originaly i think (it was added specialy for convinient reasons :))...

See the _GuiMsgBox() function in my app «Desktop Icons Restorator» (from my signature).

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Why won't any of these stuff work for me? :)

-edit-

Mm.. It says the function can't be found in that DLL. Can I just replace my Shell32.dll with one from a computer with WinXP?

Edited by Nahuel
Link to comment
Share on other sites

Ehm, shell32.dll/comdlg32.dll are integral pieces of any Windows installation, so you probably shouldn't be messing around with them (especially not replacing them with a copies from an entirely different OS). These functions have all been around in the DLLs since way before Windows XP (back to Windows 95), so the OS shouldn't be the issue.

Anyhow... Someone else mentioned that they wanted to make a set of UDFs out of these functions, but it never came to be (at least I haven't seen it), so I did some work in that direction...

WindowsDialogs.au3

;===============================================================================
;
; Function Name:   _FileOpenDialog()
; Description:     Identical to FileOpenDialog() with the added ability to set a parent dialog
;
; Parameter(s):    $sTitle       - Title of dialog
;                  $sInitDir     - Initial directory selected
;                  $sFilter      - [optional] File type filters, such as "All files (*.*)" or "Text files (*.txt)|All files (*.*)"
;                  $iOpt         - [optional] Options for the dialog; see help file on FileOpenDialog()
;                  $sDefaultFile - [optional] Suggested file to open
;                  $sDefaultExt  - [optional] Suggested extension to use
;                  $hWnd         - [optional] Handle to parent window
;
; Return Value(s): Success - Returns a string containing the full path to the selected file(s)
;                  Failure - Returns "" and sets @error
;
; Author(s):       amel27
;
;===============================================================================
Func _FileOpenDialog($sTitle, $sInitDir, $sFilter = "All files (*.*)", $iOpt = 0, $sDefaultFile = "", $sDefaultExt = "", $hWnd = 0)
    ; API flags prepare
    Local $iFlag = BitOR( _
        BitShift(BitAND($iOpt, 1), -12), _
        BitShift(BitAND($iOpt, 2), -10), _
        BitShift(BitAND($iOpt, 4), -7 ), _
        BitShift(BitAND($iOpt, 8), -10), _
        BitShift(BitAND($iOpt, 4), -17) _
    )

    ; Call API function
    Local $usPath = __FileOSDialogHelper("GetOpenFileName", $iFlag, $sTitle, $sInitDir, $sFilter, $iOpt, $sDefaultFile, $sDefaultExt, $hWnd)

    If Not @error Then
        If BitAND($iOpt, 4) Then
            $i = 1
            While 1
                If DllStructGetData($usPath, 1, $i) = 0 Then
                    If DllStructGetData($usPath, 1, $i+1) Then 
                        DllStructSetData($usPath, 1, 124, $i)
                    Else
                        ExitLoop
                    EndIf
                EndIf
                $i += 1
            Wend
        EndIf
        Return DllStructGetData($usPath, 1)
    EndIf
    Return SetError(1, 0, "")
EndFunc


;===============================================================================
;
; Function Name:   _FileSaveDialog()
; Description:     Identical to FileSaveDialog() with the added ability to set a parent dialog
;
; Parameter(s):    $sTitle       - Title of dialog
;                  $sInitDir     - Initial directory selected
;                  $sFilter      - [optional] File type filters, such as "All files (*.*)" or "Text files (*.txt)|All files (*.*)"
;                  $iOpt         - [optional] Options for the dialog; see help file on FileSaveDialog()
;                  $sDefaultFile - [optional] Suggested file to open
;                  $sDefaultExt  - [optional] Suggested extension to use
;                  $hWnd         - [optional] Handle to parent window
;
; Return Value(s): Success - Returns a string containing the full path to the selected file
;                  Failure - Returns "" and sets @error
;
; Author(s):       amel27
;
;===============================================================================
Func _FileSaveDialog($sTitle, $sInitDir, $sFilter = "All files (*.*)", $iOpt = 0, $sDefaultFile = "", $sDefaultExt = "", $hWnd = 0)
    ; API flags prepare
    Local $iFlag = BitOR( _
        BitShift(BitAND($iOpt, 2), -10), _
        BitShift(BitAND($iOpt,16),  3 ) _
    )

    ; Call API function
    Local $usPath = __FileOSDialogHelper("GetSaveFileName", $iFlag, $sTitle, $sInitDir, $sFilter, $iOpt, $sDefaultFile, $sDefaultExt, $hWnd)

    If Not @error Then Return DllStructGetData($usPath, 1)
    Return SetError(1, 0, "")
EndFunc

;-------------------------------------------------------------------------------
; File Open/Save Dialog Helper Function (originally written by amel27)
;-------------------------------------------------------------------------------
Func __FileOSDialogHelper($sFunction, $iFlag, $sTitle, $sInitDir, $sFilter, $iOpt, $sDefaultFile, $sDefaultExt, $hWnd)
    Local $iPathLen = 256 ; Max chars in returned string

    ; Filter string to array conversion
    Local $asFLines = StringSplit($sFilter, "|"), $asFilter[$asFLines[0]*2+1]
    Local $i, $iStart, $iFinal, $suFilter
    $asFilter[0] = $asFLines[0]*2
    For $i = 1 To $asFLines[0]
        $iStart = StringInStr($asFLines[$i], "(", 0, 1)
        $iFinal = StringInStr($asFLines[$i], ")", 0,-1)
        $asFilter[$i*2-1] = StringStripWS(StringLeft($asFLines[$i], $iStart-1), 3)
        $asFilter[$i*2] = StringStripWS(StringTrimRight(StringTrimLeft($asFLines[$i], $iStart), StringLen($asFLines[$i]) -$iFinal+1), 3)
        $suFilter &= "char[" & StringLen($asFilter[$i*2-1])+1 & "];char[" & StringLen($asFilter[$i*2])+1 & "];"
    Next

    ; Create API structures
    Local $uOFN      = DllStructCreate("dword;int;int;ptr;ptr;dword;dword;ptr;dword;ptr;int;ptr;ptr;dword;short;short;ptr;ptr;ptr;ptr;ptr;dword;dword")
    Local $usTitle   = DllStructCreate("char[" & StringLen($sTitle)+1 & "]")
    Local $usInitDir = DllStructCreate("char[" & StringLen($sInitDir)+1 & "]")
    Local $usFilter  = DllStructCreate($suFilter & "char")
    Local $usPath    = DllStructCreate("char[" & $iPathLen & "]")
    Local $usExtn    = DllStructCreate("char[" & StringLen($sDefaultExt)+1 & "]")
    For $i=1 To $asFilter[0]
        DllStructSetData($usFilter, $i, $asFilter[$i])
    Next

    ; Set Data of API structures
    DllStructSetData($usTitle, 1, $sTitle)
    DllStructSetData($usInitDir, 1, $sInitDir)
    DllStructSetData($usPath, 1, $sDefaultFile)
    DllStructSetData($usExtn, 1, $sDefaultExt)
    DllStructSetData($uOFN,  1, DllStructGetSize($uOFN))
    DllStructSetData($uOFN,  2, $hWnd)
    DllStructSetData($uOFN,  4, DllStructGetPtr($usFilter))
    DllStructSetData($uOFN,  7, 1)
    DllStructSetData($uOFN,  8, DllStructGetPtr($usPath))
    DllStructSetData($uOFN,  9, $iPathLen)
    DllStructSetData($uOFN, 12, DllStructGetPtr($usInitDir))
    DllStructSetData($uOFN, 13, DllStructGetPtr($usTitle))
    DllStructSetData($uOFN, 14, BitOR($iFlag, 4))
    DllStructSetData($uOFN, 17, DllStructGetPtr($usExtn))
    DllStructSetData($uOFN, 23, BitShift(BitAND($iOpt, 32), 5))

    ; Call API function
    Local $aiCall = DllCall("comdlg32.dll", "int", $sFunction, "ptr", DllStructGetPtr($uOFN))

    If $aiCall[0] Then Return $usPath
    Return SetError(1, 0, "")
EndFunc


;===============================================================================
;
; Function Name:   _FileSelectFolder()
; Description:     Identical to FileSelectFolder() with the added ability to set a parent dialog
;
; Parameter(s):    $sText        - Text of dialog
;                  $sRoot        - Root directory of folder tree
;                  $iOpt         - [optional] Options for the dialog; see help file on FileSelectFolder()
;                  $sDefaultPath - [dummy]
;                  $hWnd         - [optional] Handle to parent window
;
; Return Value(s): Success - Returns a string containing the full path to the selected file
;                  Failure - Returns "" and sets @error
;
; Author(s):       amel27
; Modification(s): Ultima - Cleaned up to UDF standard
;
; Note(s):         $sDefaultPath is a dummy variable that does nothing. It's there
;                  for partial backwards compatibility with FileSelectFolder().
;                  Implementing default path selection would require callback functions
;                  and global variables, which isn't very clean, so I decided to keep
;                  amel27's old FileSelectFolder function rather than the new one. (Ultima)
;
;===============================================================================
Func _FileSelectFolder($sText, $sRoot = "", $iOpt = 0, $sDefaultPath = "", $hWnd = 0)
    Local $iPathLen = 256 ; Max chars in returned string

    ; API flags prepare
    Local $iFlag = BitOR( _
        BitShift(BitAnd($iOpt, 1), -9), _       ; 1: NOT show Create Folder Button
        BitShift(BitAnd($iOpt, 2), -5), _       ; 2: Use New Dialog Style
        BitShift(BitAnd($iOpt, 4), -2) _            ; 4: Show Edit Control
    )

    ; Create API structures
    Local $uBI    = DllStructCreate("hwnd;ptr;ptr;ptr;int;ptr;ptr;int")
    Local $usText = DllStructCreate("char[" & StringLen($sText)+1 & "]")
    Local $usPath = DllStructCreate("char[" & $iPathLen & "]")

    ; Set Data of API structures
    DLLStructSetData($usText, 1, $sText)
    DLLStructSetData($uBI, 1, $hWnd)
    DLLStructSetData($uBI, 3, DLLStructGetPtr($usPath))
    DLLStructSetData($uBI, 4, DLLStructGetPtr($usText))
    DLLStructSetData($uBI, 5, $iFlag)

    ; Call API function
    Local $aiCall

    $aiCall = DllCall("shell32.dll", "ptr", "SHGetSpecialFolderLocation", "int", 0 , "int", $sRoot , "ptr", DllStructGetPtr($uBI, 2))
    If $aiCall[0] Then Return ""

    $aiCall = DllCall("shell32.dll", "ptr", "SHBrowseForFolder", "ptr", DllStructGetPtr($uBI))
    If $aiCall[0] Then
        $aiCall = DLLCall("shell32.dll", "int", "SHGetPathFromIDList", "ptr", $aiCall[0], "ptr", DLLStructGetPtr($usPath))
        DLLCall("ole32.dll", "int", "CoTaskMemFree", "ptr", $aiCall[0]) ; clear memory
        If $aiCall[0] Then $aiCall = DLLStructGetData($usPath, 1)
    EndIf
    DllCall("ole32.dll", "int", "CoTaskMemFree", "ptr", DllStructGetData($uBI, 2))

    Return $aiCall
EndFunc


;===============================================================================
;
; Function Name:   _MsgBox()
; Description:     Identical to MsgBox() with the added ability to set a parent dialog (but without the ability to set a timeout)
;
; Parameter(s):    $iFlag    - Options for the dialog; see help file on MsgBox()
;                  $sTitle   - Title of dialog
;                  $sText    - Text of dialog
;                  $iTimeout - [dummy]
;                  $hWnd     - [optional] Handle to parent window
;
; Return Value(s): See help file on MsgBox()
;
; Author(s):       user52
; Modification(s): Ultima - Added return ability
;
; Note(s):         $iTimeout is a dummy variable that does nothing. It's there
;                  for partial backwards compatibility with MsgBox(). There
;                  doesn't appear to be any way to implement it without coding
;                  an entirely custom MsgBox() dialog. (Ultima)
;
;===============================================================================
Func _MsgBox($iFlag, $sTitle, $sText, $iTimeout = 0, $hWnd = 0)
    Local $aiCall = DllCall("user32.dll", "int", "MessageBox", "hwnd", $hWnd, "str", $sText, "str", $sTitle, "int", $iFlag)
    Return $aiCall[0]
EndFunc


;===============================================================================
;
; Function Name:   _PickIconDialog()
; Description:     Displays a dialog box that allows the user to choose an icon embedded in a resource such as an executable or DLL file
;
; Parameter(s):    $sPath  - Initial path to file
;                  $iIndex - Initial icon to select in the dialog (0-based index)
;                  $hWnd   - [optional] Handle to parent window
;
; Return Value(s): Success - Returns a 2-element array containing the selected path and the selected index
;                  Failure - Returns "" and sets @error
;
; Author(s):       Ultima
;
;===============================================================================
Func _PickIconDialog($sPath = "shell32.dll", $iIndex = 0, $hWnd = 0)
    Local $iPathLen = 256 ; Max chars in returned string

    Local $sCharType = "char"
    If @OSType = "WIN32_NT" Then $sCharType = "wchar"

    ; Create API structures
    Local $usPath = DLLStructCreate($sCharType & "[" & $iPathLen & "]")
    Local $usIndex = DLLStructCreate("int")

    ; Set Data of API structures
    DLLStructSetData($usPath, 1, $sPath)
    DLLStructSetData($usIndex, 1, $iIndex)

    ; Call API Function
    Local $aiCall = DLLCall("shell32.dll", "int", "PickIconDlg", "hwnd", $hWnd, "ptr", DLLStructGetPtr($usPath), "int", DLLStructGetSize($usPath), "ptr", DLLStructGetPtr($usIndex))

    If $aiCall[0] Then
        Local $avReturn[2] = [DLLStructGetData($usPath, 1), DLLStructGetData($usIndex, 1)]
        Return $avReturn
    EndIf
    Return SetError(1, 0, "")
EndFunc

Credits (of course) go to all previously involved in the inception/creation of these functions (I don't and can't possibly claim to have written even half of these excellent functions; I just do the organizing :)). The function descriptions tell all.

_MsgBox() now returns the proper value (but still doesn't have -- and probably won't ever get -- the timeout feature). I "created" __FileOSDialogHelper() because _FileOpenDialog() and _FileSaveDialog() had that huge chunk of duplicate code. Um, the path length maximum is now 256 because that's the standard Windows MAX_PATH size... right?

As I explained in the function notes, I used the old _FileSelectFolder() that amel27 wrote because his new one didn't seem to work properly (because of the callback function). I tried using picasso's callback UDF with the newer version of the function, and while it worked, it would require a global variable to make it stop selecting the initial directory after the first time, which I wasn't inclined to do (I feel that global variables -- unless they're constants -- have no place in UDFs). Anyway, I cleaned the function up to UDF standards.

I wrote the _PickIconDialog() function (of course, with some inspiration from the AutoIt docs) with the intent of including other standard Windows dialogs and calling the set of UDFs something like "WindowsDialogs.au3". Other functions I was planning on copying into the UDF set included GaryFrost's _ChooseFont() and _ChooseColor() functions from Misc.au3, but never got around to it (I might do that later). Including all of these into a single UDF makes sense (to me anyway ;)).

Comments and whatnot always welcome.

Edit: Oh, and regarding flag/options, you can probably figure them out via MSDN/Google:

http://msdn2.microsoft.com/en-us/library/ms646927.aspx (GetOpenFileName)

http://msdn2.microsoft.com/en-us/library/ms646928.aspx (GetSaveFileName)

http://msdn2.microsoft.com/en-us/library/ms646839.aspx (OPENFILENAME struct)

http://msdn2.microsoft.com/en-us/library/bb762115.aspx (SHBrowseForFolder)

http://msdn2.microsoft.com/en-us/library/bb773205.aspx (BROWSEINFO struct)

http://msdn2.microsoft.com/en-us/library/ms645505.aspx (MessageBox)

http://msdn2.microsoft.com/en-us/library/bb776481.aspx (PickIconDlg)

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

Ehm, shell32.dll/comdlg32.dll are integral pieces of any Windows installation, so you probably shouldn't be messing around with them (especially not replacing them with a copies from an entirely different OS). These functions have all been around in the DLLs since way before Windows XP (back to Windows 95), so the OS shouldn't be the issue.

Anyhow... Someone else mentioned that they wanted to make a set of UDFs out of these functions, but it never came to be (at least I haven't seen it), so I did some work in that direction...

Credits (of course) go to all previously involved in the inception/creation of these functions (I don't and can't possibly claim to have written even half of these excellent functions; I just do the organizing :)). The function descriptions tell all.

_MsgBox() now returns the proper value (but still doesn't have -- and probably won't ever get -- the timeout feature). I "created" __FileOSDialogHelper() because _FileOpenDialog() and _FileSaveDialog() had that huge chunk of duplicate code. Um, the path length maximum is now 256 because that's the standard Windows MAX_PATH size... right?

As I explained in the function notes, I used the old _FileSelectFolder() that amel27 wrote because his new one didn't seem to work properly (because of the callback function). I tried using picasso's callback UDF with the newer version of the function, and while it worked, it would require a global variable to make it stop selecting the initial directory after the first time, which I wasn't inclined to do (I feel that global variables -- unless they're constants -- have no place in UDFs). Anyway, I cleaned the function up to UDF standards.

I wrote the _PickIconDialog() function (of course, with some inspiration from the AutoIt docs) with the intent of including other standard Windows dialogs and calling the set of UDFs something like "WindowsDialogs.au3". Other functions I was planning on copying into the UDF set included GaryFrost's _ChooseFont() and _ChooseColor() functions from Misc.au3, but never got around to it (I might do that later). Including all of these into a single UDF makes sense (to me anyway ;)).

Comments and whatnot always welcome.

Edit: Oh, and regarding flag/options, you can probably figure them out via MSDN/Google:

http://msdn2.microsoft.com/en-us/library/ms646927.aspx (GetOpenFileName)

http://msdn2.microsoft.com/en-us/library/ms646928.aspx (GetSaveFileName)

http://msdn2.microsoft.com/en-us/library/ms646839.aspx (OPENFILENAME struct)

http://msdn2.microsoft.com/en-us/library/bb762115.aspx (SHBrowseForFolder)

http://msdn2.microsoft.com/en-us/library/bb773205.aspx (BROWSEINFO struct)

http://msdn2.microsoft.com/en-us/library/ms645505.aspx (MessageBox)

http://msdn2.microsoft.com/en-us/library/bb776481.aspx (PickIconDlg)

NICE JOB!

A decision is a powerful thing
Link to comment
Share on other sites

  • 3 weeks later...

So, I'm confused!! How do you actuall use this script in a larger script to open or save info? I have been trying to get it to work in the following script but it won't work (doesn't open the diolog box). What am I doing wrong?

CODE
Global $sTitle

Global $sInitDir

Global $sFilter = "All (*.*)"

Global $iOpt = 0

Global $sDefaultFile = ""

Global $sDefaultExt = ""

Global $hWnd = 0

;===============================================================================

;

; Function Name: _FileOpenDialog()

; Description: Identical to FileOpenDialog() with the added ability to set a parent dialog

;

; Parameter(s): $sTitle - Title of dialog

; $sInitDir - Initial directory selected

; $sFilter - [optional] File type filters, such as "All files (*.*)" or "Text files (*.txt)|All files (*.*)"

; $iOpt - [optional] Options for the dialog; see help file on FileOpenDialog()

; $sDefaultFile - [optional] Suggested file to open

; $sDefaultExt - [optional] Suggested extension to use

; $hWnd - [optional] Handle to parent window

;

; Return Value(s): Success - Returns a string containing the full path to the selected file(s)

; Failure - Returns "" and sets @error

;

; Author(s): amel27

;

;===============================================================================

Func _FileOpenDialog($sTitle, $sInitDir, $sFilter = "All (*.*)", $iOpt = 0, $sDefaultFile = "", $sDefaultExt = "", $hWnd = 0)

; API flags prepare

Local $iFlag = BitOR( _

BitShift(BitAND($iOpt, 1), -12), _

BitShift(BitAND($iOpt, 2), -10), _

BitShift(BitAND($iOpt, 4), -7 ), _

BitShift(BitAND($iOpt, 8), -10), _

BitShift(BitAND($iOpt, 4), -17) _

)

; Call API function

Local $usPath = __FileOSDialogHelper("GetOpenFileName", $iFlag, $sTitle, $sInitDir, $sFilter, $iOpt, $sDefaultFile, $sDefaultExt, $hWnd)

If Not @error Then

If BitAND($iOpt, 4) Then

$i = 1

While 1

If DllStructGetData($usPath, 1, $i) = 0 Then

If DllStructGetData($usPath, 1, $i+1) Then

DllStructSetData($usPath, 1, 124, $i)

Else

ExitLoop

EndIf

EndIf

$i += 1

Wend

EndIf

Return DllStructGetData($usPath, 1)

EndIf

Return SetError(1, 0, "")

EndFunc

;-------------------------------------------------------------------------------

; File Open/Save Dialog Helper Function (originally written by amel27)

;-------------------------------------------------------------------------------

Func __FileOSDialogHelper($sFunction, $iFlag, $sTitle, $sInitDir, $sFilter, $iOpt, $sDefaultFile, $sDefaultExt, $hWnd)

Local $iPathLen = 256 ; Max chars in returned string

; Filter string to array conversion

Local $asFLines = StringSplit($sFilter, "|"), $asFilter[$asFLines[0]*2+1]

Local $i, $iStart, $iFinal, $suFilter

$asFilter[0] = $asFLines[0]*2

For $i = 1 To $asFLines[0]

$iStart = StringInStr($asFLines[$i], "(", 0, 1)

$iFinal = StringInStr($asFLines[$i], ")", 0,-1)

$asFilter[$i*2-1] = StringStripWS(StringLeft($asFLines[$i], $iStart-1), 3)

$asFilter[$i*2] = StringStripWS(StringTrimRight(StringTrimLeft($asFLines[$i], $iStart), StringLen($asFLines[$i]) -$iFinal+1), 3)

$suFilter &= "char[" & StringLen($asFilter[$i*2-1])+1 & "];char[" & StringLen($asFilter[$i*2])+1 & "];"

Next

; Create API structures

Local $uOFN = DllStructCreate("dword;int;int;ptr;ptr;dword;dword;ptr;dword;ptr;int;ptr;ptr;dword;short;short;ptr;ptr;ptr;ptr;p

tr;dword;dword")

Local $usTitle = DllStructCreate("char[" & StringLen($sTitle)+1 & "]")

Local $usInitDir = DllStructCreate("char[" & StringLen($sInitDir)+1 & "]")

Local $usFilter = DllStructCreate($suFilter & "char")

Local $usPath = DllStructCreate("char[" & $iPathLen & "]")

Local $usExtn = DllStructCreate("charchar[" & StringLen($sDefaultExt)+1 & "]")

For $i=1 To $asFilter[0]

DllStructSetData($usFilter, $i, $asFilter[$i])

Next

; Set Data of API structures

DllStructSetData($usTitle, 1, $sTitle)

DllStructSetData($usInitDir, 1, $sInitDir)

DllStructSetData($usPath, 1, $sDefaultFile)

DllStructSetData($usExtn, 1, $sDefaultExt)

DllStructSetData($uOFN, 1, DllStructGetSize($uOFN))

DllStructSetData($uOFN, 2, $hWnd)

DllStructSetData($uOFN, 4, DllStructGetPtr($usFilter))

DllStructSetData($uOFN, 7, 1)

DllStructSetData($uOFN, 8, DllStructGetPtr($usPath))

DllStructSetData($uOFN, 9, $iPathLen)

DllStructSetData($uOFN, 12, DllStructGetPtr($usInitDir))

DllStructSetData($uOFN, 13, DllStructGetPtr($usTitle))

DllStructSetData($uOFN, 14, BitOR($iFlag, 4))

DllStructSetData($uOFN, 17, DllStructGetPtr($usExtn))

DllStructSetData($uOFN, 23, BitShift(BitAND($iOpt, 32), 5))

; Call API function

Local $aiCall = DllCall("comdlg32.dll", "int", $sFunction, "ptr", DllStructGetPtr($uOFN))

If $aiCall[0] Then Return $usPath

Return SetError(1, 0, "")

EndFunc

$var=_FileOpenDialog($sTitle, $sInitDir, $sFilter = "All (*.*)", $iOpt = 0, $sDefaultFile = "", $sDefaultExt = "", $hWnd = 0)

If @error Then

MsgBox(4096,"","No File(s) chosen")

Else

$var = StringReplace($var, "|", @CRLF)

MsgBox(4096,"","You chose " & $var)

EndIf

Link to comment
Share on other sites

functions variables are already declared locally in function, no need to make them Global

$title = "File Open Dialog"
$initialdir = @ScriptDir
$filter = "Au3 (*.au3)"
$iOption = 1 + 2 ; verifies file existance, popup messagebox if filename entered doesn't exist
$userdat = "Filename.au3" ; default initial filename (or none) for dialog input
$parentgui = 0 ; or Hwnd returned from GuiCreate


$var=_FileOpenDialog($title, $initialdir, $filter, $iOption, $userdat, "", $parentgui)
If @error Then
MsgBox(4096,"","No File(s) chosen")
Else
$var = StringReplace($var, "|", @CRLF)
MsgBox(4096,"","You chose " & $var)
EndIf


;===============================================================================
;
; Function Name: _FileOpenDialog()
; Description: Identical to FileOpenDialog() with the added ability to set a parent dialog
;
; Parameter(s): $sTitle - Title of dialog
; $sInitDir - Initial directory selected
; $sFilter - [optional] File type filters, such as "All files (*.*)" or "Text files (*.txt)|All files (*.*)"
; $iOpt - [optional] Options for the dialog; see help file on FileOpenDialog()
; $sDefaultFile - [optional] Suggested file to open
; $sDefaultExt - [optional] Suggested extension to use
; $hWnd - [optional] Handle to parent window
;
; Return Value(s): Success - Returns a string containing the full path to the selected file(s)
; Failure - Returns "" and sets @error
;
; Author(s): amel27
;
;===============================================================================
Func _FileOpenDialog($sTitle, $sInitDir, $sFilter = "All (*.*)", $iOpt = 0, $sDefaultFile = "", $sDefaultExt = "", $hWnd = 0)
    ; API flags prepare
    Local $iFlag = BitOR( _
    BitShift(BitAND($iOpt, 1), -12), _
    BitShift(BitAND($iOpt, 2), -10), _
    BitShift(BitAND($iOpt, 4), -7 ), _
    BitShift(BitAND($iOpt, 8), -10), _
    BitShift(BitAND($iOpt, 4), -17) _
    )

    ; Call API function
    Local $usPath = __FileOSDialogHelper("GetOpenFileName", $iFlag, $sTitle, $sInitDir, $sFilter, $iOpt, $sDefaultFile, $sDefaultExt, $hWnd)

    If Not @error Then
    If BitAND($iOpt, 4) Then
    $i = 1
    While 1
    If DllStructGetData($usPath, 1, $i) = 0 Then
    If DllStructGetData($usPath, 1, $i+1) Then
    DllStructSetData($usPath, 1, 124, $i)
    Else
    ExitLoop
    EndIf
    EndIf
    $i += 1
    Wend
    EndIf
    Return DllStructGetData($usPath, 1)
    EndIf
    Return SetError(1, 0, "")
EndFunc

;-------------------------------------------------------------------------------
; File Open/Save Dialog Helper Function (originally written by amel27)
;-------------------------------------------------------------------------------
Func __FileOSDialogHelper($sFunction, $iFlag, $sTitle, $sInitDir, $sFilter, $iOpt, $sDefaultFile, $sDefaultExt, $hWnd)
    Local $iPathLen = 256 ; Max chars in returned string

    ; Filter string to array conversion
    Local $asFLines = StringSplit($sFilter, "|"), $asFilter[$asFLines[0]*2+1]
    Local $i, $iStart, $iFinal, $suFilter
    $asFilter[0] = $asFLines[0]*2
    For $i = 1 To $asFLines[0]
    $iStart = StringInStr($asFLines[$i], "(", 0, 1)
    $iFinal = StringInStr($asFLines[$i], ")", 0,-1)
    $asFilter[$i*2-1] = StringStripWS(StringLeft($asFLines[$i], $iStart-1), 3)
    $asFilter[$i*2] = StringStripWS(StringTrimRight(StringTrimLeft($asFLines[$i], $iStart), StringLen($asFLines[$i]) -$iFinal+1), 3)
    $suFilter &= "char[" & StringLen($asFilter[$i*2-1])+1 & "];char[" & StringLen($asFilter[$i*2])+1 & "];"
    Next

    ; Create API structures
    Local $uOFN = DllStructCreate("dword;int;int;ptr;ptr;dword;dword;ptr;dword;ptr;int;ptr;ptr;dword;short;short;ptr;ptr;ptr;ptr;ptr;dword;dword")
    Local $usTitle = DllStructCreate("char[" & StringLen($sTitle)+1 & "]")
    Local $usInitDir = DllStructCreate("char[" & StringLen($sInitDir)+1 & "]")
    Local $usFilter = DllStructCreate($suFilter & "char")
    Local $usPath = DllStructCreate("char[" & $iPathLen & "]")
    Local $usExtn = DllStructCreate("charchar[" & StringLen($sDefaultExt)+1 & "]")
    For $i=1 To $asFilter[0]
    DllStructSetData($usFilter, $i, $asFilter[$i])
    Next

    ; Set Data of API structures
    DllStructSetData($usTitle, 1, $sTitle)
    DllStructSetData($usInitDir, 1, $sInitDir)
    DllStructSetData($usPath, 1, $sDefaultFile)
    DllStructSetData($usExtn, 1, $sDefaultExt)
    DllStructSetData($uOFN, 1, DllStructGetSize($uOFN))
    DllStructSetData($uOFN, 2, $hWnd)
    DllStructSetData($uOFN, 4, DllStructGetPtr($usFilter))
    DllStructSetData($uOFN, 7, 1)
    DllStructSetData($uOFN, 8, DllStructGetPtr($usPath))
    DllStructSetData($uOFN, 9, $iPathLen)
    DllStructSetData($uOFN, 12, DllStructGetPtr($usInitDir))
    DllStructSetData($uOFN, 13, DllStructGetPtr($usTitle))
    DllStructSetData($uOFN, 14, BitOR($iFlag, 4))
    DllStructSetData($uOFN, 17, DllStructGetPtr($usExtn))
    DllStructSetData($uOFN, 23, BitShift(BitAND($iOpt, 32), 5))

    ; Call API function
    Local $aiCall = DllCall("comdlg32.dll", "int", $sFunction, "ptr", DllStructGetPtr($uOFN))

    If $aiCall[0] Then Return $usPath
    Return SetError(1, 0, "")
EndFunc
Edited by rover

I see fascists...

Link to comment
Share on other sites

  • 11 months later...

Well I hate to bump an old topic, but how can I use this with multiple file types?

$title = "Test"
$initialdir = @ScriptDir
$filter = "Au3 (*.au3)"
$iOption = 1 + 2 + 4; verifies file existance, popup messagebox if filename entered doesn't exist
$userdat = "Filename.au3"; default initial filename (or none) for dialog input
$parentgui = 0; or Hwnd returned from GuiCreate


$var=_FileOpenDialog($title, $initialdir, $filter, $iOption, $userdat, "", $parentgui)
If @error Then
MsgBox(4096,"","No File(s) chosen")
Else
$var = StringReplace($var, "|", @CRLF)
MsgBox(4096,"","You chose " & $var)
EndIf


;===============================================================================
;
; Function Name: _FileOpenDialog()
; Description: Identical to FileOpenDialog() with the added ability to set a parent dialog
;
; Parameter(s): $sTitle - Title of dialog
; $sInitDir - Initial directory selected
; $sFilter - [optional] File type filters, such as "All files (*.*)" or "Text files (*.txt)|All files (*.*)"
; $iOpt - [optional] Options for the dialog; see help file on FileOpenDialog()
; $sDefaultFile - [optional] Suggested file to open
; $sDefaultExt - [optional] Suggested extension to use
; $hWnd - [optional] Handle to parent window
;
; Return Value(s): Success - Returns a string containing the full path to the selected file(s)
; Failure - Returns "" and sets @error
;
; Author(s): amel27
;
;===============================================================================
Func _FileOpenDialog($sTitle, $sInitDir, $sFilter = "All (*.*)", $iOpt = 0, $sDefaultFile = "", $sDefaultExt = "", $hWnd = 0)
   ; API flags prepare
    Local $iFlag = BitOR( _
    BitShift(BitAND($iOpt, 1), -12), _
    BitShift(BitAND($iOpt, 2), -10), _
    BitShift(BitAND($iOpt, 4), -7 ), _
    BitShift(BitAND($iOpt, 8), -10), _
    BitShift(BitAND($iOpt, 4), -17) _
    )

   ; Call API function
    Local $usPath = __FileOSDialogHelper("GetOpenFileName", $iFlag, $sTitle, $sInitDir, $sFilter, $iOpt, $sDefaultFile, $sDefaultExt, $hWnd)

    If Not @error Then
    If BitAND($iOpt, 4) Then
    $i = 1
    While 1
    If DllStructGetData($usPath, 1, $i) = 0 Then
    If DllStructGetData($usPath, 1, $i+1) Then
    DllStructSetData($usPath, 1, 124, $i)
    Else
    ExitLoop
    EndIf
    EndIf
    $i += 1
    Wend
    EndIf
    Return DllStructGetData($usPath, 1)
    EndIf
    Return SetError(1, 0, "")
EndFunc

;-------------------------------------------------------------------------------
; File Open/Save Dialog Helper Function (originally written by amel27)
;-------------------------------------------------------------------------------
Func __FileOSDialogHelper($sFunction, $iFlag, $sTitle, $sInitDir, $sFilter, $iOpt, $sDefaultFile, $sDefaultExt, $hWnd)
    Local $iPathLen = 256; Max chars in returned string

   ; Filter string to array conversion
    Local $asFLines = StringSplit($sFilter, "|"), $asFilter[$asFLines[0]*2+1]
    Local $i, $iStart, $iFinal, $suFilter
    $asFilter[0] = $asFLines[0]*2
    For $i = 1 To $asFLines[0]
    $iStart = StringInStr($asFLines[$i], "(", 0, 1)
    $iFinal = StringInStr($asFLines[$i], ")", 0,-1)
    $asFilter[$i*2-1] = StringStripWS(StringLeft($asFLines[$i], $iStart-1), 3)
    $asFilter[$i*2] = StringStripWS(StringTrimRight(StringTrimLeft($asFLines[$i], $iStart), StringLen($asFLines[$i]) -$iFinal+1), 3)
    $suFilter &= "char[" & StringLen($asFilter[$i*2-1])+1 & "];char[" & StringLen($asFilter[$i*2])+1 & "];"
    Next

   ; Create API structures
    Local $uOFN = DllStructCreate("dword;int;int;ptr;ptr;dword;dword;ptr;dword;ptr;int;ptr;ptr;dword;short;short;ptr;ptr;ptr;ptr;pt

r;dword;dword")
    Local $usTitle = DllStructCreate("char[" & StringLen($sTitle)+1 & "]")
    Local $usInitDir = DllStructCreate("char[" & StringLen($sInitDir)+1 & "]")
    Local $usFilter = DllStructCreate($suFilter & "char")
    Local $usPath = DllStructCreate("char[" & $iPathLen & "]")
    Local $usExtn = DllStructCreate("charchar[" & StringLen($sDefaultExt)+1 & "]")
    For $i=1 To $asFilter[0]
    DllStructSetData($usFilter, $i, $asFilter[$i])
    Next

   ; Set Data of API structures
    DllStructSetData($usTitle, 1, $sTitle)
    DllStructSetData($usInitDir, 1, $sInitDir)
    DllStructSetData($usPath, 1, $sDefaultFile)
    DllStructSetData($usExtn, 1, $sDefaultExt)
    DllStructSetData($uOFN, 1, DllStructGetSize($uOFN))
    DllStructSetData($uOFN, 2, $hWnd)
    DllStructSetData($uOFN, 4, DllStructGetPtr($usFilter))
    DllStructSetData($uOFN, 7, 1)
    DllStructSetData($uOFN, 8, DllStructGetPtr($usPath))
    DllStructSetData($uOFN, 9, $iPathLen)
    DllStructSetData($uOFN, 12, DllStructGetPtr($usInitDir))
    DllStructSetData($uOFN, 13, DllStructGetPtr($usTitle))
    DllStructSetData($uOFN, 14, BitOR($iFlag, 4))
    DllStructSetData($uOFN, 17, DllStructGetPtr($usExtn))
    DllStructSetData($uOFN, 23, BitShift(BitAND($iOpt, 32), 5))

   ; Call API function
    Local $aiCall = DllCall("comdlg32.dll", "int", $sFunction, "ptr", DllStructGetPtr($uOFN))

    If $aiCall[0] Then Return $usPath
    Return SetError(1, 0, "")
EndFunc

Any ideas?

Cheers,

Brett

Link to comment
Share on other sites

Well I hate to bump an old topic, but how can I use this with multiple file types?

[Code box removed]

Any ideas?

Cheers,

Brett

Hi Brett

this does not work in 3.2.12.0 production.

I get lines of multiple "|" delimiters and numbers returned for any single or multiple file selections.

some time after v.3.2.10.0 new file open/save dialogs where added to the UDF's.

_WinAPI_GetOpenFileName() in WinAPI.au3

or take a look at Siao's File Open/Save Dialogs customization.

http://www.autoitscript.com/forum/index.php?showtopic=64057

$aFile = _WinAPI_GetOpenFileName("My Open File Dialog", _
"Text File (*.txt;*.au3)|All Files (*.*)", ".", @ScriptName, _
"", 1, BitOR($OFN_ALLOWMULTISELECT, $OFN_EXPLORER), 0, $hGui)

Cheers

Edit: something I should point out in my previous post above.

I should have renamed those variables and made them Local or Global to the script.

a poor job of pointing out usage of duplicate variable names outside of a function.

Edit2: just noticed something about the previous posters (glarson) copy of the UDF you reposted

there is an extra 'char' in this section:

; Create API structures
....
Local $usExtn = DllStructCreate("charchar[" & StringLen($sDefaultExt)+1 & "]")

should be

Local $usExtn = DllStructCreate("char[" & StringLen($sDefaultExt)+1 & "]")
Edited by rover

I see fascists...

Link to comment
Share on other sites

In the latest versions there was added new hWnd parameter:

$sTitle = "Test"
$sInitDir = @ScriptDir
$sFilter = "Au3 (*.au3)"
$iOption = 1 + 2 + 4 ;verifies file existance, popup messagebox if filename entered doesn't exist
$sDefaultName = "Filename.au3" ;default initial filename (or none) for dialog input
$hParentGUI = WinGetHandle("") ;or Hwnd returned from GuiCreate

$sVar = FileOpenDialog($sTitle, $sInitDir, $sFilter, $iOption, $sDefaultName, $hParentGUI)

If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else
    $sVar = StringReplace($sVar, "|", @CRLF)
    MsgBox(64+4096, "", "You chose:" & @CRLF & $sVar)
EndIf

So now (in those new versions) i think there is no need for custom functions :P.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...