Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a FolderBrowserDialog from a WPF application

Tags:

wpf

winapi

I'm trying to use the FolderBrowserDialog from my WPF application - nothing fancy. I don't much care that it has the Windows Forms look to it.

However, when I call ShowDialog, I want to pass the owner window which is an IWin32Window. How do I get this from my WPF control?

Actually, does it matter? If I run this code and use the ShowDialog overload with no parameters it works fine. Under what circumstances do I need to pass the owner window?

Thanks,

Craig

like image 359
Craig Shearer Avatar asked Nov 24 '08 19:11

Craig Shearer


People also ask

How do I use FolderBrowserDialog?

Unlike other Windows Forms controls, a FolderBrowserDialog does not have or need visual properties like others. To create a FolderBrowserDialog control at design-time, you simply drag and drop a FolderBrowserDialog control from Toolbox to a Form in Visual Studio.

What is the meaning of WPF?

WPF, stands for Windows Presentation Foundation is a development framework and a sub-system of . NET Framework. WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages.


2 Answers

And here's my final version.

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     } } 

And to actually use it:

var dlg = new FolderBrowserDialog(); System.Windows.Forms.DialogResult result = dlg.ShowDialog(this.GetIWin32Window()); 
like image 200
Craig Shearer Avatar answered Oct 04 '22 20:10

Craig Shearer


If you specify Owner, you will get a Modal dialog over the specified WPF window.

To get WinForms compatible Win32 window create a class implements IWin32Window like this

 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 } 

And use an instance of this class at your WinForms

        IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle; // 'this' means WPF Window         folderBrowserDialog.ShowDialog(new OldWindow(mainWindowPtr)); 
like image 31
Jobi Joy Avatar answered Oct 04 '22 20:10

Jobi Joy