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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
using System;
using System.Collections.Generic;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
public sealed class SystemIconsImageListWrapper : Object
{
public SystemIconsImageListWrapper()
{
this.SIImageList = new ImageList();
this.ExtensionSIImageListIndexZuordnung = new Dictionary<string, int>();
}
public SystemIconsImageListWrapper(ImageList IL, int DefaultIconIndex)
{
this.SIImageList = IL;
this.ExtensionSIImageListIndexZuordnung = new Dictionary<string, int>();
this.DefaultIconIndex = DefaultIconIndex;
}
public ImageList SIImageList { get; set; }
private Dictionary<string, int> ExtensionSIImageListIndexZuordnung;
private int DefaultIconIndex = 0;
public int GetSIImageListIndexForFileExtension(string Extension)
{
try
{
int RValue = 0;
if (!Extension.StartsWith("."))
Extension = "." + Extension;
if (this.ExtensionSIImageListIndexZuordnung.ContainsKey(Extension))
this.ExtensionSIImageListIndexZuordnung.TryGetValue(Extension, out RValue);
else //noch nicht vorhanden -> laden
{
this.SIImageList.Images.Add(this.GetIconForFileExtension(Extension));
this.ExtensionSIImageListIndexZuordnung.Add(Extension, this.SIImageList.Images.Count - 1);
this.ExtensionSIImageListIndexZuordnung.TryGetValue(Extension, out RValue);
}
return RValue;
}
catch (Exception ex) //Bei Fehler einfach DefaultIconIndex zurückgeben
{
ex.ToString(); //Suppress Visual Studio unused variable warning
return this.DefaultIconIndex;
}
}
private Icon GetIconForFileExtension(string Extension)
{
if (!Extension.StartsWith("."))
Extension = "." + Extension;
KeyValuePair<string, int> QryRS = this.GetIconPathForExtension(Extension);
if (!string.IsNullOrEmpty(QryRS.Key))
return this.GetIconFromDLL(QryRS.Key, QryRS.Value);
else //Icon nicht gefunden oder Fehler -> Default Icon verwenden
return this.ImageToIcon(this.SIImageList.Images[this.DefaultIconIndex]);
}
private KeyValuePair<string, int> GetIconPathForExtension(string Extension)
{
if (!Extension.StartsWith("."))
Extension = "." + Extension;
KeyValuePair<string, int> RValue = new KeyValuePair<string, int>(string.Empty, 0);
try
{
RegistryKey ClassRootKey = Registry.ClassesRoot;
string FileExtSubKeyName = ClassRootKey.OpenSubKey(Extension).GetValue("").ToString(); //Default Value
string IconPathRaw = ClassRootKey.OpenSubKey(FileExtSubKeyName).OpenSubKey("DefaultIcon").GetValue("").ToString(); //Default Value
RValue = new KeyValuePair<string, int>(IconPathRaw.Split(',')[0], int.Parse(IconPathRaw.Split(',')[1]));
}
catch (Exception ex)
{
ex.ToString(); //Suppress Visual Studio unused variable warning
}
return RValue;
}
private Icon GetIconFromDLL(string PathToDLL, int IconIndex)
{
IntPtr EigenesProzessHandle = Process.GetCurrentProcess().Handle;
IntPtr DLLIconPointer = ExtractIcon(EigenesProzessHandle, PathToDLL, IconIndex);
return Icon.FromHandle(DLLIconPointer);
}
[DllImport("shell32.dll")]
private static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);
private Icon ImageToIcon(Image img)
{
int size = 24;
using (Bitmap square = new Bitmap(size, size))
{
Graphics g = Graphics.FromImage(square);
int x;
int y;
int w;
int h;
float r = (float)img.Width / (float)img.Height;
if (r > 1)
{
w = size;
h = (int)((float)size / r);
x = 0;
y = (size - h) / 2;
}
else
{
w = (int)((float)size * r);
h = size;
y = 0;
x = (size - w) / 2;
}
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, x, y, w, h);
g.Flush();
return Icon.FromHandle(square.GetHicon());
}
}
}
|