Code: Select all
'var for exposed property
Private m_lvInitialView As Long
'windows version constants
Private Const VER_PLATFORM_WIN32_NT As Long = 2
Private Const OSV_LENGTH As Long = 76
Private Const OSVEX_LENGTH As Long = 88
Public OSV_VERSION_LENGTH As Long 'our const to hold appropriate OSV length
Private Type OSVERSIONINFO
  OSVSize         As Long
  dwVerMajor      As Long
  dwVerMinor      As Long
  dwBuildNumber   As Long
  PlatformID      As Long
  szCSDVersion    As String * 128
End Type
'windows messages & notifications etc
Private Const WM_COMMAND = &H111
Public Const WM_NOTIFY As Long = &H4E&
Public Const WM_INITDIALOG As Long = &H110
Public Const CDN_FIRST As Long = -601
Public Const CDN_INITDONE As Long = (CDN_FIRST - &H0&)
Public Const MAX_PATH As Long = 260
'openfilename constants
Public Const OFN_ENABLEHOOK As Long = &H20
Public Const OFN_EXPLORER As Long = &H80000
Public Const OFN_ENABLESIZING As Long = &H800000
Public Const OFN_EX_NOPLACESBAR As Long = &H1
'constants for listview view state
'provided by Brad Martinez
Public Const SHVIEW_ICON As Long = &H7029
Public Const SHVIEW_LIST As Long = &H702B
Public Const SHVIEW_REPORT As Long = &H702C
Public Const SHVIEW_THUMBNAIL As Long = &H702D
Public Const SHVIEW_TILE As Long = &H702E
'this is the version 5+ definition of
'the OPENFILENAME structure containing
'three additional members providing
'additional options on Windows 2000
'or later. The SetOSVersion routine
'will assign either OSV_LENGTH (76)
'or OSVEX_LENGTH (88) to the OSV_VERSION_LENGTH
'variable declared above. This variable, rather
'than Len(OFN) is used to assign the required
'value to the OPENFILENAME structure's nStructSize
'member which tells the OS if extended features
'- primarily the Places Bar - are supported.
Public Type OPENFILENAME
  nStructSize       As Long
  hwndOwner         As Long
  hInstance         As Long
  sFilter           As String
  sCustomFilter     As String
  nMaxCustFilter    As Long
  nFilterIndex      As Long
  sFile             As String
  nMaxFile          As Long
  sFileTitle        As String
  nMaxTitle         As Long
  sInitialDir       As String
  sDialogTitle      As String
  flags             As Long
  nFileOffset       As Integer
  nFileExtension    As Integer
  sDefFileExt       As String
  nCustData         As Long
  fnHook            As Long
  sTemplateName     As String
  pvReserved        As Long
  dwReserved        As Long
  flagsEx           As Long
End Type
Public OFN As OPENFILENAME
'defined As Any to support either the
'OSVERSIONINFO or OSVERSIONINFOEX structure
Private Declare Function GetVersionEx Lib "kernel32" _
   Alias "GetVersionExA" _
  (lpVersionInformation As Any) As Long
 
Public Declare Function GetOpenFileName Lib "comdlg32.dll" _
   Alias "GetOpenFileNameA" _
  (pOpenfilename As OPENFILENAME) As Long
Private Declare Function FindWindowEx Lib "user32" _
   Alias "FindWindowExA" _
  (ByVal hWndParent As Long, _
   ByVal hWndChildAfter As Long, _
   ByVal lpClassName As String, _
   ByVal lpWindowName As String) As Long
Private Declare Function GetParent Lib "user32" _
  (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
   (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long
Public Function FARPROC(pfn As Long) As Long
 
  'A dummy procedure that receives and returns
  'the return value of the AddressOf operator.
 
  'Obtain and set the address of the callback
  'This workaround is needed as you can't assign
  'AddressOf directly to a member of a user-
  'defined type, but you can assign it to another
  'long and use that (as returned here)
  FARPROC = pfn
End Function
Public Function IsWin2000Plus() As Boolean
  'returns True if running Windows 2000 or later
   Dim osv As OSVERSIONINFO
   osv.OSVSize = Len(osv)
   If GetVersionEx(osv) = 1 Then
   
      IsWin2000Plus = (osv.PlatformID = VER_PLATFORM_WIN32_NT) And _
                      (osv.dwVerMajor = 5 And osv.dwVerMinor >= 0)
 
   End If
End Function
Public Property Let OFN_SetInitialView(ByVal initview As Long)
   m_lvInitialView = initview
   
End Property
Public Function OFNHookProc(ByVal hwnd As Long, _
                            ByVal uMsg As Long, _
                            ByVal wParam As Long, _
                            ByVal lParam As Long) As Long
   Dim hWndParent As Long
   Dim hwndLv As Long
   Static bLvSetupDone As Boolean
   
   Select Case uMsg
      Case WM_INITDIALOG
        'Initdialog is set when the dialog
        'has been created and is ready to
        'be displayed, so set our flag
        'to prevent re-executing the code
        'in the wm_notify message. This is
        'required as the dialog receives a
        'number of WM_NOTIFY messages throughout
        'the life of the dialog. If this is not
        'done, and the user chooses a different
        'view, on the next WM_NOTIFY message
        'the listview would be reset to the
        'initial view, probably ticking off
        'the user. The variable is declared
        'static to preserve values between
        'calls; it will be automatically reset
        'on subsequent showing of the dialog.
         bLvSetupDone = False
         
        'other WM_INITDIALOG code here, such
        'as caption or button changing, or
        'centering the dialog.
     
      Case WM_NOTIFY
               
            If bLvSetupDone = False Then
               
             'hwnd is the handle to the dialog
             'hwndParent is the handle to the common control
             'hwndLv is the handle to the listview itself
               hWndParent = GetParent(hwnd)
               hwndLv = FindWindowEx(hWndParent, 0, "SHELLDLL_DefView", vbNullChar)
               
               If hwndLv > 0 Then
                  Call SendMessage(hwndLv, WM_COMMAND, ByVal m_lvInitialView, ByVal 0&)
                 
                 'since we found the lv hwnd, assume the
                 'command was received and set the flag
                 'to prevent recalling this routine
                  bLvSetupDone = True
               End If  'hwndLv
            End If  'bLvSetupDone
         Case Else
         
   End Select
End Function
Public Sub SetOSVersion()
 
   Select Case IsWin2000Plus()
      Case True
         OSV_VERSION_LENGTH = OSVEX_LENGTH '5.0+ structure size
     
      Case Else
         OSV_VERSION_LENGTH = OSV_LENGTH 'pre-5.0 structure size
   End Select
End Sub


