With this simple class you can create a FileDialog without any form needed, also without the need of using comdlg32.OCX and its visual CommonDialog component on a form.It is also more performant then when using the OCX component.The screenshot on the right shows performance difference when opening a FileDialog: left with tradizional OCX CommonDialog control, right direct DLL calls using this code.

Imagen

Option Explicit

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 Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy 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 Dlg As OPENFILENAME
Private bDialogExecuted As Boolean ' will be true if user pressed OK, false if dialog was canceled

' This can be used to set an initial filename before displaying the dialog
Public Property Let strFile(value As String)
CopyMemory ByVal Dlg.lpstrFile, ByVal value, Len(value)
End Property

' This can be used to retrieve full path of the selected file, after dialog display.
Public Property Get strFile() As String
strFile = Dlg.lpstrFile
End Property

' This can be used to get the filename (without path) of the selected file, after dialog display.
Public Property Get FileName() As String
FileName = Dlg.lpstrFileTitle
End Property

Public Property Get Executed() As Boolean
Executed = bDialogExecuted
End Property

' Set dialog title
Public Property Let Title(value As String)
Dlg.lpstrTitle = value
End Property

' Set default file extension
Public Property Let DefaultExt(value As String)
Dlg.lpstrDefExt = value
End Property

Public Property Let Filter(value As String)
Dlg.lpstrFilter = value
End Property

Public Property Let Owner(value As Long)
Dlg.hwndOwner = value
End Property


Public Sub Initialize()
Dlg.lStructSize = Len(Dlg)
Dlg.hInstance = App.hInstance
Dlg.nFilterIndex = 1
Dlg.lpstrFile = String(257, 0)
Dlg.nMaxFile = Len(Dlg.lpstrFile) - 1
Dlg.lpstrFileTitle = Dlg.lpstrFile
Dlg.nMaxFileTitle = Dlg.nMaxFile
Dlg.lpstrInitialDir = vbNullString
Dlg.flags = 0
End Sub

Public Sub ShowOpenDialog()
bDialogExecuted = GetOpenFileName(Dlg)
End Sub

Public Sub ShowSaveDialog()
bDialogExecuted = GetSaveFileName(Dlg)
End Sub
Credits Viotto
Responder

Volver a “Fuentes”