2016-07-06 78 views
-2

我有一個窗體,這個叫做OpenFileDialog。
我想在文件窗格中預先關注某個文件(突出顯示)。
可能嗎?
我有我可以選擇所有文件的代碼,現在我想要選擇1個文件。OpenFileDialog預先選擇一個文件

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 

我敢肯定有可能選擇1個文件,我只需要知道好的WM_COMMAND。
任何幫助將不勝感激。

回答

0

我發現自己是IShellBrowser,IShellView和IShellFolder實現的解決方案。現在可以關閉這個問題。

0

在調用ShowDialog之前設置對話框的FileNameDefaultExt屬性,以便在視頻文件夾中預先選擇MyFile。這將打開一個沒有擴展名或wmv的文件。任何其他擴展應該失敗。

Dim OpenFileDialog1 As New OpenFileDialog 
OpenFileDialog1.Filter = "All files (*.*)|*.*" 
OpenFileDialog1.RestoreDirectory = True 
OpenFileDialog1.FileName = "MyFile" 
OpenFileDialog1.DefaultExt = "wmv" 
OpenFileDialog1.InitialDirectory = My.Settings.VideoDirectory 
OpenFileDialog1.Multiselect = True 
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then 
    My.Settings.VideoDirectory = Path.GetDirectoryName(OpenFileDialog1.FileName) 
End If 
+0

感謝您的努力,但它沒有工作,文件沒有在文件窗格中突出顯示。我想讓文件集中在文件窗格中,以便我可以快速查看上次選擇的文件。 –

+0

你的問題不是很清楚,那麼你應該更新它。你沒有在文件窗格中突出顯示文件。這就像你問的那樣選擇文件。如果你點擊你的按鈕1然後點擊打開它將打開文件,如果它存在。至少它在VB 2013中有效。編輯:我看到你在註釋中加入了高亮請求。沒有讀過這些。編輯你的問題。 – topshot

相關問題