-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeekIcon.cs
101 lines (84 loc) · 3.8 KB
/
WeekIcon.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
#region Using statements
using System.Globalization;
using System.Runtime.Versioning;
#endregion Using statements
namespace WeekNumberLite2
{
internal static class WeekIcon
{
#region Icon Size
private const int _iconSize = (int)IconSize.Icon512;
#endregion Icon Size
#region Internal static functions
[SupportedOSPlatform("windows")]
internal static Icon GetIcon(int weekNumber)
{
Icon? icon = null;
using (Bitmap bitmap = new(_iconSize, _iconSize))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.TextContrast = 1;
DrawBackgroundOnGraphics(graphics, _iconSize);
DrawWeekNumberLite2OnGraphics(weekNumber, graphics, _iconSize);
IntPtr bHicon = bitmap.GetHicon();
Icon newIcon = Icon.FromHandle(bHicon);
icon = new Icon(newIcon, _iconSize, _iconSize);
CleanupIcon(ref newIcon);
}
return icon;
}
[SupportedOSPlatform("windows")]
internal static void CleanupIcon(ref Icon icon)
{
if (icon is null)
{
return;
}
_ = NativeMethods.DestroyIcon(icon.Handle);
icon.Dispose();
}
#endregion Internal static functions
#region Privare static helper methods
[SupportedOSPlatform("windows")]
private static void DrawBackgroundOnGraphics(Graphics graphics, int size = 0)
{
if (size == 0)
{
size = _iconSize;
}
Color backgroundColor = Color.Black;
Color foregroundColor = Color.LightGray;
using SolidBrush foregroundBrush = new(foregroundColor);
using SolidBrush backgroundBrush = new(backgroundColor);
float inset = (float)Math.Abs(size * .03125);
graphics?.FillRectangle(backgroundBrush, inset, inset, size - inset, size - inset);
using (Pen pen = new(foregroundColor, inset * 2))
{
graphics?.DrawRectangle(pen, inset, inset, size - (inset * 2), size - (inset * 2));
}
float leftInset = (float)Math.Abs(size * .15625);
graphics?.FillRectangle(foregroundBrush, leftInset, inset / 2, inset * 3, inset * 5);
float rightInset = (float)Math.Abs(size * .75);
graphics?.FillRectangle(foregroundBrush, rightInset, inset / 2, inset * 3, inset * 5);
}
[SupportedOSPlatform("windows")]
private static void DrawWeekNumberLite2OnGraphics(int WeekNumberLite2, Graphics graphics, int size = 0)
{
if (size == 0)
{
size = _iconSize;
}
float fontSize = (float)Math.Abs(size * .78125);
float insetX = (float)-(size > (int)IconSize.Icon16 ? Math.Abs(fontSize * .12) : Math.Abs(fontSize * .07));
float insetY = (float)(size > (int)IconSize.Icon16 ? Math.Abs(fontSize * .2) : Math.Abs(fontSize * .08));
Color foregroundColor = Color.White;
using Font font = new(FontFamily.GenericMonospace, fontSize, FontStyle.Bold, GraphicsUnit.Pixel, 0, false);
using Brush brush = new SolidBrush(foregroundColor);
graphics?.DrawString(WeekNumberLite2.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0'), font, brush, insetX, insetY);
}
#endregion Private static helper methods
}
}