【问题标题】:OpenFileDialog pre select a fileOpenFileDialog 预选文件
【发布时间】:2016-07-06 01:08:06
【问题描述】:

我有一个调用 OpenFileDialog 的表单。
我希望在文件窗格中预先聚焦(突出显示)某个文件。
有可能吗?
我有可以选择所有文件的代码,现在我想要选择 1 个文件。

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。
任何帮助将不胜感激。

【问题讨论】:

    标签: vb.net openfiledialog


    【解决方案1】:

    在调用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
    

    【讨论】:

    • 感谢您的努力,但它不起作用,文件未在文件窗格中突出显示。我希望文件集中在文件窗格中,以便我可以快速查看上次选择的文件。
    • 你的问题不是很清楚,所以你应该更新它。您没有提到在文件窗格中突出显示文件。就像你问的那样,这会选择文件。如果您单击按钮 1,然后单击打开,它将打开该文件(如果存在)。至少在 VB 2013 中是这样。编辑:我看到你把突出显示的请求放在评论中。没读过那些。编辑您的问题。
    【解决方案2】:

    我为自己找到了实现 IShellBrowser、IShellView 和 IShellFolder 的解决方案。现在可以关闭问题了。

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      相关资源
      最近更新 更多