如何使用WPF应用程序中的FolderBrowserDialog

时间:2008-11-24 19:26:52

标签: wpf winapi

我正在尝试使用我的WPF应用程序中的FolderBrowserDialog - 没什么特别的。我不太关心它有Windows窗体的外观。

但是,当我调用ShowDialog时,我想传递一个IWin32Window的所有者窗口。如何从我的WPF控件中获取此信息?

实际上,这有关系吗?如果我运行此代码并使用没有参数的ShowDialog重载,它可以正常工作。在什么情况下我需要通过所有者窗口?

谢谢,

克雷格

10 个答案:

答案 0 :(得分:57)

这是我的最终版本。

public static class MyWpfExtensions
{
    public static System.Windows.Forms.IWin32Window GetIWin32Window(this System.Windows.Media.Visual visual)
    {
        var source = System.Windows.PresentationSource.FromVisual(visual) as System.Windows.Interop.HwndSource;
        System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);
        return win;
    }

    private class OldWindow : System.Windows.Forms.IWin32Window
    {
        private readonly System.IntPtr _handle;
        public OldWindow(System.IntPtr handle)
        {
            _handle = handle;
        }

        #region IWin32Window Members
        System.IntPtr System.Windows.Forms.IWin32Window.Handle
        {
            get { return _handle; }
        }
        #endregion
    }
}

实际使用它:

var dlg = new FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dlg.ShowDialog(this.GetIWin32Window());

答案 1 :(得分:16)

如果指定了Owner,您将在指定的WPF窗口上获得一个Modal对话框。

要获得WinForms兼容的Win32窗口,创建一个类实现IWin32Window就像这样

 public class OldWindow : System.Windows.Forms.IWin32Window
{
    IntPtr _handle;

    public OldWindow(IntPtr handle)
    {
        _handle = handle;
    }

    #region IWin32Window Members

    IntPtr System.Windows.Forms.IWin32Window.Handle
    {
        get { return _handle; }
    }

    #endregion
}

在WinForms中使用此类的实例

        IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle; // 'this' means WPF Window
        folderBrowserDialog.ShowDialog(new OldWindow(mainWindowPtr));

答案 2 :(得分:3)

我意识到这是一个老问题,但这里的方法可能稍微优雅一些​​(以前可能会或可能没有)......

using System;
using System.Windows;
using System.Windows.Forms;

// ...

/// <summary>
///     Utilities for easier integration with WinForms.
/// </summary>
public static class WinFormsCompatibility {

    /// <summary>
    ///     Gets a handle of the given <paramref name="window"/> and wraps it into <see cref="IWin32Window"/>,
    ///     so it can be consumed by WinForms code, such as <see cref="FolderBrowserDialog"/>.
    /// </summary>
    /// <param name="window">
    ///     The WPF window whose handle to get.
    /// </param>
    /// <returns>
    ///     The handle of <paramref name="window"/> is returned as <see cref="IWin32Window.Handle"/>.
    /// </returns>
    public static IWin32Window GetIWin32Window(this Window window) {
        return new Win32Window(new System.Windows.Interop.WindowInteropHelper(window).Handle);
    }

    /// <summary>
    ///     Implementation detail of <see cref="GetIWin32Window"/>.
    /// </summary>
    class Win32Window : IWin32Window { // NOTE: This is System.Windows.Forms.IWin32Window, not System.Windows.Interop.IWin32Window!

        public Win32Window(IntPtr handle) {
            Handle = handle; // C# 6 "read-only" automatic property.
        }

        public IntPtr Handle { get; }

    }

}

然后,从您的WPF窗口,您可以简单地...

public partial class MainWindow : Window {

    void Button_Click(object sender, RoutedEventArgs e) {
        using (var dialog = new FolderBrowserDialog()) {
            if (dialog.ShowDialog(this.GetIWin32Window()) == System.Windows.Forms.DialogResult.OK) {
                // Use dialog.SelectedPath.
            }
        }
    }

}
  

实际上,这有关系吗?

我不确定在这种情况下是否重要,但一般来说,你应该告诉Windows你的窗口层次结构是什么,所以如果在子窗口是模态的情况下单击父窗口,Windows可以为用户提供视觉(并且可能听得见)的线索。

此外,当有多个模态窗口时,它确保“正确”窗口位于顶部(不是我提倡这样的UI设计)。我已经看到由一个价值数十亿美元的公司设计的用户界面(这个外壳仍未命名),只是因为一个模态对话框被“卡住”在另一个下方而被绞死,并且用户不知道它是否在那里,更不用说如何关闭它

答案 3 :(得分:2)

好的,现在想出来 - 感谢Jobi,他的答案很接近,但并不完全。

从WPF应用程序,这是我的代码:

首先是助手类:

private class OldWindow : System.Windows.Forms.IWin32Window
{    
    IntPtr _handle;    
    public OldWindow(IntPtr handle)
    {
        _handle = handle;
    }   

    #region IWin32Window Members    
    IntPtr System.Windows.Forms.IWin32Window.Handle
    {
        get { return _handle; }
    }    
    #endregion
}

然后,使用它:

    System.Windows.Forms.FolderBrowserDialog dlg = new FolderBrowserDialog();
    HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
    System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);
    System.Windows.Forms.DialogResult result = dlg.ShowDialog(win);

我确信我可以更好地包装它,但基本上它可以工作。好极了! : - )

答案 4 :(得分:2)

//add a reference to System.Windows.Forms.dll

public partial class MainWindow : Window, System.Windows.Forms.IWin32Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        var fbd = new FolderBrowserDialog();
        fbd.ShowDialog(this);
    }

    IntPtr System.Windows.Forms.IWin32Window.Handle
    {
        get
        {
            return ((HwndSource)PresentationSource.FromVisual(this)).Handle;
        }
    }
}

答案 5 :(得分:1)

VB.net翻译

Module MyWpfExtensions

Public Function GetIWin32Window(this As Object, visual As System.Windows.Media.Visual) As System.Windows.Forms.IWin32Window

    Dim source As System.Windows.Interop.HwndSource = System.Windows.PresentationSource.FromVisual(Visual)
    Dim win As System.Windows.Forms.IWin32Window = New OldWindow(source.Handle)
    Return win
End Function

Private Class OldWindow
    Implements System.Windows.Forms.IWin32Window

    Public Sub New(handle As System.IntPtr)
        _handle = handle
    End Sub


    Dim _handle As System.IntPtr
    Public ReadOnly Property Handle As IntPtr Implements Forms.IWin32Window.Handle
        Get

        End Get
    End Property


End Class

End Module

答案 6 :(得分:0)

传递所有者句柄的优点是FolderBrowserDialog不会是该窗口的模态。这可以防止用户在对话框处于活动状态时与主应用程序窗口进行交互。

答案 7 :(得分:0)

你应该能够通过使用PresentationSource.FromVisual获取IWin32Window并将结果转换为实现IWin32Window的HwndSource。

同样在评论here中:

答案 8 :(得分:0)

为什么不使用内置的WindowInteropHelper类(请参阅命名空间System.Windows.Interop)。这个类已经强制IWin32Window;)

所以你可以忘记“OldWindow类”......用法保持不变

答案 9 :(得分:0)

这是一种简单的方法。


System.Windows.Forms.NativeWindow winForm; 
public MainWindow()
{
    winForm = new System.Windows.Forms.NativeWindow();
    winForm.AssignHandle(new WindowInteropHelper(this).Handle);
    ...
}
public showDialog()
{
   dlgFolderBrowser.ShowDialog(winForm);
}