Sign in to follow this  
Followers 0
Viotto

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

0 posts in this topic

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 below shows performance difference when opening a FileDialog, left with OCX, right direct DLL calls.

Performance difference on opendialog - ocx vs dll.PNG

You can find this and more coding work of mine on my website, breaking-security.net
' cFileDialog.cls' by Viotto - www.BreakingSecurity.net'' Provides CommonDialog functionality,' without the need of ocx file and graphical component.'' ComDlg32.OCX provides an easy-to-use interface, but if you use the OCX control,' you have to load the module into memory and also distribute a 90K OCX file to users of your software.' To improve performance, and eliminate the need of additional dependencies,' you should minimize the use of controls in your applications.' Instead, you can use the Win32 API calls directly.'' More info from Microsoft: https://support.micr...en-us/kb/161286  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 StringEnd Type Private Dlg As OPENFILENAMEPrivate 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 dialogPublic 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.lpstrFileEnd 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.lpstrFileTitleEnd Property Public Property Get Executed() As Boolean  Executed = bDialogExecutedEnd Property ' Set dialog titlePublic Property Let Title(value As String)  Dlg.lpstrTitle = valueEnd Property ' Set default file extensionPublic Property Let DefaultExt(value As String)  Dlg.lpstrDefExt = valueEnd Property Public Property Let Filter(value As String)  Dlg.lpstrFilter = valueEnd Property Public Property Let Owner(value As Long)  Dlg.hwndOwner = valueEnd 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 = 0End Sub Public Sub ShowOpenDialog()  bDialogExecuted = GetOpenFileName(Dlg)End Sub Public Sub ShowSaveDialog()  bDialogExecuted = GetSaveFileName(Dlg)End Sub

57f042500a27f_Performancedifferenceonopendialog-ocxvsdll.PNG.cf72914e8ddc266a5add46360a6b3813.PNG

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0