• Earn real money by being active: Hello Guest, earn real money by simply being active on the forum — post quality content, get reactions, and help the community. Once you reach the minimum credit amount, you’ll be able to withdraw your balance directly. Learn how it works.

Visual Basic Formless File Dialog: No OCX/Form/Component needed!

Status
Not open for further replies.

top10

Moderator
User
Joined
Mar 18, 2015
Messages
487
Reputation
0
Reaction score
5,047
Points
243
Credits
0
‎10 Years of Service‎
70%
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.

Performance%20difference%20on%20opendialog%20-%20ocx%20vs%20dll.PNG


[HIDE-THANKS][LENGUAJE=VB]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[/LENGUAJE][/HIDE-THANKS]

Credits Viotto

 
Status
Not open for further replies.
Back
Top