Ooops
October 25, 2021, 17:55:24

Author Topic: [bmx] Modern RequestDir :- RequestFolder by col [ 1+ years ago ]  (Read 2345 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Modern RequestDir :- RequestFolder
Author : col
Posted : 1+ years ago

Description : Should be compatible with all Windows versions
The RequestDir requester looks out of data now-a-days on Vista and Win7 so I knocked up a quick solution for a more modern looking requester which will fall back to the original one for older OS's.

RequestFolder$(Title$,InitialPath$)

This can be used as a direct replacement for the RequestDir function.

[EDIT 06 Apr 17]
Added a requester to let you select multiple files at the same time. The results are returned as a string array.

RequestFiles$[](Title$,InitialPath$)


Code :
Code: BlitzMax
  1. Strict
  2.  
  3. ?Win32
  4.  
  5. Import "-lole32"
  6. Import Pub.Win32
  7.  
  8. Global Shell32Dll = LoadLibraryA("Shell32.dll")
  9. Global SHCreateItemFromParsingName(pszPath$w,pbc:Byte Ptr,riid:Byte Ptr,ppv:IUnknown Var)"Win32" = GetProcAddress(Shell32Dll,"SHCreateItemFromParsingName")
  10.  
  11. Global CLSID_FileOpenDialog[] = [$dc1c5a9c,$4ddee88a,$f860a1a5,$f7ae202a]
  12. Global IID_IFileOpenDialog[] = [$d57c7288,$4768d4ad,$969d02be,$60d93295]
  13. Global IID_IShellItem[] = [$43826d1e,$42eee718,$e2a155bc,$fe7bc361]
  14.  
  15.  
  16. Extern"Win32"
  17.         'These types are INCOMPLETE - DO NOT USE FOR ANYTHING ELSE !!!!!!
  18.         Type IModalWindow Extends IUnknown
  19.                 Method Show(hWnd)
  20.         EndType
  21.  
  22.         Type IFileDialog Extends IModalWindow
  23.                 Method SetFileTypes()
  24.                 Method SetFileTypeIndex()
  25.                 Method GetFileTypeIndex()
  26.                 Method Advise()
  27.                 Method Unadvise()
  28.                 Method SetOptions(dwOptions)
  29.                 Method GetOptions(dwOptions Ptr)
  30.                 Method SetDefaultFolder(pShellItem:Byte Ptr)
  31.                 Method SetFolder(pSI:Byte Ptr)
  32.                 Method GetFolder()
  33.                 Method GetCurrentSelection()
  34.                 Method SetFilename(pszName$w)
  35.                 Method GetFileName()
  36.                 Method SetTitle(pszName$w)
  37.                 Method SetOKButtonLabel()
  38.                 Method SetFilenameLabel()
  39.                 Method GetResult(pItem:IShellItem Var)
  40.                 Method AddPlace()
  41.                 Method SetDefaultExtension()
  42.                 Method Close()
  43.                 Method SetClientGuid()
  44.                 Method ClearClientData()
  45.                 Method SetFilter()
  46.         EndType
  47.        
  48.         Type IFileOpenDialog Extends IFileDialog
  49.                 Method GetResults(ppEnum:IShellItemArray Ptr)
  50.                 Method GetSelectedItems(ppsai:IShellItemArray Ptr)
  51.         EndType
  52.        
  53.         Type IShellItemArray Extends IUnknown
  54.                 Method BindToHandler()
  55.                 Method GetPropertyStore()
  56.                 Method GetPropertyDescriptionList()
  57.                 Method GetAttributes()
  58.                 Method GetCount(pdwNumItems:Int Ptr)
  59.                 Method GetItemAt(dwIndex:Int, ppsi:IShellItem Ptr)
  60.                 Method EnumItems()
  61.         EndType
  62.        
  63.         Type IShellItem Extends IUnknown
  64.                 Method BindToHandler()
  65.                 Method GetParent()
  66.                 Method GetDisplayName(sigdnName,ppszName:Short Ptr Var)
  67.                 Method GetAttributes()
  68.                 Method Compare()
  69.         EndType
  70.        
  71.         Function CoCreateInstance(rclsid:Byte Ptr,pUnkOuter:Byte Ptr,dwClsContext,riid:Byte Ptr,ppv:IUnknown Var) 'My version
  72.         Function CoInitialize(pvReserved)
  73.         Function CoUninitialize()
  74. EndExtern
  75.  
  76. Function RequestFiles:String[](title:String, initialPath:String)
  77.         Global pDialog:IFileOpenDialog
  78.         Global pInitialPath:IShellItem
  79.         Global pResults:IShellItemArray
  80.         Local hr:Int
  81.        
  82.         CoInitialize(0)
  83.  
  84.         hr = CoCreateInstance(CLSID_FileOpenDialog,Null,CLSCTX_INPROC_SERVER,IID_IFileOpenDialog,pDialog)
  85.         If hr < 0
  86.                 CleanUp()
  87.                 Return Null
  88.         EndIf
  89.        
  90.         Local dwOptions:Int
  91.         pDialog.GetOptions(Varptr dwOptions)
  92.         pDialog.SetOptions(dwOptions|$200) ' $200 = FOS_ALLOWMULTISELECT
  93.  
  94.         'Create an IShellItem for a default folder path
  95.         InitialPath = Replace(InitialPath,"/","")
  96.         SHCreateItemFromParsingName(InitialPath,Null,IID_IShellItem,pInitialPath)
  97.        
  98.         If pDialog.SetFolder(pInitialPath) < 0
  99.                 CleanUp()
  100.                 Return [RequestFile(Title,InitialPath)]
  101.         EndIf
  102.  
  103.         ' show it
  104.         pDialog.SetTitle(Title)
  105.         pDialog.Show(0)
  106.  
  107.         ' get the result
  108.         If pDialog.GetResults(Varptr pResults) < 0
  109.                 CleanUp()
  110.                 Return Null
  111.         EndIf
  112.        
  113.         'Get the results
  114.         Local count:Int
  115.         If pResults.GetCount(Varptr count) < 0
  116.                 CleanUp()
  117.                 Return Null
  118.         EndIf
  119.  
  120.         Local selectedItemNames:String[count]
  121.         For Local i:Int = 0 Until count
  122.                 Local pItem:IShellItem
  123.                 If pResults.getItemAt(i, Varptr pItem) >= 0
  124.                         Local pName:Short Ptr
  125.                         pItem.GetDisplayName($80058000,pName)
  126.                         selectedItemNames[i] = String.FromWString(pName)
  127.                 EndIf
  128.  
  129.                 If pItem pItem.Release_()
  130.         Next   
  131.        
  132.         CleanUp()
  133.         Return selectedItemNames
  134.        
  135.         Function CleanUp()
  136.                 If pDialog
  137.                         pDialog.Release_()
  138.                         pDialog = Null
  139.                 EndIf
  140.                 If pInitialPath
  141.                         pInitialPath.Release_()
  142.                         pInitialPath = Null
  143.                 EndIf
  144.                 If pResults
  145.                         pResults.Release_()
  146.                         pResults = Null
  147.                 EndIf
  148.                 CoUninitialize()
  149.         EndFunction    
  150. EndFunction
  151.  
  152.  
  153. Function RequestFolder$(Title$,InitialPath$)
  154.         Global pDialog:IFileOpenDialog
  155.         Global pInitialPath:IShellItem
  156.         Global pFolder:IShellItem
  157.         Local hr,ResultFolder$
  158.  
  159.         CoInitialize(0)
  160.  
  161.         'Create Instance of the Dialog 
  162.         hr = CoCreateInstance(CLSID_FileOpenDialog,Null,CLSCTX_INPROC_SERVER,IID_IFileOpenDialog,pDialog)
  163.  
  164.         'Not on Vista or Win7?
  165.         If hr < 0 CleanUp(); Return RequestDir(Title,InitialPath)
  166.        
  167.         'Set it to Browse Folders
  168.         Local dwOptions
  169.         pDialog.GetOptions(Varptr dwOptions)
  170.         pDialog.SetOptions(dwOptions|$20)
  171.        
  172.         'Set Title
  173.         pDialog.SetTitle(Title)
  174.        
  175.         'Create an IShellItem for a default folder path
  176.         InitialPath = Replace(InitialPath,"/","")
  177.         SHCreateItemFromParsingName(InitialPath,Null,IID_IShellItem,pInitialPath)
  178.        
  179.         If pDialog.SetFolder(pInitialPath) < 0
  180.                 CleanUp()
  181.                 Return RequestDir(Title,InitialPath)
  182.         EndIf
  183.                
  184.         'Show the Dialog
  185.         pDialog.Show(0)
  186.  
  187.         'Test the result
  188.         If pDialog.GetResult(pFolder) < 0
  189.                 CleanUp
  190.                 Return ""
  191.         EndIf
  192.        
  193.         'Get the result
  194.         Local pName:Short Ptr
  195.         pFolder.GetDisplayName($80058000,pName)
  196.         ResultFolder = String.FromWString(pName)
  197.        
  198.         CleanUp()
  199.         Return ResultFolder
  200.        
  201.         Function CleanUp()
  202.                 If pDialog
  203.                         pDialog.Release_()
  204.                         pDialog = Null
  205.                 EndIf
  206.                 If pInitialPath
  207.                         pInitialPath.Release_()
  208.                         pInitialPath = Null
  209.                 EndIf
  210.                 If pFolder
  211.                         pFolder.Release_()
  212.                         pFolder = Null
  213.                 EndIf
  214.                 CoUninitialize()
  215.         EndFunction
  216. EndFunction
  217. ?
  218.  
  219. 'Example Usage
  220. Print RequestFolder("Select a Folder...","")
  221. Local files:String[] = RequestFiles("Select some file(s)","")
  222. For Local file:String = EachIn files
  223.         Print file
  224. Next


Comments :


TomToad(Posted 1+ years ago)

 got an EAV on this pInitialPath.Release_ after clicking on "Select Folder".Windows 7 64 bit sp1


col(Posted 1+ years ago)

 UpdatedI've changed the code to include error checking and tidied it up a little.@TomToadYou would have got the EAV if you didn't supply a valid InitialPath. All fixed now.


col(Posted 1+ years ago)

 Works Ok on Win8 too.


col(Posted 2 months ago)

 Fixed a subtle bug...As long as a user selected a folder then all is ok.However if a user decided to cancel selecting a folder after the 2nd time this had been called then an EAV would pop up.Also added a file requestor to let you select multiple files simultaneouslyNote the plural function name and a string array is returned : RequestFiles:String[](title:String, initialPath:String)Any probs let me know.


Offline Ryszard

  • Jr. Member
  • **
  • Posts: 47
Re: [bmx] Modern RequestDir :- RequestFolder by col [ 1+ years ago ]
« Reply #1 on: February 11, 2021, 21:21:56 »
Good morning!

Very nice code. I use it in my technical drawing processing program. On the old version of BlitzMax it works without any problems. Unfortunately, it needs more memory and faster speed for large drawings. I switched to BlitzMax NG 64-bit. During compilation I got this message: unable to convert from byte ptr to int blitzmax. I tried the advice I found on the web. Without result. I am asking for advice.

Offline Derron

  • Hero Member
  • *****
  • Posts: 3674
Re: [bmx] Modern RequestDir :- RequestFolder by col [ 1+ years ago ]
« Reply #2 on: February 11, 2021, 23:09:25 »
The code needs a little bit of an overhaul to get it working on NG.

some "types" would now be called "interface", you need to make it "superstrict" to see differences - as some stuff is using "integer" as default while it should be "byte ptr" and other stuff.

I changed some lines but still get an EAV on startup - so surely missed a lot of stuff to change.

Code: BlitzMax
  1. SuperStrict
  2. Framework Brl.StandardIO
  3. Import Brl.SystemDefault 'RequestDir()
  4.  
  5. ?Win32
  6.  
  7. Import "-lole32"
  8. Import Pub.Win32
  9.  
  10. Global Shell32Dll:Byte Ptr = LoadLibraryA("Shell32.dll")
  11. Global SHCreateItemFromParsingName(pszPath$w,pbc:Byte Ptr,riid:Byte Ptr,ppv:IUnknown_ Var)"Win32" = GetProcAddress(Shell32Dll,"SHCreateItemFromParsingName")
  12.  
  13. Global CLSID_FileOpenDialog:Int[] = [$dc1c5a9c,$4ddee88a,$f860a1a5,$f7ae202a]
  14. Global IID_IFileOpenDialog:Int[] = [$d57c7288,$4768d4ad,$969d02be,$60d93295]
  15. Global IID_IShellItem:Int[] = [$43826d1e,$42eee718,$e2a155bc,$fe7bc361]
  16.  
  17.  
  18. Extern"Win32"
  19.         'These types are INCOMPLETE - DO NOT USE FOR ANYTHING ELSE !!!!!!
  20.         Interface IModalWindow Extends IUnknown_
  21.                 Method Show(hWnd:Byte Ptr)
  22.         EndInterface
  23.  
  24.         Interface IFileDialog Extends IModalWindow
  25.                 Method SetFileTypes()
  26.                 Method SetFileTypeIndex()
  27.                 Method GetFileTypeIndex()
  28.                 Method Advise()
  29.                 Method Unadvise()
  30.                 Method SetOptions(dwOptions:Int)
  31.                 Method GetOptions(dwOptions:Int Ptr)
  32.                 Method SetDefaultFolder(pShellItem:Byte Ptr)
  33.                 Method SetFolder:Int(pSI:Byte Ptr)
  34.                 Method GetFolder()
  35.                 Method GetCurrentSelection()
  36.                 Method SetFilename(pszName$w)
  37.                 Method GetFileName()
  38.                 Method SetTitle(pszName$w)
  39.                 Method SetOKButtonLabel()
  40.                 Method SetFilenameLabel()
  41.                 Method GetResult:Int(pItem:IShellItem Var)
  42.                 Method AddPlace()
  43.                 Method SetDefaultExtension()
  44.                 Method Close()
  45.                 Method SetClientGuid()
  46.                 Method ClearClientData()
  47.                 Method SetFilter()
  48.         EndInterface
  49.        
  50.         Interface IFileOpenDialog Extends IFileDialog
  51.                 Method GetResults:Int(ppEnum:IShellItemArray Ptr)
  52.                 Method GetSelectedItems(ppsai:IShellItemArray Ptr)
  53.         EndInterface
  54.        
  55.         Interface IShellItemArray Extends IUnknown_
  56.                 Method BindToHandler()
  57.                 Method GetPropertyStore()
  58.                 Method GetPropertyDescriptionList()
  59.                 Method GetAttributes()
  60.                 Method GetCount:Int(pdwNumItems:Int Ptr)
  61.                 Method GetItemAt:Int(dwIndex:Int, ppsi:IShellItem Ptr)
  62.                 Method EnumItems()
  63.         EndInterface
  64.        
  65.         Interface IShellItem Extends IUnknown_
  66.                 Method BindToHandler()
  67.                 Method GetParent()
  68.                 Method GetDisplayName(sigdnName:Int,ppszName:Short Ptr Var)
  69.                 Method GetAttributes()
  70.                 Method Compare()
  71.         EndInterface
  72.        
  73.         Function CoCreateInstance:Byte Ptr(rclsid:Byte Ptr,pUnkOuter:Byte Ptr,dwClsContext:Byte Ptr,riid:Byte Ptr,ppv:IUnknown_ Var)="HRESULT CoCreateInstance(REFCLSID, LPUNKNOWN, DWORD, REFIID, LPVOID)!"
  74.         Function CoInitialize:Byte Ptr(pvReserved:Byte Ptr)="HRESULT CoInitialize(LPVOID)!"
  75.         Function CoUninitialize()="void CoUninitialize()!"
  76. EndExtern
  77.  
  78. Function RequestFiles:String[](title:String, initialPath:String)
  79.         Global pDialog:IFileOpenDialog
  80.         Global pInitialPath:IShellItem
  81.         Global pResults:IShellItemArray
  82.         Local hr:Byte Ptr
  83.        
  84.         CoInitialize(0)
  85.  
  86.         hr = CoCreateInstance(CLSID_FileOpenDialog,Null,CLSCTX_INPROC_SERVER,IID_IFileOpenDialog,pDialog)
  87.         If hr < 0
  88.                 CleanUp()
  89.                 Return Null
  90.         EndIf
  91.        
  92.         Local dwOptions:Int
  93.         pDialog.GetOptions(Varptr dwOptions)
  94.         pDialog.SetOptions(dwOptions|$200) ' $200 = FOS_ALLOWMULTISELECT
  95.  
  96.         'Create an IShellItem for a default folder path
  97.         InitialPath = InitialPath.Replace("/","\")
  98.         SHCreateItemFromParsingName(InitialPath,Null,IID_IShellItem,pInitialPath)
  99.        
  100.         If pDialog.SetFolder(pInitialPath) < 0
  101.                 CleanUp()
  102.                 Return [RequestFile(Title,InitialPath)]
  103.         EndIf
  104.  
  105.         ' show it
  106.         pDialog.SetTitle(Title)
  107.         pDialog.Show(0)
  108.  
  109.         ' get the result
  110.         If pDialog.GetResults(Varptr pResults) < 0
  111.                 CleanUp()
  112.                 Return Null
  113.         EndIf
  114.        
  115.         'Get the results
  116.         Local count:Int
  117.         If pResults.GetCount(Varptr count) < 0
  118.                 CleanUp()
  119.                 Return Null
  120.         EndIf
  121.  
  122.         Local selectedItemNames:String[count]
  123.         For Local i:Int = 0 Until count
  124.                 Local pItem:IShellItem
  125.                 If pResults.getItemAt(i, Varptr pItem) >= 0
  126.                         Local pName:Short Ptr
  127.                         pItem.GetDisplayName($80058000,pName)
  128.                         selectedItemNames[i] = String.FromWString(pName)
  129.                 EndIf
  130.  
  131.                 If pItem pItem.Release_()
  132.         Next  
  133.        
  134.         CleanUp()
  135.         Return selectedItemNames
  136.        
  137.         Function CleanUp()
  138.                 If pDialog
  139.                         pDialog.Release_()
  140.                         pDialog = Null
  141.                 EndIf
  142.                 If pInitialPath
  143.                         pInitialPath.Release_()
  144.                         pInitialPath = Null
  145.                 EndIf
  146.                 If pResults
  147.                         pResults.Release_()
  148.                         pResults = Null
  149.                 EndIf
  150.                 CoUninitialize()
  151.         EndFunction    
  152. EndFunction
  153.  
  154.  
  155. Function RequestFolder$(Title$,InitialPath$)
  156.         Global pDialog:IFileOpenDialog
  157.         Global pInitialPath:IShellItem
  158.         Global pFolder:IShellItem
  159.         Local hr:Byte Ptr
  160.                 Local ResultFolder$
  161.  
  162.         CoInitialize(0)
  163.  
  164.         'Create Instance of the Dialog
  165.         hr = CoCreateInstance(CLSID_FileOpenDialog,Null,CLSCTX_INPROC_SERVER,IID_IFileOpenDialog,pDialog)
  166.  
  167.         'Not on Vista or Win7?
  168.         If hr < 0 CleanUp(); Return RequestDir(Title,InitialPath)
  169.        
  170.         'Set it to Browse Folders
  171.         Local dwOptions:Int
  172.         pDialog.GetOptions(Varptr dwOptions)
  173.         pDialog.SetOptions(dwOptions|$20)
  174.        
  175.         'Set Title
  176.         pDialog.SetTitle(Title)
  177.        
  178.         'Create an IShellItem for a default folder path
  179.         InitialPath = InitialPath.Replace("/","\")
  180.         SHCreateItemFromParsingName(InitialPath,Null,IID_IShellItem,pInitialPath)
  181.        
  182.         If pDialog.SetFolder(pInitialPath) < 0
  183.                 CleanUp()
  184.                 Return RequestDir(Title,InitialPath)
  185.         EndIf
  186.                
  187.         'Show the Dialog
  188.         pDialog.Show(0)
  189.  
  190.         'Test the result
  191.         If pDialog.GetResult(pFolder) < 0
  192.                 CleanUp
  193.                 Return ""
  194.         EndIf
  195.        
  196.         'Get the result
  197.         Local pName:Short Ptr
  198.         pFolder.GetDisplayName($80058000,pName)
  199.         ResultFolder = String.FromWString(pName)
  200.        
  201.         CleanUp()
  202.         Return ResultFolder
  203.        
  204.         Function CleanUp()
  205.                 If pDialog
  206.                         pDialog.Release_()
  207.                         pDialog = Null
  208.                 EndIf
  209.                 If pInitialPath
  210.                         pInitialPath.Release_()
  211.                         pInitialPath = Null
  212.                 EndIf
  213.                 If pFolder
  214.                         pFolder.Release_()
  215.                         pFolder = Null
  216.                 EndIf
  217.                 CoUninitialize()
  218.         EndFunction
  219. EndFunction
  220. ?
  221.  
  222. 'Example Usage
  223. Print RequestFolder("Select a Folder...","")
  224. Local files:String[] = RequestFiles("Select some file(s)","")
  225. For Local file:String = EachIn files
  226.         Print file
  227. Next
  228.  

Mine fails at "If pDialog.SetFolder(pInitialPath) < 0"

Offline col

  • Hero Member
  • *****
  • Posts: 603
Re: [bmx] Modern RequestDir :- RequestFolder by col [ 1+ years ago ]
« Reply #3 on: February 12, 2021, 01:04:27 »
Thanks Derron - you were almost there!
Just for you reference - return values (HRESULT hr) for COM functions/methods are 32bit Ints (long in c).

The remaining culprits were
Code: [Select]
Method SetDefaultFolder(pShellItem:Byte Ptr)
Method SetFolder:Int(pSI:Byte Ptr)
to this
Code: [Select]
Method SetDefaultFolder(pShellItem:IShellItem)
Method SetFolder:Int(pSI:IShellItem)

Complete example now checked and working with x86 and x64.

Code: [Select]
SuperStrict
Framework Brl.StandardIO
Import Brl.SystemDefault 'RequestDir()
 
?Win32
 
Import "-lole32"
Import Pub.Win32
 
Global Shell32Dll:Byte Ptr = LoadLibraryA("Shell32.dll")
Global SHCreateItemFromParsingName:Int(pszPath$w,pbc:Byte Ptr,riid:Byte Ptr,ppv:IUnknown_ Var)"Win32" = GetProcAddress(Shell32Dll,"SHCreateItemFromParsingName")
 
Global CLSID_FileOpenDialog:Int[] = [$dc1c5a9c,$4ddee88a,$f860a1a5,$f7ae202a]
Global IID_IFileOpenDialog:Int[] = [$d57c7288,$4768d4ad,$969d02be,$60d93295]
Global IID_IShellItem:Int[] = [$43826d1e,$42eee718,$e2a155bc,$fe7bc361]
 
 
Extern"Win32"
        'These types are INCOMPLETE - DO NOT USE FOR ANYTHING ELSE !!!!!!
        Interface IModalWindow Extends IUnknown_
                Method Show(hWnd:Byte Ptr)
        EndInterface
 
        Interface IFileDialog Extends IModalWindow
                Method SetFileTypes()
                Method SetFileTypeIndex()
                Method GetFileTypeIndex()
                Method Advise()
                Method Unadvise()
                Method SetOptions:Int(dwOptions:Int)
                Method GetOptions:Int(dwOptions:Int Ptr)
                Method SetDefaultFolder:Int(pShellItem:IShellItem)
                Method SetFolder:Int(pSI:IShellItem)
                Method GetFolder()
                Method GetCurrentSelection()
                Method SetFilename:Int(pszName$w)
                Method GetFileName()
                Method SetTitle:Int(pszName$w)
                Method SetOKButtonLabel()
                Method SetFilenameLabel()
                Method GetResult:Int(pItem:IShellItem Var)
                Method AddPlace()
                Method SetDefaultExtension()
                Method Close()
                Method SetClientGuid()
                Method ClearClientData()
                Method SetFilter()
        EndInterface
       
        Interface IFileOpenDialog Extends IFileDialog
                Method GetResults:Int(ppEnum:IShellItemArray Ptr)
                Method GetSelectedItems:Int(ppsai:IShellItemArray Ptr)
        EndInterface
       
        Interface IShellItemArray Extends IUnknown_
                Method BindToHandler()
                Method GetPropertyStore()
                Method GetPropertyDescriptionList()
                Method GetAttributes()
                Method GetCount:Int(pdwNumItems:Int Ptr)
                Method GetItemAt:Int(dwIndex:Int, ppsi:IShellItem Ptr)
                Method EnumItems()
        EndInterface
       
        Interface IShellItem Extends IUnknown_
                Method BindToHandler()
                Method GetParent()
                Method GetDisplayName:Int(sigdnName:Int,ppszName:Short Ptr Var)
                Method GetAttributes()
                Method Compare()
        EndInterface
       
        Function CoCreateInstance:Int(rclsid:Byte Ptr,pUnkOuter:Byte Ptr,dwClsContext:Byte Ptr,riid:Byte Ptr,ppv:IUnknown_ Var)="HRESULT CoCreateInstance(REFCLSID, LPUNKNOWN, DWORD, REFIID, LPVOID)!"
        Function CoInitialize:Int(pvReserved:Byte Ptr)="HRESULT CoInitialize(LPVOID)!"
        Function CoUninitialize()="void CoUninitialize()!"
EndExtern
 
Function RequestFiles:String[](title:String, initialPath:String)
        Global pDialog:IFileOpenDialog
        Global pInitialPath:IShellItem
        Global pResults:IShellItemArray
        Local hr:Byte Ptr
       
        CoInitialize(0)
 
        hr = CoCreateInstance(CLSID_FileOpenDialog,Null,CLSCTX_INPROC_SERVER,IID_IFileOpenDialog,pDialog)
        If hr < 0
Cleanup()
Return [RequestFile(Title,InitialPath)]
        EndIf
       
        Local dwOptions:Int
        pDialog.GetOptions(Varptr dwOptions)
        pDialog.SetOptions(dwOptions|$200) ' $200 = FOS_ALLOWMULTISELECT
 
        'Create an IShellItem for a default folder path
        InitialPath = InitialPath.Replace("/","\")
        If SHCreateItemFromParsingName(InitialPath,Null,IID_IShellItem,pInitialPath) < 0
Cleanup()
Return [RequestFile(Title,InitialPath)]
EndIf

        If pDialog.SetFolder(pInitialPath) < 0
                CleanUp()
                Return [RequestFile(Title,InitialPath)]
        EndIf
 
        ' show it
        pDialog.SetTitle(Title)
        pDialog.Show(0)
 
        ' get the result
        If pDialog.GetResults(Varptr pResults) < 0
                CleanUp()
                Return Null
        EndIf
       
        'Get the results
        Local count:Int
        If pResults.GetCount(Varptr count) < 0
                CleanUp()
                Return Null
        EndIf
 
        Local selectedItemNames:String[count]
        For Local i:Int = 0 Until count
                Local pItem:IShellItem
                If pResults.getItemAt(i, Varptr pItem) >= 0
                        Local pName:Short Ptr
                        pItem.GetDisplayName($80058000,pName)
                        selectedItemNames[i] = String.FromWString(pName)
                EndIf
 
                If pItem pItem.Release_()
        Next 
       
        CleanUp()
        Return selectedItemNames
       
        Function CleanUp()
                If pDialog
                        pDialog.Release_()
                        pDialog = Null
                EndIf
                If pInitialPath
                        pInitialPath.Release_()
                        pInitialPath = Null
                EndIf
                If pResults
                        pResults.Release_()
                        pResults = Null
                EndIf
                CoUninitialize()
        EndFunction   
EndFunction
 
 
Function RequestFolder$(Title$,InitialPath$)
        Global pDialog:IFileOpenDialog
        Global pInitialPath:IShellItem
        Global pFolder:IShellItem
        Local hr:Int
        Local ResultFolder$
 
        CoInitialize(0)
 
        'Create Instance of the Dialog
        hr = CoCreateInstance(CLSID_FileOpenDialog,Null,CLSCTX_INPROC_SERVER,IID_IFileOpenDialog,pDialog)
 
        'Not on Vista or Win7?
        If hr < 0
CleanUp()
Return RequestDir(Title,InitialPath)
EndIf
       
        'Set it to Browse Folders
        Local dwOptions:Int
        pDialog.GetOptions(Varptr dwOptions)
        pDialog.SetOptions(dwOptions|$20)
       
        'Set Title
        pDialog.SetTitle(Title)

        'Create an IShellItem for a default folder path
        InitialPath = InitialPath.Replace("/","\")
        hr = SHCreateItemFromParsingName(InitialPath, Null, IID_IShellItem, pInitialPath)
       
        If pDialog.SetFolder(pInitialPath) < 0
                CleanUp()
                Return RequestDir(Title,InitialPath)
        EndIf
               
        'Show the Dialog
        pDialog.Show(0)
 
        'Test the result
        If pDialog.GetResult(pFolder) < 0
                CleanUp
                Return ""
        EndIf
       
        'Get the result
        Local pName:Short Ptr
        pFolder.GetDisplayName($80058000,pName)
        ResultFolder = String.FromWString(pName)
       
        CleanUp()
        Return ResultFolder
       
        Function CleanUp()
                If pDialog
                        pDialog.Release_()
                        pDialog = Null
                EndIf
                If pInitialPath
                        pInitialPath.Release_()
                        pInitialPath = Null
                EndIf
                If pFolder
                        pFolder.Release_()
                        pFolder = Null
                EndIf
                CoUninitialize()
        EndFunction
EndFunction
?
 
'Example Usage
Print RequestFolder("Select a Folder...","")
Local files:String[] = RequestFiles("Select some file(s)","")
For Local file:String = EachIn files
        Print file
Next
« Last Edit: February 12, 2021, 02:17:23 by col »

Offline Ryszard

  • Jr. Member
  • **
  • Posts: 47
Re: [bmx] Modern RequestDir :- RequestFolder by col [ 1+ years ago ]
« Reply #4 on: February 15, 2021, 20:03:28 »
Thank you very much.

Offline Ryszard

  • Jr. Member
  • **
  • Posts: 47
Re: [bmx] Modern RequestDir :- RequestFolder by col [ 1+ years ago ]
« Reply #5 on: February 21, 2021, 21:12:55 »
Passes the corrected code and lines it on this line

Local foto: TPixmap = LoadPixmap (file)

 in my code by reporting a bug

Compile Error

Type 'tpixmap' not found.

Please give me some suggestions

Offline Derron

  • Hero Member
  • *****
  • Posts: 3674
Re: [bmx] Modern RequestDir :- RequestFolder by col [ 1+ years ago ]
« Reply #6 on: February 21, 2021, 22:33:40 »
If you use the "framework" command, you need to import the used modules on your own.

import brl.pixmap


bye
Ron

Offline Ryszard

  • Jr. Member
  • **
  • Posts: 47
Re: [bmx] Modern RequestDir :- RequestFolder by col [ 1+ years ago ]
« Reply #7 on: February 23, 2021, 22:05:09 »
Thank you very much for information. My program now works as I wanted.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal