-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuFileItem.cs
More file actions
38 lines (33 loc) · 1.23 KB
/
MenuFileItem.cs
File metadata and controls
38 lines (33 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace PersonalFolder {
class MenuFileItem : ToolStripMenuItem {
public readonly string path;
public MenuFileItem (string path) {
this.path = path;
Image = ExtractFileIcon(path);
Text = Path.GetFileNameWithoutExtension(path);
}
private static Dictionary<string, Bitmap> extensions = new Dictionary<string, Bitmap>();
private static Bitmap ExtractFileIcon (string path) {
var extension = Path.GetExtension(path);
if (extensions.ContainsKey(extension))
return extensions[extension];
Win32.SHFILEINFO shellInfo = new Win32.SHFILEINFO();
Win32.SHGetFileInfo(
path,
Win32.FILE_ATTRIBUTE_NORMAL,
ref shellInfo,
(uint) System.Runtime.InteropServices.Marshal.SizeOf(shellInfo),
Win32.SHGFI_ICON | Win32.SHGFI_USEFILEATTRIBUTES | Win32.SHGFI_SMALLICON
);
var icon = (Icon) Icon.FromHandle(shellInfo.hIcon).Clone();
Win32.DestroyIcon(shellInfo.hIcon);
var bitmap = icon.ToBitmap();
extensions.Add(extension, bitmap);
return bitmap;
}
}
}