Announcement

Collapse
No announcement yet.

GetOpenFilename - can't get more than one filename extension

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

  • GetOpenFilename - can't get more than one filename extension

    I've been stuck for the last 8 hours on the use of GetOpenFilename(). I swear this code was working correctly when I first used it about 6 years ago, but now I cannot get multiple file extensions to appear. For saving image files I want BMP, JPG, GIF, etc..., and am including those in the Filter, but only BMP shows up.

    The code is below. I've pasted in a printout of the parameters being passed to the routine - why won't the other extensions show up?

    Code:
    'MODIFIED VERSION OF OpenFileDialog     
    '         ALLOWS OPEN & SAVE BY LAST PARAMETER (0/1)
    '         OpenFileDialog was originally part of WIN32API.INC & COM32DLG.INC files
    
     
            TYPE OPENFILENAME
              lStructSize       AS LONG
              hWndOwner         AS LONG
              hInstance         AS LONG
              lpstrFilter       AS ASCIIZ PTR
              lpstrCustomFilter AS ASCIIZ PTR
              nMaxCustFilter    AS LONG
              nFilterIndex      AS LONG
              lpstrFile         AS ASCIIZ PTR
              nMaxFile          AS LONG
              lpstrFileTitle    AS ASCIIZ PTR
              nMaxFileTitle     AS LONG
              lpstrInitialDir   AS ASCIIZ PTR
              lpstrTitle        AS ASCIIZ PTR
              Flags             AS LONG
              nFileOffset       AS INTEGER
              nFileExtension    AS INTEGER
              lpstrDefExt       AS ASCIIZ PTR
              lCustData         AS LONG
              lpfnHook          AS DWORD
              lpTemplateName    AS ASCIIZ PTR
            END TYPE
     
            DECLARE FUNCTION GetOpenFileName LIB "COMDLG32.DLL" ALIAS "GetOpenFileNameA" (lpofn AS OPENFILENAME) AS LONG
            DECLARE FUNCTION GetSaveFileName LIB "COMDLG32.DLL" ALIAS "GetSaveFileNameA" (lpofn AS OPENFILENAME) AS LONG
     
        FUNCTION OpenFileDialog(BYVAL hWnd AS LONG, _            ' parent window
                            BYVAL Caption AS STRING, _           ' caption
                            Filespec AS STRING, _                ' filename
                            BYVAL InitialDir AS STRING, _        ' start directory
                            BYVAL Filter AS STRING, _            ' filename filter
                            BYVAL DefExtension AS STRING, _      ' default extension
                            Flags AS LONG,ibSave AS LONG) AS LONG ' flags & Save=1
     
              LOCAL Ofn           AS OPENFILENAME
              LOCAL zFile         AS ASCIIZ * 256
              LOCAL zFileTitle    AS ASCIIZ * 256
              LOCAL zFilter       AS ASCIIZ * 512
              LOCAL zInitialDir   AS ASCIIZ * 256
              LOCAL zTitle        AS ASCIIZ * 256
              LOCAL zDefExt       AS ASCIIZ * 10
     
    
          'code to print out test values
    'open "testprint.txt" for output as #1
    'PRINT  #1,">>>";hWnd;"<<<"
    'PRINT  #1,">>>";Caption;"<<<"
    'PRINT  #1,">>>";Filespec;"<<<"
    'PRINT  #1,">>>";InitialDir;"<<<"
    'PRINT  #1,">>>";Filter;"<<<"
    'PRINT  #1,">>>";DefExtension;"<<<"
    'PRINT  #1,">>>";Flags;"<<<"
    'PRINT  #1,">>>";ibSave;"<<<"
    'close #1
     
    'printout results - >>> and <<< are to show leading and trailing spaces
    
    '    >>> 3802330 <<<
    '    >>>Please Select or Enter a Filename for Save<<<
    '    >>>RDSimage<<<
    '    >>>C:\RDS-Win<<<
    '    >>>Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|Tagged Image File (*.tif)|*.tif|Graphics Exchange Format (*.gif)|*.gif|All Files (*.*)|*.*|<<<
    '    >>>gif<<<
    '    >>> 8198 <<<
    '    >>> 1 <<<
     
       
              REPLACE " " WITH "_" IN Filter            'added 5-08 so that spaces replaced with _ (not needed?)
              REPLACE "|" WITH CHR$(0) IN Filter
     
              IF LEN(InitialDir) = 0 THEN
                InitialDir = CURDIR$
              END IF
     
              zFilter     = Filter + CHR$(0)
              zInitialDir = InitialDir + CHR$(0)
              zFile       = Filespec + CHR$(0)
              zDefExt     = DefExtension + CHR$(0)
              zTitle      = Caption + CHR$(0)
     
              ofn.lStructSize       = SIZEOF(ofn)
              ofn.hWndOwner         = hWnd
              ofn.lpstrFilter       = VARPTR(zFilter)
              ofn.nFilterIndex      = 1
              ofn.lpstrFile         = VARPTR(zFile)
              ofn.nMaxFile          = SIZEOF(zFile)
              ofn.lpstrFileTitle    = VARPTR(zFileTitle)
              ofn.nMaxFileTitle     = SIZEOF(zFileTitle)
              ofn.lpstrInitialDir   = VARPTR(zInitialDir)
              IF LEN(zTitle) THEN
                ofn.lpstrTitle      = VARPTR(zTitle)
              END IF
              ofn.Flags             = Flags
              ofn.lpstrDefExt       = VARPTR(zDefExt)
     
              IF ibSave=0 THEN
              FUNCTION = GetOpenFilename(ofn)
              ELSE
              FUNCTION = GetSaveFilename(ofn)
              END IF
     
              Filespec = zFile
              Flags    = ofn.Flags
     
        END FUNCTION

  • #2
    Flags values used

    FYI:

    Flags&=&H00000004&+&H00002000&+&H00000002&
    'hidereadonly + createprompt + overwriteprompt

    Comment


    • #3
      Don't use an asciiz string to store an string with embedded nulls.

      Instead of

      Code:
      zFilter = Filter + CHR$(0)
      ofn.lpstrFilter = VARPTR(zFilter)
      use

      Code:
      ofn.lpstrFilter = STRPTR(Filter)
      Forum: http://www.jose.it-berater.org/smfforum/index.php

      Comment


      • #4
        That Worked!

        Awsome! Now why didn't I think of that?

        Thanks!

        Comment


        • #5
          Here's the code - all fixed and cleaned

          Code:
          '----------------------------------------------------------------------------------------------
          '           RDS MODIFIED VERSION OF OpenFileDialog                                6-01 rev 5-08
          '----------------------------------------------------------------------------------------------
          '       ALLOWS OPEN & SAVE BY LAST PARAMETER (0/1)
          '       OpenFileDialog was originally part of WIN32API.INC & COM32DLG.INC files
          '       If user changes directory when getting file, this changes directory for whole program!
          
              TYPE OPENFILENAME
                    lStructSize       AS LONG
                    hWndOwner         AS LONG
                    hInstance         AS LONG
                    lpstrFilter       AS ASCIIZ PTR
                    lpstrCustomFilter AS ASCIIZ PTR
                    nMaxCustFilter    AS LONG
                    nFilterIndex      AS LONG
                    lpstrFile         AS ASCIIZ PTR
                    nMaxFile          AS LONG
                    lpstrFileTitle    AS ASCIIZ PTR
                    nMaxFileTitle     AS LONG
                    lpstrInitialDir   AS ASCIIZ PTR
                    lpstrTitle        AS ASCIIZ PTR
                    Flags             AS LONG
                    nFileOffset       AS INTEGER
                    nFileExtension    AS INTEGER
                    lpstrDefExt       AS ASCIIZ PTR
                    lCustData         AS LONG
                    lpfnHook          AS DWORD
                    lpTemplateName    AS ASCIIZ PTR
                  END TYPE
          
              DECLARE FUNCTION GetOpenFileName LIB "COMDLG32.DLL" ALIAS "GetOpenFileNameA" (lpofn AS OPENFILENAME) AS LONG
              DECLARE FUNCTION GetSaveFileName LIB "COMDLG32.DLL" ALIAS "GetSaveFileNameA" (lpofn AS OPENFILENAME) AS LONG
          
              FUNCTION OpenFileDialog(BYVAL hWnd AS LONG, _            ' parent window
                                  BYVAL Caption AS STRING, _           ' caption
                                  Filespec AS STRING, _                ' filename
                                  BYVAL InitialDir AS STRING, _        ' start directory
                                  BYVAL Filter AS STRING, _            ' filename filter
                                  BYVAL DefExtension AS STRING, _      ' default extension
                                  Flags AS LONG,ibSave AS LONG) AS LONG ' flags & Save=1
          
                    LOCAL Ofn           AS OPENFILENAME
                    LOCAL zFile         AS ASCIIZ * 256
                    LOCAL zFileTitle    AS ASCIIZ * 256
                    LOCAL zFilter       AS ASCIIZ * 512
                    LOCAL zInitialDir   AS ASCIIZ * 256
                    LOCAL zTitle        AS ASCIIZ * 256
                    LOCAL zDefExt       AS ASCIIZ * 10
             
                    REPLACE "|" WITH CHR$(0) IN Filter
                    
                    IF LEN(InitialDir) = 0 THEN InitialDir = CURDIR$
                    zInitialDir           = InitialDir + CHR$(0)
                    zFile                 = Filespec + CHR$(0)
                    zDefExt               = DefExtension + CHR$(0)
                    zTitle                = Caption + CHR$(0)
                    ofn.lStructSize       = SIZEOF(ofn)
                    ofn.hWndOwner         = hWnd
                    ofn.lpstrFilter       = STRPTR(Filter)
                    ofn.nFilterIndex      = 1
                    ofn.lpstrFile         = VARPTR(zFile)
                    ofn.nMaxFile          = SIZEOF(zFile)
                    ofn.lpstrFileTitle    = VARPTR(zFileTitle)
                    ofn.nMaxFileTitle     = SIZEOF(zFileTitle)
                    ofn.lpstrInitialDir   = VARPTR(zInitialDir)
                    IF LEN(zTitle) THEN ofn.lpstrTitle      = VARPTR(zTitle)
                    ofn.Flags             = Flags
                    ofn.lpstrDefExt       = VARPTR(zDefExt)
           
                    IF ibSave=0 THEN FUNCTION = GetOpenFilename(ofn) ELSE FUNCTION = GetSaveFilename(ofn)
                    Filespec = zFile
                    Flags    = ofn.Flags
          
              END FUNCTION

          Comment


          • #6
            FYI:
            Flags&=&H00000004&+&H00002000&+&H00000002&
            'hidereadonly + createprompt + overwriteprompt
            FYI....

            1. You do not ADD flags, you use the boolean OR to combine them. While in some cases (eg this one) it will not matter, OR will always give the correct result; adding may not.

            2. (style points). Don't use inline numeric literals; use the symbolic equates.

            Corrected and Maintainable code:
            Code:
             flags = %OFN_HIDEREADONLY OR %OFN_CREATEPROMPT OR %OFN_OVERWRITEPROMPT
            MCM
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment

            Working...
            X