在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → winform 文件夹浏览器示例源码

winform 文件夹浏览器示例源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.01M
  • 下载次数:39
  • 浏览次数:759
  • 发布时间:2013-10-03
  • 实例类别:C#语言基础
  • 发 布 人:crazycode
  • 文件格式:.zip
  • 所需积分:2
 相关标签: winform 浏览器 文件

实例介绍

【实例简介】

【实例截图】

【核心代码】

using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CP.Windows.Forms
{
	/// <summary>
	/// Flags that control display and behaviour of folder browse dialog
	/// </summary>
	[Flags]
	public enum BrowseFlags : int
	{
		/// <summary>
		/// Same as BIF_RETURNONLYFSDIRS 
		/// </summary>
		ReturnOnlyFSDirs   = 0x0001,
		/// <summary>
		/// Same as BIF_DONTGOBELOWDOMAIN 
		/// </summary>
		DontGoBelowDomain  = 0x0002,
		/// <summary>
		/// Same as BIF_STATUSTEXT 
		/// </summary>
		ShowStatusText     = 0x0004,
		/// <summary>
		/// Same as BIF_RETURNFSANCESTORS 
		/// </summary>
		ReturnFSancestors  = 0x0008,
		/// <summary>
		/// Same as BIF_EDITBOX 
		/// </summary>
		EditBox            = 0x0010,
		/// <summary>
		/// Same as BIF_VALIDATE 
		/// </summary>
		Validate           = 0x0020,
		/// <summary>
		/// Same as BIF_NEWDIALOGSTYLE
		/// </summary>
		NewDialogStyle     = 0x0040,
		/// <summary>
		/// Same as BIF_BROWSEINCLUDEURLS 
		/// </summary>
		BrowseIncludeURLs  = 0x0080,
		/// <summary>
		/// Same as BIF_UAHINT
		/// </summary>
		AddUsageHint       = 0x0100,
		/// <summary>
		/// Same as BIF_NONEWFOLDERBUTTON 
		/// </summary>
		NoNewFolderButton  = 0x0200,
		/// <summary>
		/// Same as BIF_BROWSEFORCOMPUTER
		/// </summary>
		BrowseForComputer  = 0x1000,
		/// <summary>
		/// Same as BIF_BROWSEFORPRINTER 
		/// </summary>
		BrowseForPrinter   = 0x2000,
		/// <summary>
		/// Same as BIF_BROWSEINCLUDEFILES 
		/// </summary>
		IncludeFiles	   = 0x4000,
		/// <summary>
		/// Same as BIF_SHAREABLE 
		/// </summary>
		ShowShareable	   = 0x8000,
	}
	
	#region Delegate and Event Arg Decalarations
	
	/// <summary>
	/// Provides data for folder selection changed event
	/// </summary>
	public class FolderSelChangedEventArgs : EventArgs, IDisposable
	{
		private IntPtr pidlNewSelect;

		internal FolderSelChangedEventArgs(IntPtr pidlNewSelect)
		{
			this.pidlNewSelect = pidlNewSelect;
		}
		
		/// <summary>
		/// Return ITEMIDLIST for the currently selected folder
		/// </summary>
		public IntPtr CurSelFolderPidl
		{
			get
			{
				return pidlNewSelect;
			}
		}

		/// <summary>
		/// Gets the path of the folder which is currently selected
		/// </summary>
		public string CurSelFolderPath
		{
			get
			{
				StringBuilder path = new StringBuilder(260);
				UnManagedMethods.SHGetPathFromIDList(pidlNewSelect, path);
				
				return path.ToString();
			}
		}

		public void Dispose()
		{
			UnManagedMethods.SHMemFree(pidlNewSelect);
		}
	};

	public delegate void FolderSelChangedEventHandler(object sender, FolderSelChangedEventArgs e);
	
	/// <summary>
	/// Provides data for the IUnknownObtainedEvent.
	/// </summary>
	public class IUnknownObtainedEventArgs : EventArgs
	{
		private object siteUnknown;

		internal IUnknownObtainedEventArgs(object siteUnknown)
		{
			this.siteUnknown = siteUnknown;
		}
		
		/// <summary>
		/// Object that corrensponds to the IUnknown obtained
		/// </summary>
		public object SiteUnknown
		{
			get
			{
				return siteUnknown;
			}
		}
	}
	
	public delegate void IUnknownObtainedEventHandler(object sender, IUnknownObtainedEventArgs args);
	
	/// <summary>
	/// Provides data for validation failed event.
	/// </summary>
	public class ValidateFailedEventArgs
	{
		private string invalidText;
		private bool dismissDialog = false;

		internal ValidateFailedEventArgs(string invalidText)
		{
			this.invalidText = invalidText;
		}
		
		/// <summary>
		/// The text which called validation to fail
		/// </summary>
		public string InvalidText
		{
			get
			{
				return invalidText;
			}
		}
		
		/// <summary>
		/// Sets whether the dialog needs to be dismissed or not
		/// </summary>
		public bool DismissDialog
		{
			get
			{
				return dismissDialog;
			}
			set
			{
				dismissDialog = value;
			}
		}
	}
	
	public delegate void ValidateFailedEventHandler(object sender, ValidateFailedEventArgs args);
	
	#endregion

	/// <summary>
	/// Encapsulates the shell folder browse dialog shown by SHBrowseForFolder
	/// </summary>
	public class ShellFolderBrowser : System.ComponentModel.Component
	{
		private string title;
		private IntPtr pidlReturned = IntPtr.Zero;
		private IntPtr handle;
		private string displayName;
		private BrowseFlags flags;
		
		/// <summary>
		/// 
		/// </summary>
		public ShellFolderBrowser()
		{
		}
		
		#region Component properties

		/// <summary>
		/// String that is displayed above the tree view control in the dialog box. 
		/// This string can be used to specify instructions to the user. 
		/// Can only be modified if the dalog is not currently displayed.
		/// </summary>
		[Description("String that is displayed above the tree view control in the dialog box. This string can be used to specify instructions to the user.")] 
		public string Title
		{
			get
			{
				return title;
			}
			set
			{
				if (handle != IntPtr.Zero)
					throw new InvalidOperationException();
				
				title = value;
			}
		}
		
		/// <summary>
		/// The display name of the folder selected by the user
		/// </summary>
		[Description("The display name of the folder selected by the user")]
		public string FolderDisplayName
		{
			get
			{
				return displayName;
			}
		}
		
		/// <summary>
		/// The folder path that was selected
		/// </summary>
		public string FolderPath
		{
			get
			{
				if (pidlReturned == IntPtr.Zero)
					return string.Empty;
				
				StringBuilder pathReturned = new StringBuilder(260);

				UnManagedMethods.SHGetPathFromIDList(pidlReturned, pathReturned);
				return pathReturned.ToString();
			}
		}

		/// <summary>
		/// Sets the flags that control the behaviour of the dialog
		/// </summary>
		public BrowseFlags BrowseFlags
		{
			get
			{
				return flags;
			}
			set
			{
				flags = value;
			}
		}
		
		#endregion

		#region ShowDialog and related methods
		
		private bool ShowDialogInternal(ref BrowseInfo bi)
		{
			bi.title = title;
			bi.displayname = new string('\0', 260);
			bi.callback = new BrowseCallBackProc(this.CallBack);
			bi.flags = (int)flags;
			
			//Free any old pidls
			if (pidlReturned != IntPtr.Zero)
				UnManagedMethods.SHMemFree(pidlReturned);

			bool ret = (pidlReturned = UnManagedMethods.SHBrowseForFolder(ref bi)) != IntPtr.Zero;

			if (ret)
			{
				displayName = bi.displayname;
			}
			
			//Reset the handle
			handle = IntPtr.Zero;

			return ret;
		}
	
		/// <summary>
		/// Shows the dialog
		/// </summary>
		/// <param name="owner">The window to use as the owner</param>
		/// <returns></returns>
		public bool ShowDialog(System.Windows.Forms.IWin32Window owner)
		{
			if (handle != IntPtr.Zero)
				throw new InvalidOperationException();

			BrowseInfo bi = new BrowseInfo();
			
			if (owner != null)
				bi.hwndOwner = owner.Handle;

			return ShowDialogInternal(ref bi);
		}

		/// <summary>
		/// Shows the dialog using active window as the owner
		/// </summary>
		public bool ShowDialog()
		{
			return ShowDialog(Form.ActiveForm);
		}
		#endregion

		#region Functions that send messages to the dialog
		private const int WM_USER				  = 0x0400;

		private const int BFFM_SETSTATUSTEXTA     = (WM_USER   100);
		private const int BFFM_SETSTATUSTEXTW     = (WM_USER   104);
		
		/// <summary>
		/// Sets the text of the staus area of the folder dialog
		/// </summary>
		/// <param name="text">Text to set</param>
		public void SetStatusText(string text)
		{
			if (handle == IntPtr.Zero)
				throw new InvalidOperationException();
			
			int msg = (Environment.OSVersion.Platform == PlatformID.Win32NT) ? BFFM_SETSTATUSTEXTW : BFFM_SETSTATUSTEXTA;
			IntPtr strptr = Marshal.StringToHGlobalAuto(text);

			UnManagedMethods.SendMessage(handle, msg, IntPtr.Zero, strptr);
			
			Marshal.FreeHGlobal(strptr);
		}	

		private const int BFFM_ENABLEOK           = (WM_USER   101);
		
		/// <summary>
		/// Enables or disables the ok button
		/// </summary>
		/// <param name="bEnable">true to enable false to diasble the OK button</param>
		public void EnableOkButton(bool bEnable)
		{
			if (handle == IntPtr.Zero)
				throw new InvalidOperationException();

			IntPtr lp = bEnable ? new IntPtr(1) : IntPtr.Zero;
			
			UnManagedMethods.SendMessage(handle, BFFM_ENABLEOK, IntPtr.Zero, lp);
		}

		private const int BFFM_SETSELECTIONA      = (WM_USER   102);
		private const int BFFM_SETSELECTIONW      = (WM_USER   103);
		
		/// <summary>
		/// Sets the selection the text specified
		/// </summary>
		/// <param name="newsel">The path of the folder which is to be selected</param>
		public void SetSelection(string newsel)
		{
			if (handle == IntPtr.Zero)
				throw new InvalidOperationException();
			
			int msg = (Environment.OSVersion.Platform == PlatformID.Win32NT) ? BFFM_SETSELECTIONA : BFFM_SETSELECTIONW;
			
			IntPtr strptr = Marshal.StringToHGlobalAuto(newsel);

			UnManagedMethods.SendMessage(handle, msg, new IntPtr(1), strptr);
			
			Marshal.FreeHGlobal(strptr);
		}
		
		private const int BFFM_SETOKTEXT          = (WM_USER   105);
		
		/// <summary>
		/// Sets the text of the OK button in the dialog
		/// </summary>
		/// <param name="text">New text of the OK button</param>
		public void SetOkButtonText(string text)
		{
			if (handle == IntPtr.Zero)
				throw new InvalidOperationException();
			
			IntPtr strptr = Marshal.StringToHGlobalUni(text);

			UnManagedMethods.SendMessage(handle, BFFM_SETOKTEXT, new IntPtr(1), strptr);
			
			Marshal.FreeHGlobal(strptr);
		}
		
		private const int BFFM_SETEXPANDED        = (WM_USER   106);
		
		/// <summary>
		/// Expand a path in the folder
		/// </summary>
		/// <param name="path">The path to expand</param>
		public void SetExpanded(string path)
		{
			IntPtr strptr = Marshal.StringToHGlobalUni(path);

			UnManagedMethods.SendMessage(handle, BFFM_SETEXPANDED, new IntPtr(1), strptr);
			
			Marshal.FreeHGlobal(strptr);
		}
		#endregion
		
		#region Callback Handling and Event Propogation
		
		/// <summary>
		/// Fired when the dialog is initialized
		/// </summary>
		public event EventHandler Initialized;
		/// <summary>
		/// Fired when selection changes
		/// </summary>
		public event FolderSelChangedEventHandler SelChanged;
		/// <summary>
		/// Shell provides an IUnknown through this event. For details see documentation of SHBrowseForFolder
		/// </summary>
		public event IUnknownObtainedEventHandler IUnknownObtained;
		/// <summary>
		/// Fired when validation of text typed by user fails
		/// </summary>
		public event ValidateFailedEventHandler ValidateFailed;
		
		private const int BFFM_INITIALIZED        = 1;
		private const int BFFM_SELCHANGED         = 2;
		private const int BFFM_VALIDATEFAILEDA    = 3;
		private const int BFFM_VALIDATEFAILEDW    = 4;
		private const int BFFM_IUNKNOWN           = 5;

		private int CallBack(IntPtr hwnd, int msg, IntPtr lp, IntPtr lpData)
		{
			int ret = 0;

			switch(msg)
			{
				case BFFM_INITIALIZED:
					handle = hwnd;
					if (Initialized != null)
					{
						Initialized(this, null);
					}
					break;
				case BFFM_IUNKNOWN:
					if (IUnknownObtained != null)
					{
						IUnknownObtained(this, new IUnknownObtainedEventArgs(Marshal.GetObjectForIUnknown(lp)));
					}
					break;
				case BFFM_SELCHANGED:
					if (SelChanged != null)
					{
						FolderSelChangedEventArgs e = new FolderSelChangedEventArgs(lp);
						SelChanged(this, e);
					}
					break;
				case BFFM_VALIDATEFAILEDA:
					if (ValidateFailed != null)
					{
						ValidateFailedEventArgs e = new ValidateFailedEventArgs(Marshal.PtrToStringAnsi(lpData));
						ValidateFailed(this, e);
						
						ret = (e.DismissDialog) ? 0 : 1;
					}
					break;
				case BFFM_VALIDATEFAILEDW:
					if (ValidateFailed != null)
					{
						ValidateFailedEventArgs e = new ValidateFailedEventArgs(Marshal.PtrToStringUni(lpData));
						ValidateFailed(this, e);
						
						ret = (e.DismissDialog) ? 0 : 1;
					}
					break;
			}

			return ret;
		}

		#endregion

		protected override void Dispose(bool disposing)
		{
			if (pidlReturned != IntPtr.Zero)
			{
				UnManagedMethods.SHMemFree(pidlReturned);
				pidlReturned = IntPtr.Zero;
			}
		}
	}
}

实例下载地址

winform 文件夹浏览器示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警