(C#)把磁盘目录树加载在窗体菜单中

这又是一个没有技术含量的代码。写出来只是玩玩,所以也不敢放在首页。



这里有个问题,是获取文件/文件夹的图标。使用 System.Drawing.Icon.ExtractAssociatedIcon 只能获取大图标(不知道有没有高手能做到取小图标)。所以只能使用API了。设计一个这样的 ExtractIcon 类,提供一个静态方法 GetIcon ,用于获取小图标:

ContractedBlock.gif ExpandedBlockStart.gif ExtractIcon.cs
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Runtime.InteropServices;
None.gif
using System.Drawing;
None.gif
None.gif
namespace FileMenu
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class ExtractIcon
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [DllImport(
"Shell32.dll")]
InBlock.gif        
private static extern int SHGetFileInfo
InBlock.gif          (
InBlock.gif          
string pszPath,
InBlock.gif          
uint dwFileAttributes,
InBlock.gif          
out   SHFILEINFO psfi,
InBlock.gif          
uint cbfileInfo,
InBlock.gif          SHGFI uFlags
InBlock.gif          );
InBlock.gif 
InBlock.gif        [StructLayout(LayoutKind.Sequential)]
InBlock.gif        
private struct SHFILEINFO
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public SHFILEINFO(bool b)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                hIcon 
= IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
public IntPtr hIcon;
InBlock.gif            
public int iIcon;
InBlock.gif            
public uint dwAttributes;
InBlock.gif            [MarshalAs(UnmanagedType.LPStr, SizeConst 
= 260)]
InBlock.gif            
public string szDisplayName;
InBlock.gif            [MarshalAs(UnmanagedType.LPStr, SizeConst 
= 80)]
InBlock.gif            
public string szTypeName;
ExpandedSubBlockEnd.gif        }
;
InBlock.gif 
InBlock.gif        
private ExtractIcon()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif 
InBlock.gif        
private enum SHGFI
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SmallIcon 
= 0x00000001,
InBlock.gif            LargeIcon 
= 0x00000000,
InBlock.gif            Icon 
= 0x00000100,
InBlock.gif            DisplayName 
= 0x00000200,
InBlock.gif            Typename 
= 0x00000400,
InBlock.gif            SysIconIndex 
= 0x00004000,
InBlock.gif            UseFileAttributes 
= 0x00000010
ExpandedSubBlockEnd.gif        }

InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取文件、文件夹的小图标
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strPath">文件、文件夹路径</param>
ExpandedSubBlockEnd.gif        
/// <returns>图标</returns>

InBlock.gif        public static Icon GetIcon(string strPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SHFILEINFO info 
= new SHFILEINFO(true);
InBlock.gif            
int cbFileInfo = Marshal.SizeOf(info);
InBlock.gif            SHGFI flags;
InBlock.gif            flags 
= SHGFI.Icon | SHGFI.SmallIcon;
InBlock.gif            SHGetFileInfo(strPath, 
256out   info, (uint)cbFileInfo, flags);
InBlock.gif            
return Icon.FromHandle(info.hIcon);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

然后我们通过 ToolStripMenuItem 的 DropDownOpening 事件,加载它的子文件/文件夹。代码比较简单:
ContractedBlock.gif ExpandedBlockStart.gif Form1.cs
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.ComponentModel;
None.gif
using System.IO;
None.gif
using System.Data;
None.gif
using System.Drawing;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
using System.Collections;
None.gif
None.gif
namespace FileMenu
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public partial class Form1 : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//加载磁盘
InBlock.gif
            string[] disks = Directory.GetLogicalDrives();
InBlock.gif            
for (int i = 0; i < disks.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string disk = disks[i];
InBlock.gif                ToolStripMenuItem tsm 
= new ToolStripMenuItem(disk, ExtractIcon.GetIcon(disk).ToBitmap());
InBlock.gif                tsm.Tag 
= disk;
InBlock.gif                tsm.ToolTipText 
= disk;
InBlock.gif                tsm.DropDownOpening 
+= new EventHandler(ShowItem);
InBlock.gif                mnuMain.Items.Add(tsm);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//加载子菜单
InBlock.gif
        private void ShowItem(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//用 tag 存储路径。如果 tag 为空,表示该菜单已经加载过。
InBlock.gif
            ToolStripMenuItem tParent = (ToolStripMenuItem)sender;
InBlock.gif            
string path = (string)tParent.Tag;
InBlock.gif            
if (string.IsNullOrEmpty(path))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            tParent.Tag 
= "";                                                                                                                                                                                  
InBlock.gif            tParent.DropDownItems.Clear();
InBlock.gif
InBlock.gif            DirectoryInfo parent 
= new DirectoryInfo(path);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//先加载子文件夹
InBlock.gif
                DirectoryInfo[] dis = parent.GetDirectories();
InBlock.gif                
for (int i = 0; i < dis.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DirectoryInfo di 
= dis[i];
InBlock.gif                    ToolStripMenuItem tsm 
= new ToolStripMenuItem(di.Name, ExtractIcon.GetIcon(di.FullName).ToBitmap());
InBlock.gif                    tsm.Tag 
= di.FullName;
InBlock.gif                    tsm.ToolTipText 
= di.FullName;
InBlock.gif                    tsm.DropDownOpening 
+= new EventHandler(ShowItem);
InBlock.gif                    tsm.Click 
+= new EventHandler(OpenItem);
InBlock.gif
InBlock.gif                    
if (di.GetDirectories().Length > 0 || di.GetFiles().Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        tsm.DropDownItems.Add(
"dot.gif");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        tsm.Tag 
= "";
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    tParent.DropDownItems.Add(tsm);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
//加载子文件
InBlock.gif
                FileInfo[] fis = parent.GetFiles();
InBlock.gif                
for (int j = 0; j < fis.Length; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    FileInfo fi 
= fis[j];
InBlock.gif                    ToolStripMenuItem tsm 
= new ToolStripMenuItem(fi.Name, ExtractIcon.GetIcon(fi.FullName).ToBitmap());
InBlock.gif                    tsm.Tag 
= fi.FullName;
InBlock.gif                    tsm.ToolTipText 
= fi.FullName;
InBlock.gif                    tsm.Click 
+= new EventHandler(OpenItem);
InBlock.gif                    tParent.DropDownItems.Add(tsm);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catchdot.gif{ }
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//打开子菜单
InBlock.gif
        private void OpenItem(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ToolStripMenuItem tsm 
= (ToolStripMenuItem)sender;
InBlock.gif            
string path = (string)tsm.ToolTipText;
InBlock.gif            System.Diagnostics.Process.Start(path);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

源代码: /Files/lemony/FileMenu.rar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值