Announcement

Collapse
No announcement yet.

Not getting far with Common Dialogs

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Not getting far with Common Dialogs

    Hi all,
    Well its finally time for me to learn how to use common dialogs to
    open and save files in PBDLL6 but I'm not getting very far.

    If I do this very simple program:
    Code:
    'Common Dialogs
    
    #INCLUDE "Comdlg32.inc"
    
    FUNCTION PBMAIN()
       
     MSGBOX "Open a file"
    
    END FUNCTION
    When I try to compile it I get:

    Undefined TYPE line 169; lpLogFont AS LOGFONT PTR

    which sure enough is the fourth line down in the TYPE CHOOSEFONTAPI
    user defined type in the comdlg32.inc file of which I've downloaded the
    latest from the PB site (Last Update: November 11, 1999)

    After I've got over this hurdle I need to basically call the Open and Save
    common dialogs to allow the user to navigate around the file system
    and choose either places to save files or files to load etc...
    It would be great (for me) if I didn't necessarily have to create windows
    to do this and if anyone could show me the simplest and easiest method
    to use these common dialogs I'll be very happy.

    TIA
    IanMc



    ------------------

  • #2
    Ian --

    You need to #INCLUDE "WIN32API.INC" before you #INCLUDE "COMDLG32.INC".

    WIN32API.INC defines many of the structures and equates that are used by COMDLG32.INC.

    -- Eric

    ------------------
    Perfect Sync: Perfect Sync Development Tools
    Email: mailto:[email protected][email protected]</A>



    [This message has been edited by Eric Pearson (edited February 07, 2001).]
    "Not my circus, not my monkeys."

    Comment


    • #3
      Thanks for your help Eric, I still have a problem though.

      my revised code now looks like:
      Code:
      'Common Dialogs
      
      #INCLUDE "Win32api.inc"
      #INCLUDE "Comdlg32.inc"
      
      FUNCTION PBMAIN()
         
       MSGBOX "Open a file"
      
      END FUNCTION
      and when I try to compile it I get:

      Error 516 in D:\IANSTUFF\PWRBASIC\PBDLL60\CMMDLG\DLG1.BAS(6:18): DefType, Type id (?%&!#$), or AS...
      required
      Line 6: Function PBMAIN()

      It's probably me,,,, any ideas?

      Thanks

      IanMc


      ------------------

      Comment


      • #4
        See, I told you it was me
        I forgot to put the $COMPILE EXE line in <BLUSH>
        Thats what happens when you use PBCC a lot

        Now,,, ahem,,, what is the easiest way to use the fileopen common dialog,
        supplying it with a start path and filter and getting it to return
        a path and filename?

        and how would I use the same principal with the save dialog ?

        do I have to supply a window handle to do this?
        I will be using a DDT dialog but just out of interest is there anyway to
        simplify it and get it to work without supplying a window handle?

        Thanks for your help,, I'll 'try' to be more intelligent in the future

        Ian

        ------------------

        Comment


        • #5
          ian,

          (is this the same ian mcallister who at vbworld?)

          there is another thread going on at the moment with the same question: try this http://www.powerbasic.com/support/pb...ead.php?t=3225

          cheers


          ------------------

          paul dwyer
          network engineer
          aussie in tokyo
          (paul282 at vb-world)

          Comment


          • #6
            hi paul,
            nope not the same ian mcallister..

            that other thread you mentioned,,, they haven't answered the question either

            however i did find some code from lance edmonds (what a chap!!!)

            he provides a fully compileable demo of how to use the opendialog thingy,,,
            http://www.powerbasic.com/support/pb...ad.php?t=14492

            now,,, as soon as i've figured out how to use the savedialog i'll be sorted.

            imo open, save, save as, should all be one dialog where you can supply
            info such as start file, max filename length, different styles etc. and you can supply
            the title of the dialog,,, "save this super powerbasic program as ..." and maybe an icon
            in the title bar too, then the dialog would supply the tree view and return the values of
            whatever was chosen.

            would that be too much to ask bill gates???

            instead it seems you have to have a degree in windows api programming
            and the patience of an angel,,, wonder what'll come along in pbdll7 ???
            i can't wait,,, gimme gimme!!!

            ian


            ------------------

            Comment


            • #7
              This maybe can help. Use 0 for hWnd if you don't want to use dialog.
              Code:
              '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
              ' Simple sample of how to get Open/Save dialog results
              '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
              #COMPILE EXE
              #INCLUDE "WIN32API.INC"
              #INCLUDE "COMDLG32.INC"
               
              DECLARE CALLBACK FUNCTION DlgProc() AS LONG
              DECLARE FUNCTION GetOpenFileDlg(BYVAL hWnd AS LONG) AS STRING
              DECLARE FUNCTION GetSaveFileDlg(BYVAL hWnd AS LONG) AS STRING
               
              '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
              ' Call up File save dialog and get resulting file name, if any
              '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
              FUNCTION GetSaveFileDlg(BYVAL hWnd AS LONG) AS STRING
                LOCAL dTitle  AS STRING 'for dialog's title bar.
                LOCAL fName   AS STRING 'for returned file name
                LOCAL fPath   AS STRING 'for initial path
                LOCAL fType   AS STRING 'for type of files to select among
                LOCAL DefType AS STRING 'for default file type
                LOCAL dStyle  AS DWORD  'for dialog's style
               
                dTitle = ""        'If empty, default title is used.
                fPath   = CURDIR$  'whatever you want to set it to..
                fType   =         "Text Files (*.txt)|*.TXT|"            'list one file type, *.txt
                fType   = fType & "PB Files (*.BAS, *.INC)|*.BAS;*.INC|" 'list multiple file types, *.bas and *.inc
                fType   = fType & "All Files (*.*)|*.*"                  'list all files, *.*
                DefType = "TXT"
                dStyle = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY OR _
                         %OFN_OVERWRITEPROMPT OR %OFN_ENABLESIZING
               
                IF ISFALSE(SaveFileDialog(hWnd, "", fName, fPath, fType, DefType, dStyle)) THEN EXIT FUNCTION
               
                FUNCTION = fName 'return what we got, fName holds selected file name
              END FUNCTION
               
              '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
              ' Call up File save dialog and get resulting file name, if any
              '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
              FUNCTION GetOpenFileDlg(BYVAL hWnd AS LONG) AS STRING
                LOCAL dTitle  AS STRING 'for dialog's title bar.
                LOCAL fName   AS STRING 'for returned file name
                LOCAL fPath   AS STRING 'for initial path
                LOCAL fType   AS STRING 'for type of files to select among
                LOCAL DefType AS STRING 'for default file type
                LOCAL dStyle  AS DWORD  'for dialog's style
               
                dTitle = ""        'If empty, default title is used.
                fPath   = CURDIR$  'whatever you want to set it to..
                fType   =         "Text Files (*.txt)|*.TXT|"            'list one file type, *.txt
                fType   = fType & "PB Files (*.BAS, *.INC)|*.BAS;*.INC|" 'list multiple file types, *.bas and *.inc
                fType   = fType & "All Files (*.*)|*.*"                  'list all files, *.*
                DefType = "TXT"
                dStyle = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST OR %OFN_ENABLESIZING 'remove last one if you
                                                                                   'don't want resizable dialog
                IF ISFALSE(OpenFileDialog(hWnd, "", fName, fPath, fType, DefType, dStyle)) THEN EXIT FUNCTION
               
                   'just to show hor to trap ReadOnly checkbox result. dStyle returns this.
                   'add %OFN_HIDEREADONLY to dStyle and remove this line if not needed.
                   IF (dStyle AND %OFN_READONLY) THEN BEEP
               
                FUNCTION = fName 'return what we got, fName holds selected file name
              END FUNCTION
               
              '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
              ' Main callback
              '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
              CALLBACK FUNCTION DlgProc() AS LONG
                SELECT CASE CBMSG
                   CASE %WM_COMMAND
                      LOCAL fName AS STRING
                      SELECT CASE CBCTL
                         CASE 1  : DIALOG END CBHNDL, 1        'EXIT
               
                         CASE 10                               'OPEN
                            fName = GetOpenFileDlg(CBHNDL)     'Get open file dialog result
                            IF LEN(fName) THEN                 'Note, CBHNDL is dialog's handle.
                               MSGBOX fName                    'You can also use 0, for desktop. :-)
                            ELSE
                               BEEP
                            END IF
               
                         CASE 11                               'SAVE
                            fName = GetSaveFileDlg(CBHNDL)     'Get save file dialog result
                            IF LEN(fName) THEN
                               MSGBOX fName                    '<- do what you want with it..
                            ELSE
                               BEEP
                            END IF
                      END SELECT
                END SELECT
              END FUNCTION
               
              '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
              ' Create dialog and controls, etc
              '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
              FUNCTION PBMAIN () AS LONG
                LOCAL hDlg AS LONG
               
                DIALOG NEW 0, "Open/Save dialog test ",,, 200, 80, %WS_SYSMENU, 0 TO hDlg
               
                CONTROL ADD BUTTON, hDlg, 10, "Open file",  10, 20, 60, 18, 1
                CONTROL ADD BUTTON, hDlg, 11, "Save file",  70, 20, 60, 18, 1
                CONTROL ADD BUTTON, hDlg, 1,  "E&xit",     130, 20, 60, 18, 1
               
                DIALOG SHOW MODAL hDlg CALL DlgProc
               
              END FUNCTION

              ------------------

              Comment


              • #8
                Very nice Borje!!!

                Thank you very much I shall use that.

                Ian


                ------------------

                Comment


                • #9
                  Borje, I combined your two functions into one,,
                  Your functions work great!! thanks.

                  Code:
                  DECLARE FUNCTION GetSaveOpenFileDlg(BYVAL hWnd AS LONG, DType$, Title$, File$) AS STRING
                  
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  ' Call up File save dialog (or open dialog  [img]http://www.powerbasic.com/support/forums/smile.gif[/img] ) and get resulting file name, if any
                  '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                  
                  FUNCTION GetSaveOpenFileDlg(BYVAL hWnd AS LONG, DType$, Title$, File$) AS STRING
                    LOCAL dTitle  AS STRING 'for dialog's title bar.
                    LOCAL fName   AS STRING 'for returned file name
                    LOCAL fPath   AS STRING 'for initial path
                    LOCAL fType   AS STRING 'for type of files to select among
                    LOCAL DefType AS STRING 'for default file type
                    LOCAL dStyle  AS DWORD  'for dialog's style
                  
                    dTitle = Title$        'If empty, default title is used.
                    fName  = File$
                    fPath   = CURDIR$  'whatever you want to set it to..
                    fType   =         "Text Files (*.txt)|*.TXT|"            'list one file type, *.txt
                    fType   = fType & "PB Files (*.BAS, *.INC)|*.BAS;*.INC|" 'list multiple file types, *.bas and *.inc
                    fType   = fType & "All Files (*.*)|*.*"                  'list all files, *.*
                    DefType = "TXT"
                    dStyle = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY OR _
                             %OFN_OVERWRITEPROMPT OR %OFN_ENABLESIZING
                    IF DType$ = "save" THEN
                       IF ISFALSE(SaveFileDialog(hWnd, dTitle, fName, fPath, fType, DefType, dStyle)) THEN EXIT FUNCTION
                    ELSEIF DType$ = "open" THEN
                       IF ISFALSE(OpenFileDialog(hWnd, dTitle, fName, fPath, fType, DefType, dStyle)) THEN EXIT FUNCTION
                    END IF
                  
                    FUNCTION = fName 'return what we got, fName holds selected file name
                  END FUNCTION
                  Thanks again

                  Ian

                  ------------------

                  Comment


                  • #10
                    Time for me to give back some too!
                    Thanks for helping me with this one guys, this is my rendition of multiple file open dialog box


                    Here's a good example for doing the Multiple file dialog box, where you can hold control down and get multiple files at one time.

                    Not sure if everything is declared but should be close (Pulling from this code).
                    This goes in place where a button would be, ie my %ADD_FILES or whatever..


                    Code:
                    Global g_Files(1 to %MAX_FILES) AS STRING 'Put up in top of BAS file
                    Global g_FileSize(1 to %MAX_FILES) AS LONG
                    
                    %MAX_FILES = 10
                    
                    Declare Function MyOpenFileDialog(ByVal hWnd As Long, _                ' parent window
                                            ByVal Caption As String, _           ' caption
                                            Filespec As String, _                ' filename
                                            InitialDir As String, _        ' start directory
                                            ByVal Filter As String, _            ' filename filter
                                            ByVal DefExtension As String, _      ' default extension
                                            Flags As Long) As Long               ' flags
                    
                    
                    'Put in function
                    Local lStyle as long
                    Static CurrentDir as string 'We want to stay in the same directory, be static
                    Static LimitMSg as string
                    Local DuplicateCheck as long
                    Local FileSpec as string
                    Not sure what else I forgot...
                    
                    LimitMSg = "You have reached your file limit" 'or something similiar
                    
                                         lStyle = %OFN_FILEMUSTEXIST Or %OFN_HIDEREADONLY Or %OFN_LONGNAMES Or %OFN_EXPLORER Or %OFN_ALLOWMULTISELECT
                                            g_Result = MyOpenFileDialog(ByVal hDLg,_
                                                                        ByVal Caption,_
                                                                        FileSpec,_
                                                                        CurrentDir,_
                                                                        ByVal Filter,_
                                                                        ByVal DefExtension,_
                                                                        lStyle)
                    
                    
                                            lResult = Tally(FileSpec,Chr$(0))
                                            If lResult <> 0 Then  'Parse out the multiple files
                                               CurrentDir = Parse$(FileSpec,Chr$(0),1)
                                               FileSpec = Right$(FileSpec,Len(FileSpec) - Instr(FileSpec,Chr$(0))) 'REMOVE the directory
                                               'Now FileSpec contains NULL separated files, pull those out
                                               For lLoop = 1 To lResult
                                                   sTmp = CurrentDir & "\" & Parse$(FileSpec,Chr$(0),lLoop)
                                                   Array Scan g_Files(), Collate Ucase, = sTmp, To DuplicateCheck
                    
                                                   If DuplicateCheck Then
                                                      MsgBox "The file " & g_Files(DuplicateCheck) & " is already added", %MB_ICONINFORMATION,g_szCCS
                                                   Else
                                                      Incr g_FileIndex
                                                      If g_FileIndex > %MAX_FILES Then
                                                         MsgBox LimitMsg ,%MB_ICONSTOP,g_szCCS
                                                         Exit Function
                                                      End If
                                                      g_Files(g_FileIndex) = sTmp
                                                      ListBox Add CbHndl,%IDLISTBOX1,g_Files(lLoop)
                                                      g_Filesize(lLoop) = FileSize(g_Files(lLoop)) 'Grab file size here
                                                   End If
                                               Next
                    
                    '                        If IsFalse g_FileIndex Then g_FileIndex = 1 'Prevent GPF if at zero
                                            Else   'Add the single file but First make sure it's not in the list already
                                               CurrentDir = CurDir$
                                               If IsFalse Len(FileSpec) Then Exit Function 'User pressed escape or hit cancel
                                               Incr g_FileIndex
                                                If g_FileIndex > %MAX_FILES Then
                                                   MsgBox LimitMsg ,%MB_ICONSTOP,g_szCCS
                                                   Exit Function
                                                End If
                    
                                                sTmp = Remove$(FileSpec,Chr$(0))
                                                Array Scan g_Files(), Collate Ucase, = sTmp, To DuplicateCheck
                                                If DuplicateCheck Then
                                                    MsgBox "The file " & g_Files(DuplicateCheck) & " is already added", %MB_ICONINFORMATION,g_szCCS
                                                    Exit Function
                                                End If
                                                g_Files(g_FileIndex) = FileSpec
                                                g_Filesize(g_FileIndex) = FileSize(g_Files(g_FileIndex)) 'Grab file size here
                                                ListBox Add hDlg, %IDLISTBOX1, g_Files(g_FileIndex)
                                            End If
                                            If g_FileIndex > 0 Then
                                               Control Enable CbHndl, %IDCOMPILE
                                               g_Result = EnableMenuItem(g_hMenu,%IDCOMPILE,%MF_ENABLED)
                                            End If
                    
                                            If g_FileIndex = %MAX_FILES Then Control Disable hDlg,%IDADDFILES
                    
                                     End If
                    
                    
                    Elsewhere put this if you want filesize
                    Function FileSize(ByVal FileSpec As String) Export As Long
                    Local hDir     As Long
                    Local FindData As WIN32_FIND_DATA
                    'SPT 1/22/01
                    FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
                    hDir = FindFirstFile(ByVal StrPtr(FileSpec), FindData)
                    If hDir = %INVALID_HANDLE_VALUE Then
                    '   MsgBox "DEBUG: " & FileSpec & " has an Invalid Handle In filesize or could not be found"
                       Exit Function     'file not found
                    Else
                       Function = FindData.nFileSizeLow
                    End If
                    FindClose hDir
                    End Function
                    ------------------
                    Scott
                    mailto:[email protected][email protected]</A>
                    Scott Turchin
                    MCSE, MCP+I
                    http://www.tngbbs.com
                    ----------------------
                    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

                    Comment


                    • #11
                      Ian, the error 516 you received on your earlier sample code was due to your PBMAIN() declaration. You forgot to add AS LONG. Ie.

                      FUNCTION PBMAIN() AS LONG



                      ------------------
                      Bernard Ertl
                      Bernard Ertl
                      InterPlan Systems

                      Comment


                      • #12
                        ahh, right, thanks Bern

                        ------------------

                        Comment

                        Working...
                        X