-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Icon.cs
217 lines (187 loc) · 8.06 KB
/
Icon.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Windows.Win32;
using Windows.Foundation.Metadata;
using Windows.Win32.UI.WindowsAndMessaging;
using Windows.Win32.Foundation;
namespace WinUIEx
{
/// <summary>
/// Manages a native Windows Icon instance
/// </summary>
[CreateFromString(MethodName = "WinUIEx.Icon.FromFile")]
public unsafe class Icon : IDisposable
{
private readonly HICON handle;
private readonly Microsoft.Win32.SafeHandles.SafeFileHandle? _fileHandle;
private Icon(Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle)
{
_fileHandle = fileHandle; //pin
bool hInstanceAddRef = false;
fileHandle.DangerousAddRef(ref hInstanceAddRef);
handle = new HICON(fileHandle.DangerousGetHandle());
}
private Icon(HICON icon)
{
handle = icon;
}
internal HICON Handle => handle;
/// <summary>
/// Loads an icon from an .ico file.
/// </summary>
/// <param name="filename">Path to file</param>
/// <returns>Icon</returns>
public static Icon FromFile(string filename)
{
var handle = PInvoke.LoadImage(null, filename, GDI_IMAGE_TYPE.IMAGE_ICON, 16, 16, Windows.Win32.UI.Controls.IMAGE_FLAGS.LR_LOADFROMFILE);
ThrowIfInvalid(handle);
return new Icon(handle);
}
/// <summary>
/// Creates an icon from a raw icon byte array
/// </summary>
/// <param name="rgba">RGBA byte array</param>
/// <param name="size">The width and height of the image</param>
/// <returns></returns>
public static Icon FromByteArray(byte[] rgba, uint size)
{
byte[] ANDmaskIcon = new byte[size * size * 3];
byte[] XORmaskIcon = new byte[size * size];
for (int i = 0; i < size * size; i++)
{
ANDmaskIcon[i * 3] = rgba[i * 4+3];
ANDmaskIcon[i * 3 + 1] = rgba[i * 4 + 2];
ANDmaskIcon[i * 3 + 2] = rgba[i * 4 + 1];
XORmaskIcon[i] = 0xAA;// rgba[i * 4 + 3];
}
var hinstance = PInvoke.GetModuleHandle((string?)null);
HICON handle;
fixed (byte* and = ANDmaskIcon)
fixed (byte* xor = XORmaskIcon)
{
handle = PInvoke.CreateIcon(new HINSTANCE(hinstance.DangerousGetHandle()), 32, 32, 24, 1, xor, and);
}
ThrowIfInvalid(handle);
return new Icon(handle);
}
private static void ThrowIfInvalid(HICON handle)
{
if (handle.Value == IntPtr.Zero)
{
var ex = new Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
throw ex;
}
}
private static void ThrowIfInvalid(SafeHandle handle)
{
if (handle == null || handle.IsInvalid)
{
var ex = new Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
throw ex;
}
}
/// <summary>
/// For testing - Creates a simple Yang icon
/// </summary>
/// <returns>Icon with a Yang icon</returns>
public static Icon Yang()
{
var hinstance = PInvoke.GetModuleHandle((string?)null);
byte[] ANDmaskIcon = {0xFF, 0xFF, 0xFF, 0xFF, // line 1
0xFF, 0xFF, 0xC3, 0xFF, // line 2
0xFF, 0xFF, 0x00, 0xFF, // line 3
0xFF, 0xFE, 0x00, 0x7F, // line 4
0xFF, 0xFC, 0x00, 0x1F, // line 5
0xFF, 0xF8, 0x00, 0x0F, // line 6
0xFF, 0xF8, 0x00, 0x0F, // line 7
0xFF, 0xF0, 0x00, 0x07, // line 8
0xFF, 0xF0, 0x00, 0x03, // line 9
0xFF, 0xE0, 0x00, 0x03, // line 10
0xFF, 0xE0, 0x00, 0x01, // line 11
0xFF, 0xE0, 0x00, 0x01, // line 12
0xFF, 0xF0, 0x00, 0x01, // line 13
0xFF, 0xF0, 0x00, 0x00, // line 14
0xFF, 0xF8, 0x00, 0x00, // line 15
0xFF, 0xFC, 0x00, 0x00, // line 16
0xFF, 0xFF, 0x00, 0x00, // line 17
0xFF, 0xFF, 0x80, 0x00, // line 18
0xFF, 0xFF, 0xE0, 0x00, // line 19
0xFF, 0xFF, 0xE0, 0x01, // line 20
0xFF, 0xFF, 0xF0, 0x01, // line 21
0xFF, 0xFF, 0xF0, 0x01, // line 22
0xFF, 0xFF, 0xF0, 0x03, // line 23
0xFF, 0xFF, 0xE0, 0x03, // line 24
0xFF, 0xFF, 0xE0, 0x07, // line 25
0xFF, 0xFF, 0xC0, 0x0F, // line 26
0xFF, 0xFF, 0xC0, 0x0F, // line 27
0xFF, 0xFF, 0x80, 0x1F, // line 28
0xFF, 0xFF, 0x00, 0x7F, // line 29
0xFF, 0xFC, 0x00, 0xFF, // line 30
0xFF, 0xF8, 0x03, 0xFF, // line 31
0xFF, 0xFC, 0x3F, 0xFF}; // line 32
byte[] XORmaskIcon = {0x00, 0x00, 0x00, 0x00, // line 1
0x00, 0x00, 0x00, 0x00, // line 2
0x00, 0x00, 0x00, 0x00, // line 3
0x00, 0x00, 0x00, 0x00, // line 4
0x00, 0x00, 0x00, 0x00, // line 5
0x00, 0x00, 0x00, 0x00, // line 6
0x00, 0x00, 0x00, 0x00, // line 7
0x00, 0x00, 0x38, 0x00, // line 8
0x00, 0x00, 0x7C, 0x00, // line 9
0x00, 0x00, 0x7C, 0x00, // line 10
0x00, 0x00, 0x7C, 0x00, // line 11
0x00, 0x00, 0x38, 0x00, // line 12
0x00, 0x00, 0x00, 0x00, // line 13
0x00, 0x00, 0x00, 0x00, // line 14
0x00, 0x00, 0x00, 0x00, // line 15
0x00, 0x00, 0x00, 0x00, // line 16
0x00, 0x00, 0x00, 0x00, // line 17
0x00, 0x00, 0x00, 0x00, // line 18
0x00, 0x00, 0x00, 0x00, // line 19
0x00, 0x00, 0x00, 0x00, // line 20
0x00, 0x00, 0x00, 0x00, // line 21
0x00, 0x00, 0x00, 0x00, // line 22
0x00, 0x00, 0x00, 0x00, // line 23
0x00, 0x00, 0x00, 0x00, // line 24
0x00, 0x00, 0x00, 0x00, // line 25
0x00, 0x00, 0x00, 0x00, // line 26
0x00, 0x00, 0x00, 0x00, // line 27
0x00, 0x00, 0x00, 0x00, // line 28
0x00, 0x00, 0x00, 0x00, // line 29
0x00, 0x00, 0x00, 0x00, // line 30
0x00, 0x00, 0x00, 0x00, // line 31
0x00, 0x00, 0x00, 0x00}; // line 32
fixed (byte* and = ANDmaskIcon)
fixed (byte* xor = XORmaskIcon)
{
var icon = PInvoke.CreateIcon(new HINSTANCE(hinstance.DangerousGetHandle()), 32, 32, 1, 1, and, xor);
return new Icon(icon);
}
}
/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <inheritdoc />
~Icon()
{
Dispose(false);
}
/// <summary>
/// Disposes the icon
/// </summary>
/// <param name="disposing"></param>
protected void Dispose(bool disposing)
{
PInvoke.DestroyIcon(Handle); // also closes filehandle
}
}
}