原贴地址:http://bbs.cskin.net/thread-1849-1-1.html
说明:我们选择文件夹就要使用net自带的folderBrowserDialog,但是这个dialog看上去并不是非常好看
因此使用IFileOpenDialog接口重写了一个 folderBrowserDialog,可以实现类似OpenFileDialog的样式
效果图:
是不是觉得你的程序一下子大气多了呢?
废话不多说,直接上代码:
usingSystem;
usingSystem.ComponentModel;
usingSystem.Drawing.Design;
usingSystem.Runtime.InteropServices;
usingSystem.Windows.Forms;
namespaceFileBorserDialog
{
#region Editor
/// <summary>
/// FolderBrowser 的设计器基类
/// </summary>
publicclass FolderNameEditor : UITypeEditor
{
publicoverride UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
returnUITypeEditorEditStyle.Modal;
}
publicoverride object EditValue(ITypeDescriptorContext context, IServiceProvider provider, objectvalue)
{
FolderBrowserDialog browser = newFolderBrowserDialog();
if(value != null)
{
browser.DirectoryPath = string.Format("{0}", value);
}
if(browser.ShowDialog(null) == DialogResult.OK)
returnbrowser.DirectoryPath;
returnvalue;
}
}
#endregion
#region FolderBrowserDialog Base
/// <summary>
/// Vista 样式的选择文件对话框的基类
/// </summary>
[Description("提供一个Vista样式的选择文件对话框")]
[Editor(typeof(FolderNameEditor),typeof(UITypeEditor))]
publicclass FolderBrowserDialog : Component
{
/// <summary>
/// 初始化 FolderBrowser 的新实例
/// </summary>
publicFolderBrowserDialog()
{
}
#region Public Property
/// <summary>
/// 获取在 FolderBrowser 中选择的文件夹路径
/// </summary>
publicstring DirectoryPath { get;set; }
/// <summary>
/// 向用户显示 FolderBrowser 的对话框
/// </summary>
/// <param name="owner">任何实现 System.Windows.Forms.IWin32Window(表示将拥有模式对话框的顶级窗口)的对象。</param>
/// <returns></returns>
publicDialogResult ShowDialog(IWin32Window owner)
{
IntPtr hwndOwner = owner != null? owner.Handle : GetActiveWindow();
IFileOpenDialog dialog = (IFileOpenDialog)newFileOpenDialog();
try
{
IShellItem item;
if(!string.IsNullOrEmpty(DirectoryPath))
{
IntPtr idl;
uintatts = 0;
if(SHILCreateFromPath(DirectoryPath, outidl, refatts) == 0)
{
if(SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, outitem) == 0)
{
dialog.SetFolder(item);
}
}
}
dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM);
uinthr = dialog.Show(hwndOwner);
if(hr == ERROR_CANCELLED)
returnDialogResult.Cancel;
if(hr != 0)
returnDialogResult.Abort;
dialog.GetResult(outitem);
stringpath;
item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH,outpath);
DirectoryPath = path;
returnDialogResult.OK;
}
finally
{
Marshal.ReleaseComObject(dialog);
}
}
#endregion
#region BaseType
[DllImport("shell32.dll")]
privatestatic extern int SHILCreateFromPath([MarshalAs(UnmanagedType.LPWStr)] stringpszPath, outIntPtr ppIdl, refuint rgflnOut);
[DllImport("shell32.dll")]
privatestatic extern int SHCreateShellItem(IntPtr pidlParent, IntPtr psfParent, IntPtr pidl, outIShellItem ppsi);
[DllImport("user32.dll")]
privatestatic extern IntPtr GetActiveWindow();
privateconst uint ERROR_CANCELLED = 0x800704C7;
[ComImport]
[Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")]
privateclass FileOpenDialog
{
}
[ComImport]
[Guid("42f85136-db7e-439c-85f1-e4075d135fc8")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
privateinterface IFileOpenDialog
{
[PreserveSig]
uintShow([In] IntPtr parent); // IModalWindow
voidSetFileTypes(); // not fully defined
voidSetFileTypeIndex([In] uintiFileType);
voidGetFileTypeIndex(outuint piFileType);
voidAdvise(); // not fully defined
voidUnadvise();
voidSetOptions([In] FOS fos);
voidGetOptions(outFOS pfos);
voidSetDefaultFolder(IShellItem psi);
voidSetFolder(IShellItem psi);
voidGetFolder(outIShellItem ppsi);
voidGetCurrentSelection(outIShellItem ppsi);
voidSetFileName([In, MarshalAs(UnmanagedType.LPWStr)] stringpszName);
voidGetFileName([MarshalAs(UnmanagedType.LPWStr)] outstring pszName);
voidSetTitle([In, MarshalAs(UnmanagedType.LPWStr)] stringpszTitle);
voidSetOkButtonLabel([In, MarshalAs(UnmanagedType.LPWStr)] stringpszText);
voidSetFileNameLabel([In, MarshalAs(UnmanagedType.LPWStr)] stringpszLabel);
voidGetResult(outIShellItem ppsi);
voidAddPlace(IShellItem psi, intalignment);
voidSetDefaultExtension([In, MarshalAs(UnmanagedType.LPWStr)] stringpszDefaultExtension);
voidClose(inthr);
voidSetClientGuid(); // not fully defined
voidClearClientData();
voidSetFilter([MarshalAs(UnmanagedType.Interface)] IntPtr pFilter);
voidGetResults([MarshalAs(UnmanagedType.Interface)] outIntPtr ppenum); // not fully defined
voidGetSelectedItems([MarshalAs(UnmanagedType.Interface)] outIntPtr ppsai); // not fully defined
}
[ComImport]
[Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
privateinterface IShellItem
{
voidBindToHandler(); // not fully defined
voidGetParent(); // not fully defined
voidGetDisplayName([In] SIGDN sigdnName, [MarshalAs(UnmanagedType.LPWStr)] outstring ppszName);
voidGetAttributes(); // not fully defined
voidCompare(); // not fully defined
}
privateenum SIGDN : uint
{
SIGDN_DESKTOPABSOLUTEEDITING = 0x8004c000,
SIGDN_DESKTOPABSOLUTEPARSING = 0x80028000,
SIGDN_FILESYSPATH = 0x80058000,
SIGDN_NORMALDISPLAY = 0,
SIGDN_PARENTRELATIVE = 0x80080001,
SIGDN_PARENTRELATIVEEDITING = 0x80031001,
SIGDN_PARENTRELATIVEFORADDRESSBAR = 0x8007c001,
SIGDN_PARENTRELATIVEPARSING = 0x80018001,
SIGDN_URL = 0x80068000
}
[Flags]
privateenum FOS
{
FOS_ALLNONSTORAGEITEMS = 0x80,
FOS_ALLOWMULTISELECT = 0x200,
FOS_CREATEPROMPT = 0x2000,
FOS_DEFAULTNOMINIMODE = 0x20000000,
FOS_DONTADDTORECENT = 0x2000000,
FOS_FILEMUSTEXIST = 0x1000,
FOS_FORCEFILESYSTEM = 0x40,
FOS_FORCESHOWHIDDEN = 0x10000000,
FOS_HIDEMRUPLACES = 0x20000,
FOS_HIDEPINNEDPLACES = 0x40000,
FOS_NOCHANGEDIR = 8,
FOS_NODEREFERENCELINKS = 0x100000,
FOS_NOREADONLYRETURN = 0x8000,
FOS_NOTESTFILECREATE = 0x10000,
FOS_NOVALIDATE = 0x100,
FOS_OVERWRITEPROMPT = 2,
FOS_PATHMUSTEXIST = 0x800,
FOS_PICKFOLDERS = 0x20,
FOS_SHAREAWARE = 0x4000,
FOS_STRICTFILETYPES = 4
}
#endregion
}
#endregion
}
(需要添加System.Drawing.Design引用)
调用方法和原版 folderBrowserDialog一样
[C#]
纯文本查看
复制代码
1
2
3
4
|
if
(folderBrowserDialog1.ShowDialog(
this
) == DialogResult.OK)
{
MessageBox.Show(
"选择了:"
+ folderBrowserDialog1.DirectoryPath);
}
|