UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on June 10, 2014, 08:24:18 AM

Title: LoadDialog Replacement (Windows File Request)
Post by: kevin on June 10, 2014, 08:24:18 AM
   LoadDialog Replacement (Windows File Request)

      This example calls the windows open file dialog.   It's a replacement (of sort) for the PBdialogs library.  

      To request a file, call the LoadDialog() function.  The function has three main parameters,   Title$, PathAndFilename$ and Filter$

     Title$ = the title message that should appear across the top of this dialog

     PathAndFilename$ = (OPTIONAL) This can either be a Filename, or a filename with path$.  When the path is included, the dialog function will be passed this as the start up path for the dialog.    So you can prompt where the user starts searching for their file

     Filter$ = (OPTIONAL) THis defaults to all files, but you can set it to limit what file types (by extension) the user can choose between.  


 

[pbcode]

      cls 255
      
      print LoadDialog("Load File","C:/PlayBASIC.exe")

      Sync
      waitkey

      end

      



   Type tOpenFileName
     ;  DWORD         lStructSize;
          lStructSize;

    ;   HWND          hwndOwner;
           hwndOwner

;    HINSTANCE     hInstance;
           hInstance;

;   LPCTSTR       lpstrFilter;
            lpstrFilter


 ;  LPTSTR        lpstrCustomFilter;
            lpstrCustomFilter


 ;  DWORD         nMaxCustFilter;
             nMaxCustFilter;

 ;  DWORD         nFilterIndex;
              nFilterIndex;

 ;  LPTSTR        lpstrFile;
              lpstrFile;

 ;  DWORD         nMaxFile;
               nMaxFile;

 ;  LPTSTR        lpstrFileTitle;
               lpstrFileTitle;


 ;  DWORD         nMaxFileTitle;
                  nMaxFileTitle;
 
 ;  LPCTSTR       lpstrInitialDir;
                   lpstrInitialDir;

 ;  LPCTSTR       lpstrTitle;
                    lpstrTitle;
 


 ;  DWORD         Flags;
                   Flags;

 ;  WORD          nFileOffset;
                     nFileOffset as word

 ;  WORD          nFileExtension;
                  nFileExtension as word

 ;  LPCTSTR       lpstrDefExt;
                    lpstrDefExt

 ;  DWORD         lCustData;
                   lCustData;

 ;  LPOFNHOOKPROC lpfnHook;
               lpfnHook;
 
 
 ;  LPCTSTR       lpTemplateName;
               lpTemplateName;

endtype



   constant OFN_READONLY                 = 0x00000001
   constant OFN_OVERWRITEPROMPT          = 0x00000002
   constant OFN_HIDEREADONLY             = 0x00000004
   constant OFN_NOCHANGEDIR              = 0x00000008
   constant OFN_SHOWHELP                 = 0x00000010
   constant OFN_ENABLEHOOK               = 0x00000020
   constant OFN_ENABLETEMPLATE           = 0x00000040
   constant OFN_ENABLETEMPLATEHANDLE     = 0x00000080
   constant OFN_NOVALIDATE               = 0x00000100
   constant OFN_ALLOWMULTISELECT         = 0x00000200
   constant OFN_EXTENSIONDIFFERENT       = 0x00000400
   constant OFN_PATHMUSTEXIST            = 0x00000800
   constant OFN_FILEMUSTEXIST            = 0x00001000
   constant OFN_CREATEPROMPT             = 0x00002000
   constant OFN_SHAREAWARE               = 0x00004000
   constant OFN_NOREADONLYRETURN         = 0x00008000
   constant OFN_NOTESTFILECREATE         = 0x00010000
   constant OFN_NONETWORKBUTTON          = 0x00020000
   constant OFN_NOLONGNAMES              = 0x00040000     // force no long names for 4.x modules

   constant OFN_EXPLORER                 = 0x00080000     // new look commdlg
   constant OFN_NODEREFERENCELINKS       = 0x00100000
   constant OFN_LONGNAMES                = 0x00200000     // force long names for 3.x modules
   constant OFN_ENABLEINCLUDENOTIFY      = 0x00400000     // send include message to callback
   constant OFN_ENABLESIZING             = 0x00800000




linkdll "comdlg32.dll"
      GetOpenFileName(OFN_Structure_Ptr) alias "GetOpenFileNameA" as integer
EndLinkDll   

   

Function LoadDialog(Title$,PathAndFilename$="",Filter$ = "(All Files | *.*)")

      Dim OFN as tOpenFileName Pointer
      OFN = new tOpenFileName
      if Int(OFN)
      
         OFN.lStructSize = sizeof(tOpenFileName)

         // Strip the Filename from the what could be an absolute path/with filename$
         Filename$=trim$(getfilename$(pathAndFilename$),"\/")

         // path /oflder the dialog should open in  (if none defaults to current dir$())
         Path$=getfoldername$(pathAndFilename$)

         // get the path$ exists, if it doesn't set to the currentdir$         
         if len(path$)
            if folderexist(Path$)=false
                  path$=currentdir$()
            endif
         endif         
         
         OFN.lpstrInitialDir=_MakeDialogString(Path$)

         OFN.lpstrFile     =_MakeDialogString(Filename$)
         OFN.nMaxFile      =1024
      
         OFN.hwndOwner    =GetScreenHandle()


         OFN.lpstrTitle  =_MakeDialogString(Title$)

         OFN.Flags  = OFN_FILEMUSTEXIST

         OFN.lpstrFilter=_MakeDialogString(Filter$)


         STatus=GetOpenFileName(int(OFN))

         if Status=1
               Ptr=OFN.lpstrFile
               if Ptr
                  Filename$=peekString(ptr,0)
               endif      
         endif

         // clean up dialog
         _FreeDialogStringFromPtr(OFN.lpstrFile)
         _FreeDialogStringFromPtr(OFN.lpstrTitle)
         _FreeDialogStringFromPtr(OFN.lpstrFilter)
         _FreeDialogStringFromPtr(OFN.lpstrInitialDir)

         Free OFN
         
      endif

EndFunction  Filename$



Function _MakeDialogString(ThisSTring$)
      Size=Len(ThisString$)
      ThisBank=NewBank(4096)
      Ptr=GetBankPtr(ThisBank)
      pokeString Ptr,ThisString$,0
EndFunction Ptr


Psub _FreeDialogStringFromPtr(Ptr)
      For lp =1 to GetBankQuantity()
            if GetBankStatus(lp)
                  if Ptr=GetBankPtr(lp)
                        deletebank lp
                  endif
            endif
      next
EndPsub

[/pbcode]


  Related Articles:

      [plink]Windows File Dialogs Library (http://www.underwaredesign.com/forums/index.php?topic=4207.0)[/plink]



Title: Re: LoadDialog Replacement (Windows File Request)
Post by: monkeybot on June 10, 2014, 09:35:59 AM
that works for me!