Results 1 to 5 of 5

Thread: [VB6, Vista+] Remember Open/Save state per-dialog instead of per-app (IFileDialog)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,653

    [VB6, Vista+] Remember Open/Save state per-dialog instead of per-app w/ IFileDialog

    This code tip applies to the new Common Item Dialog interfaces, IFileOpenDialog and IFileSaveDialog, that replace the old Common Dialog control and GetOpenFileName/GetSaveFileName API calls in Windows Vista and newer. See this project for an introduction to using these interfaces.

    Users definitely appreciate the common dialogs opening to the last path, and programmers that this is automatically handled by Windows. Previously, this was limited however to a single state memory for the entire app; but among the many new features of the new IFileDialog-based Common Dialogs is the ability to have Windows manage it automatically for individual dialogs, as well as clear the settings without mucking about in the registry.

    The key is IFileDialog's .SetClientGuid method. You can specify a unique GUID for your dialog, and the settings like last path are stored under the GUID, instead of under your app's name.

    This example code is based on accessing the new Common Item Dialogs through my oleexp type library, with the IID module loaded as well.

    First, establish GUIDs for each dialog you want to have its own settings. Two are shown here, but there's no limit to how many an app can have. These must new, unique GUIDs. Visual Studio 6 came with a tool called GUIDGEN, but there's plenty of other GUID generators out there.

    Code:
    Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpszGuid As Long, pGuid As Any) As Long
    Private Const sGUID_Dialog1 = "{A4BF774D-0029-4c8f-9174-B397211B92F5}"
    Private Const sGUID_Dialog2 = "{83E461A9-5341-46f5-8825-EAC176603E94}"
    
    Private Function GUID_Dialog1() As UUID
    Static iid As UUID
     If (iid.Data1 = 0) Then Call CLSIDFromString(StrPtr(sGUID_Dialog1), iid)
     GUID_Dialog1 = iid
    End Function
    Private Function GUID_Dialog2() As UUID
    Static iid As UUID
     If (iid.Data1 = 0) Then Call CLSIDFromString(StrPtr(sGUID_Dialog2), iid)
     GUID_Dialog2 = iid
    End Function
    Then all you have to do it associate the Common Dialog with the GUID. Per MSDN, this should be the very first call after creating the dialog:
    Code:
    Dim pFOD As New FileOpenDialog
    pFOD.SetClientGuid GUID_Dialog1 'whichever GUID you want here
    '[...now set any other options
    And that's all there is to it. If you want to clear the saved state, simply call .ClearClientData after setting the associated GUID (you can also clear it for the app-level state by calling it without having set a GUID).

    Attached is a small project showing this technique in action. It requires oleexp.tlb (version 4.0 or newer is referenced) and addon mIID.bas from the oleexp zip.
    Attached Files Attached Files
    Last edited by fafalone; Mar 17th, 2020 at 05:36 AM. Reason: Attached project updated to reference oleexp.tlb 4.0

  2. #2
    Hyperactive Member
    Join Date
    Feb 2015
    Location
    Colorado USA
    Posts
    261

    Re: [VB6, Vista+] Remember Open/Save state per-dialog instead of per-app (IFileDialog

    In your sample, you have GUIDS for each of two dialog boxes you show that settings can be saved individually. Where did you get the GUIDs that you used? In my own application how do I generate the GUIDs for the dialog boxes that my project may use?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,653

    Re: [VB6, Vista+] Remember Open/Save state per-dialog instead of per-app (IFileDialog

    You generate a new, unique GUID for each dialog (not each run, the GUID identifies which save you want, so you always use the same GUID for e.g. Dialog1, another for Dialog2, etc). Visual Studio 6 comes with a utility to do this, 'GUID Generator', but if you don't have the full VS install you can just google an online one. There's no need to generate one yourself in your apps code, since it won't change.

  4. #4
    Hyperactive Member
    Join Date
    Feb 2015
    Location
    Colorado USA
    Posts
    261

    Re: [VB6, Vista+] Remember Open/Save state per-dialog instead of per-app (IFileDialog

    Thanks for the info. I bought two copies of VB6 but they are both the Professional VB6 and not Visual Studio so I am missing some of the utilities that came with Visual Studio.

    From what you replied, I am interpreting that to mean that the actual GUID values are irrelevant and that what is important is the uniqueness of the GUIDs. Yes? Thanks.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,653

    Re: [VB6, Vista+] Remember Open/Save state per-dialog instead of per-app (IFileDialog

    Correct.

    First search result was https://www.guidgenerator.com/ which will work fine (it's probably better to use a generator like that to guarantee uniqueness instead of just making one up in your head).

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width