(C#)把磁盘目录树加载在窗体菜单中
这又是一个没有技术含量的代码。写出来只是玩玩,所以也不敢放在首页。
这里有个问题,是获取文件/文件夹的图标。使用 System.Drawing.Icon.ExtractAssociatedIcon 只能获取大图标(不知道有没有高手能做到取小图标)。所以只能使用API了。设计一个这样的 ExtractIcon 类,提供一个静态方法 GetIcon ,用于获取小图标......
这又是一个没有技术含量的代码。写出来只是玩玩,所以也不敢放在首页。
这里有个问题,是获取文件/文件夹的图标。使用 System.Drawing.Icon.ExtractAssociatedIcon 只能获取大图标(不知道有没有高手能做到取小图标)。所以只能使用API了。设计一个这样的 ExtractIcon 类,提供一个静态方法 GetIcon ,用于获取小图标:
然后我们通过 ToolStripMenuItem 的 DropDownOpening 事件,加载它的子文件/文件夹。代码比较简单:
源代码:/Files/lemony/FileMenu.rar
这里有个问题,是获取文件/文件夹的图标。使用 System.Drawing.Icon.ExtractAssociatedIcon 只能获取大图标(不知道有没有高手能做到取小图标)。所以只能使用API了。设计一个这样的 ExtractIcon 类,提供一个静态方法 GetIcon ,用于获取小图标:
ExtractIcon.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace FileMenu
{
public class ExtractIcon
{
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
);
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
public string szTypeName;
};
private ExtractIcon()
{
}
private enum SHGFI
{
SmallIcon = 0x00000001,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,
Typename = 0x00000400,
SysIconIndex = 0x00004000,
UseFileAttributes = 0x00000010
}
/**//// <summary>
/// 获取文件、文件夹的小图标
/// </summary>
/// <param name="strPath">文件、文件夹路径</param>
/// <returns>图标</returns>
public static Icon GetIcon(string strPath)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
flags = SHGFI.Icon | SHGFI.SmallIcon;
SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace FileMenu
{
public class ExtractIcon
{
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
);
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
public string szTypeName;
};
private ExtractIcon()
{
}
private enum SHGFI
{
SmallIcon = 0x00000001,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,
Typename = 0x00000400,
SysIconIndex = 0x00004000,
UseFileAttributes = 0x00000010
}
/**//// <summary>
/// 获取文件、文件夹的小图标
/// </summary>
/// <param name="strPath">文件、文件夹路径</param>
/// <returns>图标</returns>
public static Icon GetIcon(string strPath)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
flags = SHGFI.Icon | SHGFI.SmallIcon;
SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
}
}
然后我们通过 ToolStripMenuItem 的 DropDownOpening 事件,加载它的子文件/文件夹。代码比较简单:
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace FileMenu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//加载磁盘
string[] disks = Directory.GetLogicalDrives();
for (int i = 0; i < disks.Length; i++)
{
string disk = disks[i];
ToolStripMenuItem tsm = new ToolStripMenuItem(disk, ExtractIcon.GetIcon(disk).ToBitmap());
tsm.Tag = disk;
tsm.ToolTipText = disk;
tsm.DropDownOpening += new EventHandler(ShowItem);
mnuMain.Items.Add(tsm);
}
}
//加载子菜单
private void ShowItem(object sender, EventArgs e)
{
//用 tag 存储路径。如果 tag 为空,表示该菜单已经加载过。
ToolStripMenuItem tParent = (ToolStripMenuItem)sender;
string path = (string)tParent.Tag;
if (string.IsNullOrEmpty(path))
{
return;
}
tParent.Tag = "";
tParent.DropDownItems.Clear();
DirectoryInfo parent = new DirectoryInfo(path);
try
{
//先加载子文件夹
DirectoryInfo[] dis = parent.GetDirectories();
for (int i = 0; i < dis.Length; i++)
{
DirectoryInfo di = dis[i];
ToolStripMenuItem tsm = new ToolStripMenuItem(di.Name, ExtractIcon.GetIcon(di.FullName).ToBitmap());
tsm.Tag = di.FullName;
tsm.ToolTipText = di.FullName;
tsm.DropDownOpening += new EventHandler(ShowItem);
tsm.Click += new EventHandler(OpenItem);
if (di.GetDirectories().Length > 0 || di.GetFiles().Length > 0)
{
tsm.DropDownItems.Add("");
}
else
{
tsm.Tag = "";
}
tParent.DropDownItems.Add(tsm);
}
//加载子文件
FileInfo[] fis = parent.GetFiles();
for (int j = 0; j < fis.Length; j++)
{
FileInfo fi = fis[j];
ToolStripMenuItem tsm = new ToolStripMenuItem(fi.Name, ExtractIcon.GetIcon(fi.FullName).ToBitmap());
tsm.Tag = fi.FullName;
tsm.ToolTipText = fi.FullName;
tsm.Click += new EventHandler(OpenItem);
tParent.DropDownItems.Add(tsm);
}
}
catch{ }
}
//打开子菜单
private void OpenItem(object sender, EventArgs e)
{
ToolStripMenuItem tsm = (ToolStripMenuItem)sender;
string path = (string)tsm.ToolTipText;
System.Diagnostics.Process.Start(path);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace FileMenu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//加载磁盘
string[] disks = Directory.GetLogicalDrives();
for (int i = 0; i < disks.Length; i++)
{
string disk = disks[i];
ToolStripMenuItem tsm = new ToolStripMenuItem(disk, ExtractIcon.GetIcon(disk).ToBitmap());
tsm.Tag = disk;
tsm.ToolTipText = disk;
tsm.DropDownOpening += new EventHandler(ShowItem);
mnuMain.Items.Add(tsm);
}
}
//加载子菜单
private void ShowItem(object sender, EventArgs e)
{
//用 tag 存储路径。如果 tag 为空,表示该菜单已经加载过。
ToolStripMenuItem tParent = (ToolStripMenuItem)sender;
string path = (string)tParent.Tag;
if (string.IsNullOrEmpty(path))
{
return;
}
tParent.Tag = "";
tParent.DropDownItems.Clear();
DirectoryInfo parent = new DirectoryInfo(path);
try
{
//先加载子文件夹
DirectoryInfo[] dis = parent.GetDirectories();
for (int i = 0; i < dis.Length; i++)
{
DirectoryInfo di = dis[i];
ToolStripMenuItem tsm = new ToolStripMenuItem(di.Name, ExtractIcon.GetIcon(di.FullName).ToBitmap());
tsm.Tag = di.FullName;
tsm.ToolTipText = di.FullName;
tsm.DropDownOpening += new EventHandler(ShowItem);
tsm.Click += new EventHandler(OpenItem);
if (di.GetDirectories().Length > 0 || di.GetFiles().Length > 0)
{
tsm.DropDownItems.Add("");
}
else
{
tsm.Tag = "";
}
tParent.DropDownItems.Add(tsm);
}
//加载子文件
FileInfo[] fis = parent.GetFiles();
for (int j = 0; j < fis.Length; j++)
{
FileInfo fi = fis[j];
ToolStripMenuItem tsm = new ToolStripMenuItem(fi.Name, ExtractIcon.GetIcon(fi.FullName).ToBitmap());
tsm.Tag = fi.FullName;
tsm.ToolTipText = fi.FullName;
tsm.Click += new EventHandler(OpenItem);
tParent.DropDownItems.Add(tsm);
}
}
catch{ }
}
//打开子菜单
private void OpenItem(object sender, EventArgs e)
{
ToolStripMenuItem tsm = (ToolStripMenuItem)sender;
string path = (string)tsm.ToolTipText;
System.Diagnostics.Process.Start(path);
}
}
}
源代码:/Files/lemony/FileMenu.rar