How To Use a Common Dialog File Open Dialog with the Win32

    Article ID: Q154079
    Creation Date: 29-JUL-1996
    Revision Date: 31-JUL-1996

    The information in this article applies to:

    • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 32-bit only, for Windows, version 4.0

    SUMMARY

    This article provides a code sample and demonstrates how to use the File Open dialog box in the Comdlg32.dll. Making use of Comdlg32.OCX is a good reuse of resources under Windows, as Windows always has its dependent file, Comdlg32.dll, loaded. It provides an easy-to-use interface, but can be a performance hit. If you use the OCX control, you have to load the module into memory and also distribute a 90K OCX control to users of your software. To improve performance, you should minimize the use of controls in your applications. Instead, you can use the Win32 API calls directly.

    If you use the API calls, you may lose some of the functionality that comdlg32.ocx offers, such as the Help button. If you require the Help button, you will need to use the comdlg32.ocx control.

    MORE INFORMATION

    Step by Step Example

    1. Start a new project in Visual Basic. Form1 is created by default.
    2. Place a Command Button on Form1.
    3. Add the following code to the Form1 code window:

      Option Explicit

          Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
            "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) 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 Sub Command1_Click()
            Dim OpenFile As OPENFILENAME
            Dim lReturn As Long
            Dim sFilter As String
            OpenFile.lStructSize = Len(OpenFile)
            OpenFile.hwndOwner = Form1.hWnd
            OpenFile.hInstance = App.hInstance
            sFilter = "Batch Files (*.bat)" & Chr(0) & "*.BAT" & Chr(0)
            OpenFile.lpstrFilter = sFilter
            OpenFile.nFilterIndex = 1
            OpenFile.lpstrFile = String(257, 0)
            OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
            OpenFile.lpstrFileTitle = OpenFile.lpstrFile
            OpenFile.nMaxFileTitle = OpenFile.nMaxFile
            OpenFile.lpstrInitialDir = "C:\"
            OpenFile.lpstrTitle = "Use the Comdlg API not the OCX"
            OpenFile.flags = 0
            lReturn = GetOpenFileName(OpenFile)
            If lReturn = 0 Then
               MsgBox "The User pressed the Cancel Button"
            Else
               MsgBox "The user Chose " & Trim(OpenFile.lpstrFile)
            End If
          End Sub
      
    4. Press the F5 key to run the project, and click the Command button. You should see the File Open dialog appear with the options set.

    REFERENCES

    The Win32 SDK documents other calls to the Comdlg32.dll such as ChooseColor, ChooseFont, and GetSaveFileName.

    For more information, please see the following articles in the Microsoft Knowledge Base:

       ARTICLE ID: Q106682
       TITLE     : Default Extension Ignores File Type in VB Common Dialog
    
       ARTICLE ID: Q137194
       TITLE     : GetOpenFileName nMaxFile Interpreted Incorrectly
    

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
©1997 Microsoft Corporation. All rights reserved. Legal Notices.

Additional reference words: 4.00 vb4win vb432
KBCategory: kbusage kbhowto
KBSubcategory: kbnocat