-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppIconProvider.cs
More file actions
90 lines (79 loc) · 2.56 KB
/
Copy pathAppIconProvider.cs
File metadata and controls
90 lines (79 loc) · 2.56 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
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
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Runtime.InteropServices;
namespace PrimeDictate;
internal enum TrayVisualState
{
Ready,
Recording,
Processing,
Error
}
internal static class AppIconProvider
{
private const string IconFileName = "PrimeDictate.ico";
public static Icon LoadWindowIcon()
{
foreach (var candidate in EnumerateIconCandidates())
{
if (!File.Exists(candidate))
{
continue;
}
try
{
return new Icon(candidate);
}
catch (ArgumentException)
{
}
catch (IOException)
{
}
}
return SystemIcons.Application;
}
public static Icon CreateTrayIcon(TrayVisualState state)
{
var color = state switch
{
TrayVisualState.Ready => Color.FromArgb(34, 122, 255),
TrayVisualState.Recording => Color.FromArgb(220, 53, 69),
TrayVisualState.Processing => Color.FromArgb(32, 164, 112),
TrayVisualState.Error => Color.FromArgb(245, 184, 0),
_ => Color.FromArgb(34, 122, 255)
};
using var bitmap = new Bitmap(16, 16);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.Clear(Color.Transparent);
using var fillBrush = new SolidBrush(color);
using var borderPen = new Pen(Color.FromArgb(40, 40, 40), 1f);
using var centerBrush = new SolidBrush(Color.White);
graphics.FillEllipse(fillBrush, 1, 1, 14, 14);
graphics.DrawEllipse(borderPen, 1, 1, 14, 14);
graphics.FillEllipse(centerBrush, 6, 6, 4, 4);
}
var handle = bitmap.GetHicon();
try
{
using var unmanagedIcon = Icon.FromHandle(handle);
return (Icon)unmanagedIcon.Clone();
}
finally
{
_ = DestroyIcon(handle);
}
}
private static IEnumerable<string> EnumerateIconCandidates()
{
yield return Path.Combine(AppContext.BaseDirectory, IconFileName);
yield return Path.Combine(Directory.GetCurrentDirectory(), IconFileName);
yield return Path.Combine(Directory.GetCurrentDirectory(), "installer", "wix", "assets", IconFileName);
}
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DestroyIcon(IntPtr hIcon);
}