Making A File Common Dialog Open in Details or Large Icon View

Summary

Id: 1419.6
Type: Issue Issue
Current Status: Open

Detail

21 Oct 2003 Open Ken Halter

I've been messing around with your CommonDialog Direct samples. There's a fair number of posts in the newsgroups asking how to open a common dialog in "Details" or "Large Icons" view and no samples in sight. Using your project, I managed to have them open in either of these views (small icons don't seem to work). fwiw, using a Timer and FindWindow, I can get this to work on normal Common Dialogs (VB's ocx) as well (in a compiled app). I'm sure it's possible but I didn't try real hard to get them to work on your non-hooked dialogs (without the timer).

There are very few changes involved. If interested, here they are....

The following changes were made to cCommonDialog.

Public Enum ViewStyles
   [_NoViewProcess] = 0
   vsLargeIcons = &H7029
   vsSmallIcons = &H702A 'this doesn't seem to work
   vsList = &H702B
   vsDetails = &H702C
End Enum
Private meLocalViewStyle As ViewStyles
 
'Mods. '=========================================================
Private Sub Class_Initialize()
    m_lFilterIndex = 1
    meLocalViewStyle = [_NoViewProcess] 'set default
End Sub
 
Public Sub DialogClose()
    meLocalViewStyle = [_NoViewProcess] 'set default
    RaiseEvent DialogClose
End Sub
 
Public Function FolderChange(ByVal hDlg As Long) As Long
   If meLocalViewStyle <> [_NoViewProcess] Then
      Call SetView(hDlg, meLocalViewStyle)
   End If
   RaiseEvent FolderChange(hDlg)
End Function
'New property
Public Property Get ViewStyle() As ViewStyles
   ViewStyle = meLocalViewStyle
End Property
 
Public Property Let ViewStyle(ByVal Setting As ViewStyles)
   meLocalViewStyle = Setting
End Property '=========================================================

I added an additional module that consists of.... '=================================================================

Option Explicit
 
Private Declare Function SendMessage _
   Lib "user32.dll" Alias "SendMessageA" _
   (ByVal hwnd As Long, ByVal wMsg As Long _
   , ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName _
   Lib "user32.dll" Alias "GetClassNameA" _
   (ByVal hwnd As Long, ByVal lpClassName As String _
   , ByVal nMaxCount As Long) As Long
Private Declare Function EnumChildWindows _
   Lib "user32.dll" (ByVal hWndParent As Long _
   , ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Private Declare Function GetParent _
   Lib "user32.dll" (ByVal hwnd As Long) As Long
 
Private Const WM_COMMAND As Long = &H111
Private meDesiredView As ViewStyles
 
Public Sub SetView(ByVal DLG As Long _
   , ByVal DesiredView As ViewStyles)
   Dim hwnd As Long
   meDesiredView = DesiredView
   hwnd = GetParent(DLG)
   Call EnumSelected(hwnd)
End Sub
 
Private Sub EnumSelected(hwnd As Long)
   Call EnumChildWindows(hwnd, AddressOf EnumChildProc, &H0) End Sub
 
Public Function EnumChildProc(ByVal hwnd As Long, _
                              ByVal lParam As Long) As Long
 
   Dim lCont As Long
   Dim lRet As Long
   Dim sBuffer As String
 
   lCont = 1
 
   sBuffer = Space$(260)
   lRet = GetClassName(hwnd, sBuffer, Len(sBuffer))
 
   If InStr(1, sBuffer, "SHELLDLL_DefView") > 0 Then
      Call SendMessage(hwnd, WM_COMMAND, meDesiredView, ByVal 0&)
      lCont = 0 'stop looking for windows
   End If
 
   EnumChildProc = lCont
 
End Function '=================================================================

'Sample project mod (a single line of code)

Private Sub cmdHookOpen_Click()
On Error GoTo cmdHookError
 
    With m_cHookDlg
        .DialogTitle = "Choose Text FIle"
        .CancelError = True
        .flags = OFN_FILEMUSTEXIST Or OFN_PATHMUSTEXIST
        .InitDir = "C:\STEVEMAC"
        .Filter = "Internet documents (*.HTM)|*.HTM|Text files (*.TXT)|*.TXT|All Files (*.*)|*.*"
        .FilterIndex = 1
        .HookDialog = True
        .ViewStyle = vsLargeIcons '<<<<========================================
        .ShowOpen
 
        txtFileName = .FileName
        txtFilter = .Filter
        txtContents = GetFileText(.FileName)
 
    End With
 
    Exit Sub
 
cmdHookError:
    If (Err.Number <> 20001) Then
        MsgBox "Error: " & Err.Description
    End If
 
End Sub '=================================================================

I think that's about it. If you have no interest in adding this functionality to your class, I can always post these mods on my site and require people to download your classes first. Either way, I think this will come in handy. I plan on posting the Timer based sample as well in case people want to use VBs common dialog.

Ken Halter - MS-MVP-VB