Search code examples
vb.netopenfiledialog

OpenFileDialog pre select a file


I have a form wich call OpenFileDialog.
I want a certain file to be focused in advance (Highlighted) in the files pane.
Is it possible?
I have code where i can select all files, now i want 1 file to be selected.

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim OpenFileDialog1 As New OpenFileDialog
        OpenFileDialog1.Filter = "All files (*.*)|*.*"
        OpenFileDialog1.RestoreDirectory = True
        OpenFileDialog1.FileName = "C:\MyFile.wmv"
        OpenFileDialog1.InitialDirectory = My.Settings.VideoDirectory
        OpenFileDialog1.Multiselect = True
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            My.Settings.VideoDirectory = Path.GetDirectoryName(OpenFileDialog1.FileName)
        End If

    End Sub

    Dim m_lastDialogHandle As IntPtr

    Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    Public Declare Function FindWindowExW Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal hWndChildAfter As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszClass As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszWindow As String) As IntPtr

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = 289 Then  ' Notify of message loop
            Dim dialogHandle As IntPtr = m.LParam
            If (dialogHandle <> m_lastDialogHandle) Then
                Dim hChild1 As IntPtr = 0
                Dim hChild2 As IntPtr = 0
                Dim hChild3 As IntPtr = 0
                m_lastDialogHandle = dialogHandle
                hChild1 = FindWindowExW(dialogHandle, 0, "DUIViewWndClassName", Nothing)
                If hChild1 = 0 Then Exit Sub
                hChild1 = FindWindowExW(hChild1, 0, "DirectUIHWND", Nothing)
                If hChild1 = 0 Then Exit Sub
                Do
                    hChild2 = FindWindowExW(hChild1, hChild2, Nothing, Nothing)
                    If hChild2 = 0 Then Exit Sub
                    hChild3 = FindWindowExW(hChild2, 0, "SHELLDLL_DefView", "ShellView")
                Loop Until hChild3 <> 0
                SendMessage(hChild3, &H111, &H17021, 0)
            End If
        End If
    End Sub

End Class

I'm sure it is possible to select 1 file, i just have to know the good WM_COMMAND.
Any help will be appreciated.


Solution

  • I found myself the solution with implementation of IShellBrowser, IShellView and IShellFolder. The question can be closed now.