Standarddialog: Datei öffnen und speichern
[Windows 95/98/NT/2000]

10.11.1999


Diese Standarddialoge müssen Sie nicht mit dem
    CommonDialog-Steuerelement darstellen.
    Es geht auch mittels API-Funktionen.

ERFORDERLICHE OBJEKTE
   2 Commandbuttons (Command1, Command2)
 FORM-CODE
   Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
      "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
   Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias _
      "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
   Private Type OPENFILENAME
      lStructSize As Long
      hwndOwner As Long
      hInstance As Long
      lpstrFilter As String
      lpstrCustomFilter As String
      nMaxCustFilter As Long
      nFilterIndex As Long
      lpstrFile As String
      nMaxFile As Long
      lpstrFileTitle As String
      nMaxFileTitle As Long
      lpstrInitialDir As String
      lpstrTitle As String
      flags As Long
      nFileOffset As Integer
      nFileExtension As Integer
      lpstrDefExt As String
      lCustData As Long
      lpfnHook As Long
      lpTemplateName As String
   End Type
   Private Const OFN_HIDEREADONLY = &H4
   Private Const OFN_PATHMUSTEXIST = &H800
   Private Const OFN_FILEMUSTEXIST = &H1000
   Private Const OFN_OVERWRITEPROMPT = &H2
   Function DialogOpenSave$(F As Form, ByVal ftitle$, _
      fpfad$, ByVal ffile$, ByVal ffilter$, ByVal fext$, SaveFile%)
      Dim O As OPENFILENAME
      Dim wSize As Long
      Dim Memhandle As Long
      szFile$ = ffile$ + String$(128 - Len(ffile$), 0)
      szFilter$ = ffilter$ + "||"
      Do While InStr(szFilter$, "|")
         szFilter$ = Left$(szFilter$, InStr(szFilter$, "|") - 1) + _
            Chr$(0) + Mid$(szFilter$, InStr(szFilter$, "|") + 1)
      Loop
      O.lStructSize = Len(O)
      O.hwndOwner = F.hWnd
      O.nFilterIndex = 1
      O.nMaxFile = Len(szFile$)
      Select Case SaveFile%
         Case 0:
            O.flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or  _
               OFN_FILEMUSTEXIST
         Case 1:
            O.flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or _
               OFN_OVERWRITEPROMPT
         Case 2:
            O.flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST 
     End Select
      O.lpstrFile = szFile$
      O.lpstrFilter = szFilter$
      O.lpstrTitle = ftitle$
      O.lpstrDefExt = fext$
      O.lpstrInitialDir = fpfad$
      If SaveFile% = 0 Then
         result = GetOpenFileName(O) 
      Else 
         result = GetSaveFileName(O)
      End If
      file$ = Left$(O.lpstrFile, InStr(O.lpstrFile, Chr$(0)) - 1)
      If result = 0 Then DialogOpenSave$ = "": Exit Function
      fpfad$ = Left$(file$, O.nFileOffset)
      DialogOpenSave$ = Right$(file$, Len(file$) - O.nFileOffset)
   End Function
   Function Dialog_Open$(F As Form, ByVal ftitle$, fpfad$, _
      ByVal ffile$, ByVal ffilter$, ByVal fext$)
      Dialog_Open$ = DialogOpenSave$(F, ftitle$, fpfad$, _
         ffile$, ffilter$, fext$, 0)
   End Function
   Function Dialog_Save$(F As Form, ByVal ftitle$, fpfad$, _
      ByVal ffile$, ByVal ffilter$, ByVal fext$)
      Dialog_Save$ = DialogOpenSave$(F, ftitle$, fpfad$, _
         ffile$, ffilter$, fext$, 1)
   End Function
   Private Sub Command1_Click()
      p$ = CurDir$ 'Vorgabe-Pfad
      F$ = "" 'Vorgabe-Datei
      a$ = Dialog_Open$(Me, "Datei öffnen", p$, F$, _
         "Text-Dateien|*.txt|Alle    Dateien|*.*", "txt")
      If Right$(p$, 1) <> "\" Then p$ = p$ + "\"
      If Len(a$) Then MsgBox p$ + a$
   End Sub
   Private Sub Command2_Click()
      p$ = CurDir$ 'Vorgabe-Pfad
      F$ = "test.txt" 'Vorgabe-Datei
      a$ = Dialog_Save$(Me, "Datei speichern", p$, F$, _
         "Text-Dateien|*.txt|Alle Dateien|*.*", "txt")
      If Right$(p$, 1) <> "\" Then p$ = p$ + "\"
      If Len(a$) Then MsgBox p$ + a$
   End Sub
   Private Sub Form_Load()
      Command1.Caption = "Öffnen..."
      Command2.Caption = "Speichern..."
  End Sub
 
Download - 3 KB