[vb6]Common Dialog Class (Yet Another One)-VBForums
Results 1 to 9 of 9

Thread: [vb6]Common Dialog Class (Yet Another One)

  1. #1

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,967

    [vb6]Common Dialog Class (Yet Another One)

    This class combines the Windows XP/Win2000 Open/Save dialog that uses APIs to generate the dialog with the IFileDialog interface used in Vista and higher. Basically, the class is a unicode-friendly dialog option as a drop-in, self-contained class. Do note that the class has been hard-coded to not run on any O/S less than XP/Win2000.

    Though the class makes heavy use of calling to interfaces not known to VB, it does not use type libraries (TLBs). However, I have made every effort to make it compatible to TLBs you may be using in your project. In other words, objects returned by this class through its events or functions should be 100% compatible with a TLB that defines interfaces that this class is using. Anything less would be an oversight by me and considered a "bug report".

    This class has absolutely no real benefit over existing code you may already be using unless you want more advanced options. Some of those options include:

    - XP/Win2000: class-generated thunks for hooking the dialog. Those thunks result in raised events from the class to its host, i.e., form, usercontrol, other class, etc.

    - Vista and higher
    -- Customize by adding additional controls to the dialog and receive events for those controls
    -- Add a read-only checkbox back to the dialog that populates the common OFN_ReadOnly flag
    -- Interact with the dialog via class-generated thunks that raise events from the class to its host
    -- Use embedded custom configurations. There are currently 7 of those.
    1. Browse for Folders while showing file names too
    2. Navigate into compressed folders (zips) while being able to select the zip itself or one of its contained files or any other file
    3. Show both files and folders and be able to select either folders or files or both
    4. Four "basket mode" settings which allows selecting files/folders across multiple directories. Similar to "Add to my Cart" button.
    -- All custom mode button captions can be assigned by you or default to locale-aware captions (see screenshot below)

    Nearly all of the advanced Vista options are incorporated into this class, but not all. If you find you need anything more that is not offered, modify as needed.

    If you just want a simple Open/Save dialog where the filter is: All Files, the code needed for the dialog is as simple as:
    Code:
        Dim cBrowser As OSDialogEx
        Set cBrowser = New OSDialogEx
        If cBrowser.ShowOpen(Me.hWnd) = True Then
            MsgBox "File Selected: " & cBrowser.FileName
        End If
    Want to add the "Read-Only" checkbox back to the dialog?
    Code:
        Dim cBrowser As OSDialogEx
        Set cBrowser = New OSDialogEx
        cBrowser.Controls_AddReadOnlyOption 100    ' << user-defined Control ID
        If cBrowser.ShowOpen(Me.hWnd) = True Then
            MsgBox "File Selected and Read-Only opted for: " & CBool(cBrowser.Flags And Dlg_ReadOnly)
        End If
    Want a "Browse for Folder" like dialog that also shows files (not doable with newer dialog using standard options)?
    Code:
        Dim cBrowser As OSDialogEx
        Set cBrowser = New OSDialogEx
        cBrowser.Controls_SetCustomMode cm_BrowseFoldersShowFiles
        If cBrowser.ShowOpen(Me.hWnd) = True Then
            MsgBox "Selected Folder: " & cBrowser.FileName
        End If
    The screenshot below highlights locale-aware captions. The only one I haven't been able to find is a locale-aware caption like: All Files. That would be a nice-touch. But since I haven't found it yet in a common DLL, the dialog filter is hard-coded as "All Files" if you do not provide your own filter.
    Name:  Dialog.jpg
Views: 444
Size:  28.4 KB

    The sample project offers examples of several dialog variations. The class itself is heavily commented.
    Code:
    Update History
    21 Jan 18 - initial release
    23 Jan 18 - Minor fixes
            Fixed a couple locale-aware captions being retrieved from common dlls
            Addressed case where Windows can convert "Open" button to unexpected split-button
    Attached Files Attached Files
    Last edited by LaVolpe; Jan 24th, 2018 at 01:19 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  2. #2

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,967

    Tips, Tricks, Notes...

    Notes. For those of you not yet using a real Vista+ dialog, there are several new flags made available. And some of those flags conflict with previous version of the dialog. Here's a couple of those changes. See this MSDN link for a complete list, also commented in the class.

    ForceFileSystem. Display only true files, not virtual files
    AllNonStorageItems. Allows you to navigate into non-system folders and zip files. AllNonStorageItems & ForceFileSystem are mutually exclusive. One or the other.
    PickFolders. Enables the new basic "Browse for Folder" dialog.
    Previous version flag OFN_ENABLEHOOK. Not supported. For pre-Vista users, the ShowOpen & ShowSave functions have an optional parameter to hook the dialog and pass events to you instead.

    All public enumerations in the class have been made unique so that they don't conflict with any similar enumerations you may already have. So, the class isn't truly plug & play with your existing dialog code, but very little effort is required. Typically, you may only need to change your enumeration values from FOS_[xxxx] or OFN_[xxx] values to the class' DLG_[xxx] & DLGex_[xxx] values.
    Tip. DLG_ prefix is for all systems, DLGex_ prefix is for Vista and above.

    Another major change is the heavy usage of IShellItem and IShellItemArray interfaces. Though for basic usage, interacting with those interfaces is not absolutely needed. The class does offer some 'courtesy' functions to query most of those interface methods.

    For those of you still quite comfortable with VB's common dialog or other classes designed to use GetOpenFileName and GetSaveFileName APIs, this class was designed around that structure. But the newer Vista+ dialog offers much more. The class offers many additional properties/methods to address those. All methods and properties in the class, except 3, are valid for the Vista+ dialog. Those 3 are silently ignored. Many of those others are not valid for the older XP/Win2K dialog. There are comments for each method/property. Those not applicable for the older dialog should be clearly stated.

    Tip. Any additional controls/containers you add to the dialog are assigned a user-defined numeric ID. That ID allows you to call the Controls_PropertyGet/Set functions to get/set properties before the dialog is displayed, while displayed, or after it closes. No ID can be duplicated and the IDs can range from 1 to 268435455

    Tip. The FileName property contains the selected item(s) from the dialog. This property can have 3 different formats. First 2 are just like any common dialog.
    1. Only 1 item selected, regardless of mode: Full path/file name of the selection
    2. Multiple files selected (non-basket mode, same folder). Just like the common dialog. Path followed by vbNullChar-delimited list of selected files
    3. Multiple files selected (basket mode). Since selected items can come from any folder, the format is a vbNullChar-delimited list of full path and file names

    Note 1: To quickly determine if multiple selections were made, simply test if the FileName property has a vbNullChar in it.
    Note 2: If you allow non-system folders to be browsed/selected from within (AllNonStorageItems flag), then it is possible you will get a virtual path/file.

    Tip. The ShowOpen/ShowSave functions have an optional parameter to pass a stdole.IUnknown reference. This reference will contain an IShellItem or IShellItemArray (multiple selected items) on return. That reference can be passed to the modern IFileOperation interface which supersedes the SHFileOperation API (Win2K/XP). This class does not expose the IFileOperation interface. A TLB or custom solution is required.

    Tip. This class appears to work just fine in MS Office (at least Access), even with the thunks and subclassing in play. However, you will need to change some vartypes. Access does not recognize CheckBoxConstants used in public event: DialogCheckBoxChanged. Change that to Long or Boolean. It also does not recognize the vbCFFiles constant used in pvGetDisplayName. Change that constant to: 15.

    Tip. Though the dialog can be displayed without a passed owner hWnd, you should avoid this unless absolutely needed. Without that hWnd, the dialog is modal only to the line of code that called it, not the form that called it. That means your form is not 'disabled' while the dialog is displayed. It also means the user can close your form/project while the dialog is running. When they eventually close the dialog, your code continues to run which may lead to reloading your form/project, hidden to the user. In any case, the dialog does not disable the entire project (multiple forms displayed), only the form that called it when you also pass the form's hWnd as the owner hWnd.
    Last edited by LaVolpe; Jan 24th, 2018 at 07:04 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    383

    Re: [vb6]Common Dialog Class (Yet Another One)

    This is excellent until I needed to dump some files to my 'F:\' drive. It wouldn't select the drive root with browseforfolder! Or am I doing something wrong

  4. #4

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,967

    Re: [vb6]Common Dialog Class (Yet Another One)

    Quote Originally Posted by Steve Grant View Post
    This is excellent until I needed to dump some files to my 'F:\' drive. It wouldn't select the drive root with browseforfolder! Or am I doing something wrong
    I'm not sure what you mean by "wouldn't select". Are you saying it wouldn't navigate to F root?

    Can you be more specific and maybe also include the options you used? Maybe my logic in the subclassing for a custom mode you might have been using has a logic flaw?

    Edited: Yep, a logic flaw in the sample project, but not the dialog class.
    The dialog returns the root folder, but the looping I used in the sample failed...
    Code:
    ...
                sPath = cBrowser.FilePath & "\"
                sItems() = Split(Mid$(cBrowser.FileName, Len(sPath) + 1&), vbNullChar)
    sPath length is 3 since root folder selected. So the split is trying to split on Mid$(Path, 4). When I get a chance I'll update the sample project. If you actually look at cBrowser.FilePath or .FileName, you should see your folder selected.

    However, if multi-selecting root folders using "Browse for Folders", the class does have a logic flaw. I didn't take that into consideration and assumed multi-selection (non-basket mode) would all be in the same parent folder, i.e., same root path. Roots don't quite fit that failed logic. I'll need to tweak that & post the update this weekend.
    Last edited by LaVolpe; Feb 9th, 2018 at 04:29 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    383

    Re: [vb6]Common Dialog Class (Yet Another One)

    You are so right. Thank you.

    Code:
            If UBound(sItems) > -1 Then
                For lItem = 0 To UBound(sItems)
                    lstEvents.AddItem "    Selected: " & sPath & sItems(lItem), lItem + 1&
                Next
            Else
                lstEvents.AddItem "    Selected: " & sPath
            End If

  6. #6
    Lively Member
    Join Date
    Jan 2010
    Posts
    104

    Re: [vb6]Common Dialog Class (Yet Another One)

    Is it possible to hide/remove Help button in Vista style dialog?

  7. #7

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,967

    Re: [vb6]Common Dialog Class (Yet Another One)

    If it's an actual window, I would think you can use FindWindow/FindWindowEx to locate it an hide it, but don't know if you can remove it resulting in the other buttons shifting to fill the empty space. That is not an option that is made available by the IFileDialog or its related interfaces. At least not that I know of. Clicking on that help button does not raise an event in the IFileDialog.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    Lively Member
    Join Date
    Jan 2010
    Posts
    104

    Re: [vb6]Common Dialog Class (Yet Another One)

    Yes, I can find that button, it is called "&Help". Given that IModalWindow::Show is a blocking call, it would be complicated to use ShowWindow API to hide it. I was hoping to use IFileDialogCustomize interface to remove it.

  9. #9

    Thread Starter
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,967

    Re: [vb6]Common Dialog Class (Yet Another One)

    Quote Originally Posted by riov View Post
    Yes, I can find that button, it is called "&Help". Given that IModalWindow::Show is a blocking call, it would be complicated to use ShowWindow API to hide it. I was hoping to use IFileDialogCustomize interface to remove it.
    If on Vista and higher and using this class, you can opt for events. One of the events is the DialogOnInit event. That event contains the dialog hWnd. Within any event, you can call the class IFileDialog_GetHwnd function. Events are not blocked
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width