Results 1 to 5 of 5

Thread: Open Dialog API

  1. #1

    Thread Starter
    Addicted Member Monkey Man's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere deep in the Caribbean.....
    Posts
    202

    Question Open Dialog API

    O.k - my arms been twisted and I've decided to enter the world of using API's. I've read Karle Moores introduction but am still stuck.

    Could you please give me an example of using the Open Dialog box, including the API call and how to call it from the form (plus how do you reference the value that is returned - i.e with comdiag control it is Commondialog1.filename)

    TIA

  2. #2

    Thread Starter
    Addicted Member Monkey Man's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere deep in the Caribbean.....
    Posts
    202
    Don't worry people, I've exhausted my 100 free hours on AOL looking but I found it in the end:

    VB Code:
    1. Private Type OPENFILENAME
    2.   lStructSize As Long
    3.   hwndOwner As Long
    4.   hInstance As Long
    5.   lpstrFilter As String
    6.   lpstrCustomFilter As String
    7.   nMaxCustFilter As Long
    8.   nFilterIndex As Long
    9.   lpstrFile As String
    10.   nMaxFile As Long
    11.   lpstrFileTitle As String
    12.   nMaxFileTitle As Long
    13.   lpstrInitialDir As String
    14.   lpstrTitle As String
    15.   flags As Long
    16.   nFileOffset As Integer
    17.   nFileExtension As Integer
    18.   lpstrDefExt As String
    19.   lCustData As Long
    20.   lpfnHook As Long
    21.   lpTemplateName As String
    22. End Type
    23.  
    24. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
    25. "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    26.  
    27. Private Sub Command1_Click()
    28. Dim ofn As OPENFILENAME
    29. ofn.lStructSize = Len(ofn)
    30. ofn.hwndOwner = Form.Hwnd
    31. ofn.lpstrFilter = "Access Databases (*.mdb)"
    32. ofn.lpstrFile = Space$(254)
    33. ofn.nMaxFile = 255
    34. ofn.lpstrFileTitle = Space$(254)
    35. ofn.nMaxFileTitle = 255
    36. ofn.lpstrInitialDir = CurDir
    37. ofn.lpstrTitle = "Select Database" ' the title of the open dialog window goes here
    38. ofn.flags = 0
    39. Dim a
    40. a = GetOpenFileName(ofn)
    41.  
    42. If (a) Then
    43.   MsgBox "File selected was " + Trim$(ofn.lpstrFile)
    44. Else
    45.   MsgBox "The user pressed the cancel button"
    46. End If
    47. End Sub

    Thnx Anyhow

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I was doing some searching too. You were just faster than I was, but I'm going to post what I found anyway. This has a bunch more stuff than what you found, and you might find some of it useful.
    VB Code:
    1. 'type that passes/returns value through ShowOpenDialog function
    2. Private Type stcFileStruct
    3.     strFileName     As String
    4.     strFileTitle    As String
    5.     strFilter       As String
    6.     strDialogtitle  As String
    7.     lngFilterIndex  As Long
    8.     blnReadOnly     As Boolean
    9. End Type
    10. 'Max filename and path constants
    11. Private Const cMaxPath = 260
    12. Private Const cMaxFile = 260
    13. 'Open File name type
    14. Private Type OPENFILENAME
    15.     lStructSize As Long           ' Filled with UDT size
    16.     hwndOwner As Long             ' Tied to Owner
    17.     hInstance As Long             ' Ignored (used only by templates)
    18.     lpstrFilter As String        ' Tied to Filter
    19.     lpstrCustomFilter As String  ' Ignored
    20.     nMaxCustFilter As Long       ' Ignored
    21.     nFilterIndex As Long         ' Tied to FilterIndex
    22.     lpstrFile As String           ' Tied to FileName
    23.     nMaxFile As Long              ' Handled internally
    24.     lpstrFileTitle As String     ' Tied to FileTitle
    25.     nMaxFileTitle As Long        ' Handled internally
    26.     lpstrInitialDir As String    ' Tied to InitDir
    27.     lpstrTitle As String         ' Tied to DlgTitle
    28.     Flags As Long                 ' Tied to Flags
    29.     nFileOffset As Integer       ' Ignored
    30.     nFileExtension As Integer    ' Ignored
    31.     lpstrDefExt As String        ' Tied to DefaultExt
    32.     lCustData As Long             ' Ignored (needed for hooks)
    33.     lpfnHook As Long              ' Ignored (good luck with hooks)
    34.     lpTemplateName As Long       ' Ignored (good luck with templates)
    35. End Type
    36.  
    37. Private Declare Function GetOpenFileName Lib "COMDLG32" Alias "GetOpenFileNameA" (File As OPENFILENAME) As Long
    38. 'flags
    39. Private Enum EOpenFile
    40.     OFN_READONLY = &H1
    41.     OFN_OVERWRITEPROMPT = &H2
    42.     OFN_HIDEREADONLY = &H4
    43.     OFN_NOCHANGEDIR = &H8
    44.     OFN_SHOWHELP = &H10
    45.     OFN_ENABLEHOOK = &H20
    46.     OFN_ENABLETEMPLATE = &H40
    47.     OFN_ENABLETEMPLATEHANDLE = &H80
    48.     OFN_NOVALIDATE = &H100
    49.     OFN_ALLOWMULTISELECT = &H200
    50.     OFN_EXTENSIONDIFFERENT = &H400
    51.     OFN_PATHMUSTEXIST = &H800
    52.     OFN_FILEMUSTEXIST = &H1000
    53.     OFN_CREATEPROMPT = &H2000
    54.     OFN_SHAREAWARE = &H4000
    55.     OFN_NOREADONLYRETURN = &H8000
    56.     OFN_NOTESTFILECREATE = &H10000
    57.     OFN_NONETWORKBUTTON = &H20000
    58.     OFN_NOLONGNAMES = &H40000
    59.     OFN_EXPLORER = &H80000
    60.     OFN_NODEREFERENCELINKS = &H100000
    61.     OFN_LONGNAMES = &H200000
    62. End Enum
    63.  
    64. 'Main function
    65. Private Function VBGetOpenFileName(FileName As String, _
    66.                            Optional FileTitle As String, _
    67.                            Optional FileMustExist As Boolean = True, _
    68.                            Optional MultiSelect As Boolean = False, _
    69.                            Optional ReadOnly As Boolean = False, _
    70.                            Optional HideReadOnly As Boolean = False, _
    71.                            Optional filter As String = "All (*.*)| *.*", _
    72.                            Optional FilterIndex As Long = 1, _
    73.                            Optional InitDir As String, _
    74.                            Optional DlgTitle As String, _
    75.                            Optional DefaultExt As String, _
    76.                            Optional Owner As Long = -1, _
    77.                            Optional Flags As Long = 0) As Boolean
    78.  
    79.     Dim opfile As OPENFILENAME, s As String, afFlags As Long
    80. With opfile
    81.     .lStructSize = Len(opfile)
    82.  
    83.     ' Add in specific flags and strip out non-VB flags
    84.     .Flags = (-FileMustExist * OFN_FILEMUSTEXIST) Or _
    85.              (-MultiSelect * OFN_ALLOWMULTISELECT) Or _
    86.              (-ReadOnly * OFN_READONLY) Or _
    87.              (-HideReadOnly * OFN_HIDEREADONLY) Or _
    88.              (Flags And CLng(Not (OFN_ENABLEHOOK Or _
    89.                                   OFN_ENABLETEMPLATE)))
    90.     ' Owner can take handle of owning window
    91.     If Owner <> -1 Then .hwndOwner = Owner
    92.     ' InitDir can take initial directory string
    93.     .lpstrInitialDir = InitDir
    94.     ' DefaultExt can take default extension
    95.     .lpstrDefExt = DefaultExt
    96.     ' DlgTitle can take dialog box title
    97.     .lpstrTitle = DlgTitle
    98.  
    99.     ' To make Windows-style filter, replace | and : with nulls
    100.     Dim ch As String, i As Integer
    101.     For i = 1 To Len(filter)
    102.         ch = Mid$(filter, i, 1)
    103.         If ch = "|" Or ch = ":" Then
    104.             s = s & vbNullChar
    105.         Else
    106.             s = s & ch
    107.         End If
    108.     Next
    109.     ' Put double null at end
    110.     s = s & vbNullChar & vbNullChar
    111.     .lpstrFilter = s
    112.     .nFilterIndex = FilterIndex
    113.  
    114.     ' Pad file and file title buffers to maximum path
    115.     s = FileName & String$(cMaxPath - Len(FileName), 0)
    116.     .lpstrFile = s
    117.     .nMaxFile = cMaxPath
    118.     s = FileTitle & String$(cMaxFile - Len(FileTitle), 0)
    119.     .lpstrFileTitle = s
    120.     .nMaxFileTitle = cMaxFile
    121.     ' All other fields set to zero
    122.  
    123.     If GetOpenFileName(opfile) Then
    124.         VBGetOpenFileName = True
    125.         FileName = StrZToStr(.lpstrFile)
    126.         FileTitle = StrZToStr(.lpstrFileTitle)
    127.         Flags = .Flags
    128.         ' Return the filter index
    129.         FilterIndex = .nFilterIndex
    130.         ' Look up the filter the user selected and return that
    131.         filter = FilterLookup(.lpstrFilter, FilterIndex)
    132.         If (.Flags And OFN_READONLY) Then ReadOnly = True
    133.     Else
    134.         VBGetOpenFileName = False
    135.         FileName = Empty
    136.         FileTitle = Empty
    137.         Flags = 0
    138.         FilterIndex = -1
    139.         filter = Empty
    140.     End If
    141. End With
    142. End Function
    143.  
    144. 'convert the filter to standard required by windows api
    145. Private Function FilterLookup(ByVal sFilters As String, ByVal iCur As Long) As String
    146.     Dim iStart As Long, iEnd As Long, s As String
    147.     iStart = 1
    148.     If sFilters = Empty Then Exit Function
    149.     Do
    150.         ' Cut out both parts marked by null character
    151.         iEnd = InStr(iStart, sFilters, vbNullChar)
    152.         If iEnd = 0 Then Exit Function
    153.         iEnd = InStr(iEnd + 1, sFilters, vbNullChar)
    154.         If iEnd Then
    155.             s = Mid$(sFilters, iStart, iEnd - iStart)
    156.         Else
    157.             s = Mid$(sFilters, iStart)
    158.         End If
    159.         iStart = iEnd + 1
    160.         If iCur = 1 Then
    161.             FilterLookup = s
    162.             Exit Function
    163.         End If
    164.         iCur = iCur - 1
    165.     Loop While iCur
    166. End Function
    167.  
    168. 'show open dialog function (pass/return filestruct)
    169. Private Function ShowOpenDialog(filestruct As stcFileStruct) As Boolean
    170.     With filestruct
    171.         If .strFilter = Empty Then .strFilter = "All Files|*.*"
    172.         ShowOpenDialog = VBGetOpenFileName(.strFileName, .strFileTitle, True, , .blnReadOnly, , .strFilter, .lngFilterIndex, , .strDialogtitle)
    173.     End With
    174.     StripFileStruct filestruct 'Return FileStruct
    175. End Function
    176. 'Removes nulls from the two strings in stcFileStruct
    177. Private Sub StripFileStruct(filestruct As stcFileStruct)
    178.     With filestruct
    179.         .strFileName = StripTerminator(.strFileName)
    180.         .strFileTitle = StripTerminator(.strFileTitle)
    181.     End With
    182. End Sub
    183.  
    184. 'Removes trailing nulls from a string
    185. Private Function StripTerminator(ByVal strString As String) As String
    186.     Dim intZeroPos As Integer
    187.     intZeroPos = InStr(strString, Chr$(0))
    188.     If intZeroPos > 0 Then
    189.         StripTerminator = Left$(strString, intZeroPos - 1)
    190.     Else
    191.         StripTerminator = strString
    192.     End If
    193. End Function
    194. Private Function StrZToStr(s As String) As String
    195.     StrZToStr = Left$(s, Len(s))
    196. End Function
    197.  
    198. 'Form Code
    199. Private Sub cmdOpen_Click()
    200.     Dim File As stcFileStruct
    201.     'fill values (not required)
    202.     File.strDialogtitle = "Select file to open"
    203.     File.strFilter = "Text Files *.txt|*.txt" 'use same format as commondialog Control
    204.     'pass stcFileStruct
    205.     ShowOpenDialog File
    206.     'get return values (passed back through type)
    207.     With File
    208.         MsgBox "FileName: " & .strFileName
    209.         MsgBox "ReadOnly: " & .blnReadOnly
    210.         MsgBox "FileTitle: " & .strFileTitle
    211.         MsgBox "Filter Index: " & .lngFilterIndex
    212.     End With
    213. End Sub

  4. #4

    Thread Starter
    Addicted Member Monkey Man's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere deep in the Caribbean.....
    Posts
    202

    Smile

    Cor Blimey Guvner, I thought I had the hang of all this API stuff before you posted that monsterous code.

    Urrmmm, the tutorial on this site didn't quite prepare me for that, does anyone know of any other sites that might help me get my head round it, cos the site I got the code from wasn't much help in explaining either

    Thanks for your reply though Hack, at least know I know what I was missing out on........

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    You didn't say what site you have been visiting, so if I'm giving you something you already have, my apologies. There are two sites I use extenstively.

    http://www.allapi.net/
    http://www.vbapi.com/

    Allapi.Net has a free API Viewer for downloading. I would strongly urge you to do so.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width