Accueil > > > BOITE DE DIALOGUE OUVRIR/ENREGISTRER
BOITE DE DIALOGUE OUVRIR/ENREGISTRER
Information sur la source
Description
Ce code vous permet d'appeler directement une boite de dialogue ouvrir ou enregistrer sans nécissiter de référencer comdlg32.dll dans votre projet. Créer un module de classe et nommer le "CFileDialog".
Source
- 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 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 m_strDefaultExt As String
- Private m_strDialogTitle As String
- Private m_strFileName As String
- Private m_strFileTitle As String
- Private m_strInitialDir As String
- Private m_strFilter As String
- Private m_intFilterIndex As Integer
- Private m_intMaxFileSize As Integer
- Private m_lnghWndParent As Long
-
- Private Const cintMaxFileLength As Integer = 260
-
- Public Property Get DefaultExt() As String
- DefaultExt = m_strDefaultExt
- End Property
-
- Public Property Let DefaultExt(ByVal strValue As String)
- m_strDefaultExt = strValue
- End Property
-
- Public Property Get DialogTitle() As String
- DialogTitle = m_strDialogTitle
- End Property
-
- Public Property Let DialogTitle(ByVal strValue As String)
- m_strDialogTitle = strValue
- End Property
-
- Public Property Get FileName() As String
- FileName = m_strFileName
- End Property
-
- Public Property Let FileName(ByVal strValue As String)
- m_strFileName = strValue
- End Property
-
- Public Property Get FileTitle() As String
- FileTitle = m_strFileTitle
- End Property
-
- Public Property Let FileTitle(ByVal strValue As String)
- m_strFileTitle = strValue
- End Property
-
- Public Property Get Filter() As String
- Filter = m_strFilter
- End Property
-
- Public Property Let Filter(ByVal strValue As String)
- m_strFilter = strValue
- End Property
-
- Public Property Get FilterIndex() As Integer
- FilterIndex = m_intFilterIndex
- End Property
-
- Public Property Let FilterIndex(ByVal intValue As Integer)
- m_intFilterIndex = intValue
- End Property
-
- Public Property Get hWndParent() As Long
- hWndParent = m_lnghWndParent
- End Property
-
- Public Property Let hWndParent(ByVal lngValue As Long)
- m_lnghWndParent = lngValue
- End Property
-
- Public Property Get InitialDir() As String
- InitialDir = m_strInitialDir
- End Property
-
- Public Property Let InitialDir(ByVal strValue As String)
- m_strInitialDir = strValue
- End Property
-
- Public Property Get MaxFileSize() As Integer
- MaxFileSize = m_intMaxFileSize
- End Property
-
- Public Property Let MaxFileSize(ByVal intValue As Integer)
- m_intMaxFileSize = intValue
- End Property
-
- Public Function Show(fOpen As Boolean) As Boolean
-
- Dim of As OPENFILENAME
- Dim strChar As String * 1
- Dim intCounter As Integer
- Dim strTemp As String
-
- On Error GoTo PROC_ERR
-
- of.lpstrTitle = m_strDialogTitle & ""
- of.Flags = &H80000
- of.lpstrDefExt = m_strDefaultExt & ""
- of.lStructSize = LenB(of)
- of.lpstrFilter = m_strFilter & "||"
- of.nFilterIndex = m_intFilterIndex
-
- For intCounter = 1 To Len(m_strFilter)
- strChar = Mid$(m_strFilter, intCounter, 1)
- If strChar = "|" Then
- strTemp = strTemp & vbNullChar
- Else
- strTemp = strTemp & strChar
- End If
- Next
-
-
- strTemp = strTemp & vbNullChar & vbNullChar
- of.lpstrFilter = strTemp
-
- strTemp = m_strFileName & String$(cintMaxFileLength - Len(m_strFileName), 0)
- of.lpstrFile = strTemp
- of.nMaxFile = cintMaxFileLength
-
- strTemp = m_strFileTitle & String$(cintMaxFileLength - Len(m_strFileTitle), 0)
- of.lpstrFileTitle = strTemp
- of.lpstrInitialDir = m_strInitialDir
- of.nMaxFileTitle = cintMaxFileLength
- of.hwndOwner = m_lnghWndParent
-
- If fOpen Then
- If GetOpenFileName(of) Then
- Show = True
- m_strFileName = TrimNulls(of.lpstrFile)
- m_strFileTitle = TrimNulls(of.lpstrFileTitle)
- Else
- Show = False
- End If
- Else
- If GetSaveFileName(of) Then
- Show = True
- m_strFileName = TrimNulls(of.lpstrFile)
- m_strFileTitle = TrimNulls(of.lpstrFileTitle)
- Else
- Show = False
- End If
- End If
-
- PROC_EXIT:
- Exit Function
-
- PROC_ERR:
- MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
- "Show"
- Resume PROC_EXIT
-
- End Function
-
- Private Function TrimNulls(ByVal strIn As String) As String
- Dim intPos As Integer
-
- On Error GoTo PROC_ERR
-
- intPos = InStr(strIn, vbNullChar)
-
- If intPos = 0 Then
- TrimNulls = strIn
- Else
- If intPos = 1 Then
- TrimNulls = ""
- Else
- TrimNulls = Left$(strIn, intPos - 1)
- End If
- End If
-
- PROC_EXIT:
- Exit Function
-
- PROC_ERR:
- MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
- "TrimNulls"
- Resume PROC_EXIT
-
- End Function
-
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 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 m_strDefaultExt As String
Private m_strDialogTitle As String
Private m_strFileName As String
Private m_strFileTitle As String
Private m_strInitialDir As String
Private m_strFilter As String
Private m_intFilterIndex As Integer
Private m_intMaxFileSize As Integer
Private m_lnghWndParent As Long
Private Const cintMaxFileLength As Integer = 260
Public Property Get DefaultExt() As String
DefaultExt = m_strDefaultExt
End Property
Public Property Let DefaultExt(ByVal strValue As String)
m_strDefaultExt = strValue
End Property
Public Property Get DialogTitle() As String
DialogTitle = m_strDialogTitle
End Property
Public Property Let DialogTitle(ByVal strValue As String)
m_strDialogTitle = strValue
End Property
Public Property Get FileName() As String
FileName = m_strFileName
End Property
Public Property Let FileName(ByVal strValue As String)
m_strFileName = strValue
End Property
Public Property Get FileTitle() As String
FileTitle = m_strFileTitle
End Property
Public Property Let FileTitle(ByVal strValue As String)
m_strFileTitle = strValue
End Property
Public Property Get Filter() As String
Filter = m_strFilter
End Property
Public Property Let Filter(ByVal strValue As String)
m_strFilter = strValue
End Property
Public Property Get FilterIndex() As Integer
FilterIndex = m_intFilterIndex
End Property
Public Property Let FilterIndex(ByVal intValue As Integer)
m_intFilterIndex = intValue
End Property
Public Property Get hWndParent() As Long
hWndParent = m_lnghWndParent
End Property
Public Property Let hWndParent(ByVal lngValue As Long)
m_lnghWndParent = lngValue
End Property
Public Property Get InitialDir() As String
InitialDir = m_strInitialDir
End Property
Public Property Let InitialDir(ByVal strValue As String)
m_strInitialDir = strValue
End Property
Public Property Get MaxFileSize() As Integer
MaxFileSize = m_intMaxFileSize
End Property
Public Property Let MaxFileSize(ByVal intValue As Integer)
m_intMaxFileSize = intValue
End Property
Public Function Show(fOpen As Boolean) As Boolean
Dim of As OPENFILENAME
Dim strChar As String * 1
Dim intCounter As Integer
Dim strTemp As String
On Error GoTo PROC_ERR
of.lpstrTitle = m_strDialogTitle & ""
of.Flags = &H80000
of.lpstrDefExt = m_strDefaultExt & ""
of.lStructSize = LenB(of)
of.lpstrFilter = m_strFilter & "||"
of.nFilterIndex = m_intFilterIndex
For intCounter = 1 To Len(m_strFilter)
strChar = Mid$(m_strFilter, intCounter, 1)
If strChar = "|" Then
strTemp = strTemp & vbNullChar
Else
strTemp = strTemp & strChar
End If
Next
strTemp = strTemp & vbNullChar & vbNullChar
of.lpstrFilter = strTemp
strTemp = m_strFileName & String$(cintMaxFileLength - Len(m_strFileName), 0)
of.lpstrFile = strTemp
of.nMaxFile = cintMaxFileLength
strTemp = m_strFileTitle & String$(cintMaxFileLength - Len(m_strFileTitle), 0)
of.lpstrFileTitle = strTemp
of.lpstrInitialDir = m_strInitialDir
of.nMaxFileTitle = cintMaxFileLength
of.hwndOwner = m_lnghWndParent
If fOpen Then
If GetOpenFileName(of) Then
Show = True
m_strFileName = TrimNulls(of.lpstrFile)
m_strFileTitle = TrimNulls(of.lpstrFileTitle)
Else
Show = False
End If
Else
If GetSaveFileName(of) Then
Show = True
m_strFileName = TrimNulls(of.lpstrFile)
m_strFileTitle = TrimNulls(of.lpstrFileTitle)
Else
Show = False
End If
End If
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
"Show"
Resume PROC_EXIT
End Function
Private Function TrimNulls(ByVal strIn As String) As String
Dim intPos As Integer
On Error GoTo PROC_ERR
intPos = InStr(strIn, vbNullChar)
If intPos = 0 Then
TrimNulls = strIn
Else
If intPos = 1 Then
TrimNulls = ""
Else
TrimNulls = Left$(strIn, intPos - 1)
End If
End If
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
"TrimNulls"
Resume PROC_EXIT
End Function
Conclusion
Code à insérer dans un module pour tester la classe : Sub Test() Dim dlg As CFileDialog
Set dlg = New CFileDialog dlg.DialogTitle = "Choisissez une balance d'importation" dlg.Filter = "Balance d'importation Format Texte|*.txt|Balance d'importation Format Excel|*.xls|Tous les fichiers|*.*" dlg.InitialDir = "C:\" If dlg.Show(False) Then MsgBox "Fichier sélectionné : " & dlg.FileName Else MsgBox "Aucun fichier sélectionné." End If End Sub
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
boite de dialogue enregistrer et ouvrir [ par raph951 ]
Bonjour,je suis entrain de programmer un jeu de dames et je voudrais pouvoir sauvegarder une partie et pouvoir la relancer à l'aide des boites de dial
Boite de dialogue enregistrer et picturebox [ par Monico9385 ]
Bonjour tout le monde!!!J'aurai une petit question:J'ai fais une boite de dialogue ouvrir, et maintenant j'aimerai que l'image sélectionnée dans la bo
Boite de dialogue Ouvrir / Enregistrer Sous (API OPENFILENAME) [ par isa911 ]
Bonjour,Je cherche à utiliser cette API, elle fonctionne correctement, mais comment faire pour mettre un nom de fichier par défaut pour l'en
ouvrir la boite de dialogue "Enregistrer Sous" [ par midoparis ]
Bonjour !!J'ai une macro qui fait un certain traitement sur le fichier Excel. Il peut arriver que l'utilisateur lance la macro sur un nouveau fichier
enregistrer/ouvrir listbox ET alimenter une listbox a partir d'une richtextbox multiligne [ par thekiller123 ]
bonjour a tous! Debutant en VBV.net, j'aurai aimer savoir comment : 1)enregistrer une listbox et la re-ouvrir 2)remplir une listbox a partir d'une r
Boîte de dialogue Internet Explorer [ par Preetamus ]
Bonjour,j'ai créé une macro qui se log sur un site et ensuite va chercher un tableau. Ce tableau je veut l'ouvrir en excel donc je simule un clique su
Enregistrer et ouvrir données . [ par Chauliac ]
Bonjour Etant d'ébutant j'aimerai savoir comment sauvegarder des données de plusieurs textbox et pouvoir les restituer dans leurs textbox d'origine. J
enregistrer/ouvrir plusieurs listbox dans un seul fichier texte [ par maxpruv ]
Bonjour, je suis nouveaux sur le forum et très "novice" en programmation,excusez ma question qui paraitra peut-être trop facile pour certain...Comme
Afficher la fenetre "Ouvrir avec" [ par Stefun ]
Bonjour, j'aimerais afficher la boite de dialogue "Ouvrir avec" (qui permet d'associer un type de fichier à un programme). En fait j'essaye d'ouvrir
openfile [ par nabilac ]
bonjour, 1) svp qlq sait cmmen faire appel a la boite de dialogue de windows "ouvrir avec ..." mon pb sait que j'ai un label qui contient des chemi
|
Derniers Blogs
PRéSENTATION DES API REST DE WINDOWS AZURE : LISTER LES COMPTES DE STORAGEPRéSENTATION DES API REST DE WINDOWS AZURE : LISTER LES COMPTES DE STORAGE par richardc
http://www.c2idotnet.com/articles/presentation-des-api-rest-de-windows-azure-lister-les-comptes-de-storage
Désolé pour "toto", mais c2i existait avant blogs.developpeur.org et c'est mon site "officiel" ;-) ...
Cliquez pour lire la suite de l'article par richardc [HTML5] SLIDES ET DéMOS : AUTOUR DU W3C , NOUVEAUX STANDARDS ET WEB MOBILE (LILLE)[HTML5] SLIDES ET DéMOS : AUTOUR DU W3C , NOUVEAUX STANDARDS ET WEB MOBILE (LILLE) par Gio
Très bonne après-midi passée lors cette conférence avec le W3C, organisée par L' Inria sur les nouveaux standards, ce Mardi 14 Février, on sent vraiment que çà bosse au W3C, et l'avenir est très très prometteur pour le HTML5, notamment ...
Cliquez pour lire la suite de l'article par Gio GESTION D'EXCEPTION AVEC LES TASKSGESTION D'EXCEPTION AVEC LES TASKS par richardc
Nous avons vu dans un précédent article comment utiliser Task pour effectuer des opérations dans un autre thread.
Malheureusement, comme tout le monde n'est pas parfait, il se peut que cette exécution se passe mal et qu'une exception se produise.
La...
Cliquez pour lire la suite de l'article par richardc DéMARRONS AVEC LES TASKSDéMARRONS AVEC LES TASKS par richardc
Que vous le vouliez ou non, le développement multi-tâche est maintenant une obligation pour toute nouvelle application. Il est donc vital d'en comprendre les mécanismes et de s'y mettre le plus tôt possible.
En attendant le .NET Framework 4.5 avec le...
Cliquez pour lire la suite de l'article par richardc
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|