Common dialog : Save
Le code suivant vous permet d'afficher la boite de dialogue "Enregistrer" par API:
'---- Définition de quelques masques prédéfinis ---- Public Const SO_ALL_FILES = "Tous les fichiers (*.*),*.*" Public Const SO_PICTURES = "Fichiers image (*.bmp; *.jpg; *.gif; *.png; *.ico; *.cur),*.bmp;*.jpg;*.gif;*.png;*.ico;*.cur" Public Const SO_BITMAP = "Image bitmap (*.bmp),*.bmp" Public Const SO_TEXT = "Document texte (*.txt),*.txt" Public Const SO_DATA = "Fichier de données (*.dat),*.dat" Public Const SO_WAV = "Sons (*.wav),*.wav" '----- Définition des flags pouvant être choisis ----- Public Enum OFN_flags OFN_ALLOWMULTISELECT = &H200 OFN_CREATEPROMPT = &H2000 OFN_EXPLORER = &H80000 ' new look commdlg OFN_EXTENSIONDIFFERENT = &H400 OFN_FILEMUSTEXIST = &H1000 OFN_HIDEREADONLY = &H4 OFN_LONGNAMES = &H200000 ' force long names for 3.x modules OFN_NOCHANGEDIR = &H8 OFN_NOLONGNAMES = &H40000 ' force no long names for 4.x modules OFN_NODEREFERENCELINKS = &H100000 OFN_NONETWORKBUTTON = &H20000 OFN_NOREADONLYRETURN = &H8000 OFN_NOTESTFILECREATE = &H10000 OFN_NOVALIDATE = &H100 OFN_OVERWRITEPROMPT = &H2 OFN_PATHMUSTEXIST = &H800 OFN_READONLY = &H1 OFN_SHAREAWARE = &H4000 OFN_SHAREFALLTHROUGH = 2 OFN_SHARENOWARN = 1 OFN_SHAREWARN = 0 OFN_SHOWHELP = &H10 OFN_ENABLEHOOK = &H20 OFN_ENABLETEMPLATE = &H40 OFN_ENABLETEMPLATEHANDLE = &H80 End Enum '----- Déclaration des API ----- 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 Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long 'Procédure d'affichage de la boite de dialogue Function ShowSave(Optional InitFile As String, Optional Flags As OFN_flags, Optional Filter As String, Optional SelectedFilter As Byte = 1, Optional FilterSeparator As String = ",", Optional InitialDir As String, Optional Title As String) As String() Dim OFNStruct As OPENFILENAME, sTempTable() As String 'Association des paramètres passés à la fonction avec la structure principale OFNStruct.lStructSize = Len(OFNStruct) OFNStruct.lpstrFilter = Replace(Filter, FilterSeparator, Chr(0)) & Chr(0) & Chr(0) OFNStruct.nFilterIndex = SelectedFilter OFNStruct.lpstrInitialDir = InitialDir & Chr(0) OFNStruct.lpstrTitle = Title & Chr(0) OFNStruct.Flags = Flags OFNStruct.lpstrFile = InitFile & String(260 - Len(InitFile), Chr(0)) & Chr(0) OFNStruct.nMaxFile = 2600 'Si on a pu choisir le fichier If GetSaveFileName(OFNStruct) Then 'Si plusieurs fichiers sont retournés If InStr(1, OFNStruct.lpstrFile, Chr(0) & Chr(0)) <> 0 Then 'On retrouve le nom des fichiers OFNStruct.lpstrFile = Left(OFNStruct.lpstrFile, InStr(1, OFNStruct.lpstrFile, Chr(0) & Chr(0))) Else 'Si un seul fichier 'On retrouve le nom du fichier OFNStruct.lpstrFile = Left(OFNStruct.lpstrFile, InStr(1, OFNStruct.lpstrFile, Chr(0))) End If 'Initialisation du tableau et de la première entrée ReDim sTempTable(0) sTempTable(0) = Left(OFNStruct.lpstrFile, InStr(1, OFNStruct.lpstrFile, Chr(0)) - 1) OFNStruct.lpstrFile = Right(OFNStruct.lpstrFile, Len(OFNStruct.lpstrFile) - Len(sTempTable(0)) - 1) 'Boucle d'ajout des noms de fichiers Do Until InStr(1, OFNStruct.lpstrFile, Chr(0)) = 0 ReDim Preserve sTempTable(UBound(sTempTable) + 1) sTempTable(UBound(sTempTable)) = Left(OFNStruct.lpstrFile, InStr(1, OFNStruct.lpstrFile, Chr(0)) - 1) OFNStruct.lpstrFile = Right(OFNStruct.lpstrFile, Len(OFNStruct.lpstrFile) - Len(sTempTable(UBound(sTempTable))) - 1) Loop End If 'On retourne le tableau ShowSave = sTempTable End Function