最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#用Treeview實現(xiàn)FolderBrowerDialog 和動態(tài)獲取系統(tǒng)圖標(運用了Win32 dll類庫)

 更新時間:2013年03月03日 10:58:18   投稿:shangke  
其實,FolderBrowerDialog 很好用呢,有木有啊親,反正我特別的喜歡,微軟大哥把這個瀏覽文件夾的東東封裝的多好呀

事情是這樣子的。我需要做一個下面的東東:

這個不難啊,然后就用FolderBrowerDialog這個神器,嗯 還不錯,剛開始客戶用了也很喜歡。
 
可是過了一段時間之后,客戶說 要屏蔽右鍵功能,他不想讓其他通過右鍵能打開或瀏覽文件夾,如下面 紅色要給屏蔽。

我一開始以為只是一個參數問題,就爽快的答應了客戶咯。可是啊后來找啊找 找到天荒地老也木有找到。。。放棄了,然后改用了TreeView。。結果,版本出來了,先截圖:

好吧,確實很丑哦。。

復制代碼 代碼如下:

public MyDirectory()
      {
          InitializeComponent();
          treeViewDirectory.BeginUpdate();
          label1.Text = folderTitle;
          treeViewDirectory.ImageList = imageList1;
          treeViewDirectory.SelectedImageIndex = 3;
          EnumDrivers();
          treeViewDirectory.EndUpdate();
 
          this.SetBounds((Screen.GetBounds(this).Width / 2) - (this.Width / 2), (Screen.GetBounds(this).Height / 2) - (this.Height / 2), this.Width, this.Height, BoundsSpecified.Location);
      }
      public static string folderTitle = "";
      private void EnumDrivers()
      {
          //treeViewDirectory.ImageIndex = 1;
          string[] allDriveNames = Directory.GetLogicalDrives();
          TreeNode rootNode = new TreeNode();
          rootNode.Text = "My Computer";
          rootNode.ImageIndex = 1;
          rootNode.Expand();
          treeViewDirectory.Nodes.Add(rootNode);
          treeViewDirectory.SelectedNode = rootNode.FirstNode;
          DriveInfo[] allDrives = DriveInfo.GetDrives();
          int j = 0;
          try
          {
              int i = 0;
              foreach (DriveInfo d in allDrives)
              {
                  TreeNode tn = new TreeNode(d.Name);
 
                  // GetIcon(d.Name, false)
                  this.imageList1.Images.Add(GetIcon(d.Name, false));
 
                  tn.ImageIndex = 2;
               
                  tn.Tag = d.RootDirectory.FullName;
                  treeViewDirectory.Nodes[0].Nodes.Add(tn);
                   treeViewDirectory.Nodes[0].Nodes[j].Text = d.Name ;
                  ShowDirs(tn);
                  j++;
              }
          }
          catch (System.Exception)
          {
          }
      }
 
      private void ShowDirs(TreeNode tn)
      {
          tn.Nodes.Clear();
          try
          {
              DirectoryInfo DirInfo = new DirectoryInfo(tn.Tag.ToString());
              if (!DirInfo.Exists)
              {
                  return;
              }
              else
              {
                  DirectoryInfo[] Dirs;
                  try
                  {
                      Dirs = DirInfo.GetDirectories();
                  }
                  catch (Exception e)
                  {
                      return;
                  }
                  foreach (DirectoryInfo Dir in Dirs)
                  {
                      TreeNode dir = new TreeNode(Dir.Name);
                      dir.ImageIndex = 0;
                      dir.Tag = Dir.FullName;
                      tn.Nodes.Add(dir);
                  }
              }
          }
          catch (System.Exception)
          { }
      }
 
      private void treeViewDirectory_BeforeExpand(object sender, TreeViewCancelEventArgs e)
      {
          treeViewDirectory.BeginUpdate();
          foreach (TreeNode tn in e.Node.Nodes)
          {
              ShowDirs(tn);
          }
          treeViewDirectory.EndUpdate();
      }
 
      public static string myValue { set; get; }
      private void btnOK_Click(object sender, EventArgs e)
      {
          MyDirectory.myValue = lastResult;
          this.Close();
      }
 
      private void btnCancel_Click(object sender, EventArgs e)
      {
          MyDirectory.myValue = null;
          this.Close();
      }
      private static string lastResult = null;
      private void treeViewDirectory_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
      {
          lastResult = null;
          string result = e.Node.FullPath;
          if (result != "My Computer")
          {
              if (result.Contains(@"My Computer\") || result.Contains("My Computer"))
              {
                  int len = 0;
                  if (result.Contains(@"My Computer\"))
                  {
                      len = @"My Computer\".Length;
                  }
                  else
                  {
                      len = "My Computer".Length;
                  }
                  result = result.Substring(len);
return result;
              }
          }
      }

雖然 這個時候,把右鍵點擊功能給取消啦,但是接著用戶提了三個要求:

1.需要系統(tǒng)自動匹配它的圖標

2.要有磁盤容量的大小。。

好吧,然后最后修改一下。這里面用到了 Win32 dll的幾個函數,確實很好用呢。。贊一個。。

復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
 
namespace HP.DMT.UI
{
    public partial class MyDirectory : Form
    {
 
        public MyDirectory()
        {
            InitializeComponent();
            treeViewDirectory.BeginUpdate();
            label1.Text = folderTitle;
            treeViewDirectory.ImageList = imageList1;
            treeViewDirectory.SelectedImageIndex = 3;
            EnumDrivers();
            treeViewDirectory.EndUpdate();
 
            this.SetBounds((Screen.GetBounds(this).Width / 2) - (this.Width / 2), (Screen.GetBounds(this).Height / 2) - (this.Height / 2), this.Width, this.Height, BoundsSpecified.Location);
        }
        public static string folderTitle = "";
        private void EnumDrivers()
        {
            //treeViewDirectory.ImageIndex = 1;
            string[] allDriveNames = Directory.GetLogicalDrives();
            TreeNode rootNode = new TreeNode();
            rootNode.Text = "My Computer";
            rootNode.ImageIndex = 1;
            rootNode.Expand();
            treeViewDirectory.Nodes.Add(rootNode);
            treeViewDirectory.SelectedNode = rootNode.FirstNode;
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            int j = 0;
            try
            {
                int i = 0;
                foreach (DriveInfo d in allDrives)
                {
                    TreeNode tn = new TreeNode(d.Name);
 
                    // GetIcon(d.Name, false)
                    this.imageList1.Images.Add(GetIcon(d.Name, false));
 
                    tn.ImageIndex = 4 + i;
                    i++;
                    tn.Tag = d.RootDirectory.FullName;
                    treeViewDirectory.Nodes[0].Nodes.Add(tn);
                    if (d.DriveType.ToString() == "Fixed")
                    {
                        treeViewDirectory.Nodes[0].Nodes[j].Text = d.Name + "(" + d.DriveType.ToString() + "," + d.TotalFreeSpace / 1024 / 1024 / 1024 + "G/" + d.TotalSize / 1024 / 1024 / 1024 + "G)";
                    }
                    else
                    {
                        treeViewDirectory.Nodes[0].Nodes[j].Text = d.Name + "(" + d.DriveType.ToString() + ")";
                    }
                    ShowDirs(tn);
                    j++;
                }
            }
            catch (System.Exception)
            {
            }
        }
 
        private void ShowDirs(TreeNode tn)
        {
            tn.Nodes.Clear();
            try
            {
                DirectoryInfo DirInfo = new DirectoryInfo(tn.Tag.ToString());
                if (!DirInfo.Exists)
                {
                    return;
                }
                else
                {
                    DirectoryInfo[] Dirs;
                    try
                    {
                        Dirs = DirInfo.GetDirectories();
                    }
                    catch (Exception e)
                    {
                        return;
                    }
                    foreach (DirectoryInfo Dir in Dirs)
                    {
                        TreeNode dir = new TreeNode(Dir.Name);
                        dir.ImageIndex = 0;
                        dir.Tag = Dir.FullName;
                        tn.Nodes.Add(dir);
                    }
                }
            }
            catch (System.Exception)
            { }
        }
 
        private void treeViewDirectory_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            treeViewDirectory.BeginUpdate();
            foreach (TreeNode tn in e.Node.Nodes)
            {
                ShowDirs(tn);
            }
            treeViewDirectory.EndUpdate();
        }
 
        public static string myValue { set; get; }
        private void btnOK_Click(object sender, EventArgs e)
        {
            MyDirectory.myValue = lastResult;
            this.Close();
        }
 
        private void btnCancel_Click(object sender, EventArgs e)
        {
            MyDirectory.myValue = null;
            this.Close();
        }
        private static string lastResult = null;
        private void treeViewDirectory_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            lastResult = null;
            string result = e.Node.FullPath;
            if (result != "My Computer")
            {
                if (result.Contains(@"My Computer\") || result.Contains("My Computer"))
                {
                    int len = 0;
                    if (result.Contains(@"My Computer\"))
                    {
                        len = @"My Computer\".Length;
                    }
                    else
                    {
                        len = "My Computer".Length;
                    }
                    result = result.Substring(len);
 
                    char[] arrs = result.ToCharArray();
                    int beforLenth = result.Remove(result.IndexOf('/') + 1).Length;
                    int afterLenth = result.Substring(result.IndexOf('/') + 1).Remove(result.Substring(result.IndexOf('/') + 1).IndexOf(')')).Length;
 
                    char[] c = { ')' };
                    string str1 = result.Substring(0, 3);
                    string str2 = result.Substring(result.IndexOfAny(c, beforLenth + afterLenth, 1) + 1);
 
                    lastResult = str1 + str2;
                }
            }
        }
   
 
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct SHFILEINFO
        {
            public IntPtr hIcon;
            public int iIcon;
            public uint dwAttributes;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
            public string szTypeName;
        }
 
        [DllImport("Shell32.dll", EntryPoint = "SHGetFileInfo", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
 
        [DllImport("User32.dll", EntryPoint = "DestroyIcon")]
        public static extern int DestroyIcon(IntPtr hIcon);
 
 
        public const uint SHGFI_ICON = 0x100;
        public const uint SHGFI_LARGEICON = 0x0;
        public const uint SHGFI_SMALLICON = 0x1;
        public const uint SHGFI_USEFILEATTRIBUTES = 0x10;
 
        static Icon GetIcon(string fileName, bool isLargeIcon)
        {
            SHFILEINFO shfi = new SHFILEINFO();
            IntPtr hI;
 
            if (isLargeIcon)
                hI = MyDirectory.SHGetFileInfo(fileName, 0, ref shfi,
                     (uint)Marshal.SizeOf(shfi), MyDirectory.SHGFI_ICON | MyDirectory.SHGFI_USEFILEATTRIBUTES | MyDirectory.SHGFI_LARGEICON);
            else
                hI = MyDirectory.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), MyDirectory.SHGFI_ICON | MyDirectory.SHGFI_USEFILEATTRIBUTES | MyDirectory.SHGFI_SMALLICON);
            Icon icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
            MyDirectory.DestroyIcon(shfi.hIcon);
            return icon;
        }
    }
}

結果如下:

核心代碼是:

復制代碼 代碼如下:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct SHFILEINFO
        {
            public IntPtr hIcon;
            public int iIcon;
            public uint dwAttributes;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
            public string szTypeName;
        }
 
        [DllImport("Shell32.dll", EntryPoint = "SHGetFileInfo", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
 
        [DllImport("User32.dll", EntryPoint = "DestroyIcon")]
        public static extern int DestroyIcon(IntPtr hIcon);
 
 
        public const uint SHGFI_ICON = 0x100;
        public const uint SHGFI_LARGEICON = 0x0;
        public const uint SHGFI_SMALLICON = 0x1;
        public const uint SHGFI_USEFILEATTRIBUTES = 0x10;
 
        static Icon GetIcon(string fileName, bool isLargeIcon)
        {
            SHFILEINFO shfi = new SHFILEINFO();
            IntPtr hI;
 
            if (isLargeIcon)
                hI = MyDirectory.SHGetFileInfo(fileName, 0, ref shfi,
                     (uint)Marshal.SizeOf(shfi), MyDirectory.SHGFI_ICON | MyDirectory.SHGFI_USEFILEATTRIBUTES | MyDirectory.SHGFI_LARGEICON);
            else
                hI = MyDirectory.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), MyDirectory.SHGFI_ICON | MyDirectory.SHGFI_USEFILEATTRIBUTES | MyDirectory.SHGFI_SMALLICON);
            Icon icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
            MyDirectory.DestroyIcon(shfi.hIcon);
            return icon;
        }

很好懂呢,只需要在程序中調用一下就ok啦。

作者:Lanny☆蘭東才
出處:http://www.cnblogs.com/damonlan
Q Q:*********
E_mail:Damon_lan@163.com or Dongcai.lan@hp.com

本博文歡迎大家瀏覽和轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,在『參考』的文章中,我會表明參考的文章來源,尊重他人版權。若您發(fā)現(xiàn)我侵犯了您的版權,請及時與我聯(lián)系。

相關文章

  • c#通過app.manifest使程序以管理員身份運行

    c#通過app.manifest使程序以管理員身份運行

    通常我們使用c#編寫的程序不會彈出這個提示,也就無法以管理員身分運行。微軟的操作系統(tǒng)使用微軟的產品方法當然是有的,通過app.manifest配置可以使程序打開的時候,彈出UAC提示需要得到允許才可以繼續(xù),這樣就獲得了管理員的權限來執(zhí)行程序
    2015-01-01
  • C#控制Excel Sheet使其自適應頁寬與列寬的方法

    C#控制Excel Sheet使其自適應頁寬與列寬的方法

    這篇文章主要介紹了C#控制Excel Sheet使其自適應頁寬與列寬的方法,涉及C#操作Excel的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • C#實現(xiàn)文件操作(復制,移動,刪除)的方法詳解

    C#實現(xiàn)文件操作(復制,移動,刪除)的方法詳解

    File類提供了常見的文件操作函數,包括復制、移動、刪除、創(chuàng)建快捷方式等,本文將通過一些簡單的示例為大家詳細講講具體的使用,希望對大家有所幫助
    2023-05-05
  • 使用C#操作ftp服務器的示例代碼

    使用C#操作ftp服務器的示例代碼

    這篇文章主要為大家詳細介紹了使用C#操作ftp服務器的相關知識,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下
    2024-02-02
  • C#使用Monitor類實現(xiàn)線程同步

    C#使用Monitor類實現(xiàn)線程同步

    這篇文章介紹了C#使用Monitor類實現(xiàn)線程同步的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#判斷指定驅動器是否是Fat分區(qū)格式的方法

    C#判斷指定驅動器是否是Fat分區(qū)格式的方法

    這篇文章主要介紹了C#判斷指定驅動器是否是Fat分區(qū)格式的方法,涉及C#中DriveFormat屬性的使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • 如何獲取C#中方法的執(zhí)行時間以及其代碼注入詳解

    如何獲取C#中方法的執(zhí)行時間以及其代碼注入詳解

    這篇文章主要給大家介紹了關于如何獲取C#中方法的執(zhí)行時間以及其代碼注入的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧
    2018-11-11
  • C#編程中常見數據結構的比較(Unity3D游戲開發(fā))

    C#編程中常見數據結構的比較(Unity3D游戲開發(fā))

    在本篇內容里我們給大家整理了關于Unity3D游戲開發(fā)中C#編程中常見數據結構的比較相關知識點內容,需要的朋友們參考下。
    2019-05-05
  • unity實現(xiàn)簡單抽獎系統(tǒng)

    unity實現(xiàn)簡單抽獎系統(tǒng)

    這篇文章主要為大家詳細介紹了unity實現(xiàn)簡單抽獎系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#結合JS實現(xiàn)HtmlTable動態(tài)添加行并保存到數據庫的流程步驟

    C#結合JS實現(xiàn)HtmlTable動態(tài)添加行并保存到數據庫的流程步驟

    在 Web 應用項目中,實現(xiàn)一對多錄入的數據管理功能是一項常見的應用,因此可以實現(xiàn)一個相對輕量化的設計實現(xiàn)表格的錄入,為保證功能的可用性、界面友好性,本文給大家介紹了C#結合JS實現(xiàn)HtmlTable動態(tài)添加行并保存到數據庫,需要的朋友可以參考下
    2024-12-12

最新評論

西峡县| 望江县| 磐安县| 克拉玛依市| 宁南县| 南乐县| 阳曲县| 耿马| 南漳县| 洞头县| 博乐市| 蒙城县| 体育| 漯河市| 尼玛县| 班玛县| 吉安县| 平果县| 喀喇| 蓝山县| 福鼎市| 吉木乃县| 海盐县| 黄浦区| 庆城县| 宝鸡市| 蕉岭县| 太和县| 景东| 江华| 龙南县| 连山| 辉县市| 阜平县| 民权县| 呼伦贝尔市| 旌德县| 关岭| 磐石市| 临湘市| 兴安县|