明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

把昨天寫的東西的代碼公布出來。哎。。真惡心。

[摘要]非要自己寫。算了。。大家自己看看吧。。不過我還是喜歡使用API。嘿嘿目錄選擇對話框類using System;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace Bl...
非要自己寫。
算了。。大家自己看看吧。。不過我還是喜歡使用API。嘿嘿

目錄選擇對話框類
using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Blood.Com.ClassLib
{
/// <summary>
/// 目錄對話框控件
/// </summary>
public class DirectoryDialog
{
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
///<summary>
///瀏覽信息
///</summary>
public struct BROWSEINFO
{
public IntPtr hWndOwner;
public int pIDLRoot;
public string pszDisplayName;
public string lpszTitle;
public int ulFlags;
public int lpfnCallback;
public int lParam;
public int iImage;
}

private const int MAX_PATH = 260;
/// <summary>
/// 指定瀏覽類型
/// </summary>
public enum BrowseForTypes
{
/// <summary>
/// 瀏覽計算機
/// </summary>
Computers = 0x1000,
/// <summary>
/// 瀏覽目錄
/// </summary>
Directories = 0x1,
/// <summary>
/// 瀏覽目錄和文件
/// </summary>
/// <remarks>只能工作于4.71或更高的版本<remarks>
FilesAndDirectories = 0x4000, // 4.71版
/// <summary>
/// 瀏覽系統(tǒng)根目錄
/// </summary>
FileSystemAncestors = 0x8
}

[ DllImport( "ole32.dll")]
private static extern int CoTaskMemFree(IntPtr hMem);
[ DllImport( "kernel32.dll")]
private static extern IntPtr lstrcat(string lpString1, string lpString2);
[ DllImport( "shell32.dll")]
private static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi);
[ DllImport( "shell32.dll")]
private static extern int SHGetPathFromIDList(IntPtr pidList, StringBuilder lpBuffer);
/// <summary>
/// 顯示公共文件夾對話框
/// </summary>
/// <param name="hWndOwner">文件夾對話框所有者</param>
protected bool RunDialog(IntPtr hWndOwner)
{
BROWSEINFO udtBI = new BROWSEINFO();
IntPtr lpIDList;
GCHandle hTitle = GCHandle.Alloc(Title, GCHandleType.Pinned);
// 設置WINDOWS的所有者
udtBI.hWndOwner = hWndOwner;
// 設置WINDOWS的所有者
udtBI.lpszTitle =Title;
// 設置WINDOWS的所有者
udtBI.ulFlags= (int)BrowseFor;
// 創(chuàng)建一個字符串緩沖用來顯示名稱
StringBuilder buffer = new StringBuilder(MAX_PATH);
buffer.Length = MAX_PATH;
udtBI.pszDisplayName = buffer.ToString();
// 顯示瀏覽目錄對話框
lpIDList = SHBrowseForFolder(ref udtBI);
hTitle.Free();
if (lpIDList.ToInt64() != 0)
{
if (BrowseFor == BrowseForTypes.Computers)
{
m_Selected = udtBI.pszDisplayName.Trim();
}
else
{
StringBuilder path = new StringBuilder(MAX_PATH);
//從lpIDList中取得路徑
SHGetPathFromIDList(lpIDList, path);
m_Selected = path.ToString();
}
//釋放內存
CoTaskMemFree(lpIDList);
}
else
{
return false;
}
return true;
}
/// <summary>顯示公共文件夾對話框</summary>
public DialogResult ShowDialog()
{
return ShowDialog(null);
}
/// <summary>Shows the common folder dialog.</summary>
/// <param name="owner">The owner of the folder dialog.</param>
public DialogResult ShowDialog(IWin32Window owner)
{
IntPtr handle;
if (owner != null)
handle = owner.Handle;
else
handle = IntPtr.Zero;
if (RunDialog(handle))
{
return DialogResult.OK;
}
else
{
return DialogResult.Cancel;
}
}

/// <summary>
/// 指定對話框的標題
/// </summary>
/// <value>對話框標題</value>
/// <exceptions cref="ArgumentNullException">當值為null(VB.NET為Nothing)時拋出錯誤</exceptions>
public string Title
{
get
{
return m_Title;
}
set
{
if (value == null)
throw new ArgumentNullException();
m_Title = value;
}
}
/// <summary>返回選擇的項目</summary>
/// <value>選擇的項目</value>
public string Selected
{
get
{
return m_Selected;
}
}
/// <summary>指定瀏覽類型</summary>
/// <value>瀏覽類型</value>
public BrowseForTypes BrowseFor
{
get
{
return m_BrowseFor;
}
set
{
m_BrowseFor = value;
}
}
//申明私有變量
private BrowseForTypes m_BrowseFor = BrowseForTypes.Directories;
private string m_Title = "";
private string m_Selected = "";

/// <summary>
/// 構造函數(shù)
/// </summary>
public DirectoryDialog()
{
//
// TODO: 在此處添加構造函數(shù)邏輯
//
}
}
}