Controls, controls, controls. Everybody makes controls for almost everything. Only one
problem, not everybody has these controls. This article will show you how to avoid adding
extra controls by calling them with API, especially the Common Dialog Control.
Let’s start at the beginning. The people who will most likely see your program are probably going to be VB programmers, or these users will at least have one piece of Microsoft software installed on their system. This means that they will have the Common Dialog Control (COMDLG32.ocx).
Adding this control to your setup program increases the already large file size, and,
this alone could put off potential users. Well, you DON’T need to add the Common Dialog
Control to your project. All you need to do is use some simple API calls to call the different procedures of the Common Dialog Control.
The first, and most used function is the Open File dialog. Just add the following
code to a form’s General Declarations procedure (making sure that you have a command
button on the form) and you should be able to call the open file dialog:
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 Sub Command1_Click() Dim ofn As OPENFILENAME ofn.lStructSize = Len(ofn) ofn.hwndOwner = Form1.hWnd ofn.hInstance = App.hInstance ofn.lpstrFilter = "Text Files_ (*.txt)" + Chr$(0) + "_ *.txt" + Chr$(0) + "_ Rich Text Files (*.rtf)"_ + Chr$(0) + "*.rtf" + Chr$(0) ofn.lpstrFile = Space$(254) ofn.nMaxFile = 255 ofn.lpstrFileTitle = Space$(254) ofn.nMaxFileTitle = 255 ofn.lpstrInitialDir = CurDir ofn.lpstrTitle = "Dialog Title" ofn.flags = 0 Dim a a = GetOpenFileName(ofn) If (a) Then MsgBox "File to Open: "_ + Trim$(ofn.lpstrFile) 'Do the file open stuff here Else MsgBox "Cancel was pressed" End If End Sub
The print dialog is a useful function. The following code will give you access to this functionality:
Private Type PrintDlg lStructSize As Long hwndOwner As Long hDevMode As Long hDevNames As Long hdc As Long flags As Long nFromPage As Integer nToPage As Integer nMinPage As Integer nMaxPage As Integer nCopies As Integer hInstance As Long lCustData As Long lpfnPrintHook As Long lpfnSetupHook As Long lpPrintTemplateName As String lpSetupTemplateName As String hPrintTemplate As Long hSetupTemplate As Long End Type Private Declare Function PrintDlg_ Lib "comdlg32.dll"_ Alias "PrintDlgA" _ (pPrintdlg As PrintDlg) As Long Private Sub Command1_Click() Dim tPrintDlg As PrintDlg tPrintDlg.lStructSize = Len(tPrintDlg) tPrintDlg.hwndOwner = Me.hWnd tPrintDlg.hdc = hdc tPrintDlg.flags = 0 tPrintDlg.nFromPage = 0 tPrintDlg.nToPage = 0 tPrintDlg.nMinPage = 0 tPrintDlg.nMaxPage = 0 tPrintDlg.nCopies = 1 tPrintDlg.hInstance = App.hInstance lpPrintTemplateName = "Print_ Page" Dim a a = PrintDlg(tPrintDlg) If a Then lFromPage = tPrintDlg.nFromPage lToPage = tPrintDlg.nToPage lMin = tPrintDlg.nMinPage lMax = tPrintDlg.nMaxPage lCopies = tPrintDlg.nCopies 'Printing stuff here End If End Sub
And finally, the following code will help you access the save as dialog:
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 Private Sub Command1_Click() Dim ofn As OPENFILENAME ofn.lStructSize = Len(ofn) ofn.hwndOwner = Form1.hWnd ofn.hInstance = App.hInstance ofn.lpstrFilter = "Text Files_ (*.txt)" + Chr$(0) + _ "*.txt" + Chr$(0)_ + "Rich Text Files (*.rtf)_ " + Chr$(0) _ + "*.rtf" + Chr$(0) ofn.lpstrFile = Space$(254) ofn.nMaxFile = 255 ofn.lpstrFileTitle = Space$(254) ofn.nMaxFileTitle = 255 ofn.lpstrInitialDir = CurDir ofn.lpstrTitle = "Dialog_ Title" ofn.flags = 0 Dim a a = GetSaveFileName(ofn) If (a) Then MsgBox "File to Save: "_ + Trim$(ofn.lpstrFile) 'FileSave Stuff Here Else MsgBox "Cancel was pressed" End If End Sub
That’s it! Easy eh. No need for large file sizes because of bulky .ocx
files, just use an API call!