-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWeekIcon.cs
203 lines (180 loc) · 8.75 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
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
#region Using statements
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Forms;
#endregion Using statements
namespace WeekNumber
{
internal static class WeekIcon
{
#region Icon Size
private const int _defaultSize = (int)IconSize.Icon256;
private const int _saveSize = (int)IconSize.Icon256;
#endregion Icon Size
#region Internal static functions
internal static int GetIconResolution(bool forceUpdate = false)
{
int iconResolution = Settings.GetIntSetting(Resources.IconResolution, -1);
double myDbl = 1.0d;
if (forceUpdate || iconResolution == -1)
{
// guess what icon resolution to use based on system
double winZoomLvl = GetWindowsZoom();
bool usingSmallTaskbarIcons = TaskbarUtil.UsingSmallTaskbarButtons();
myDbl = (double)System.Windows.SystemParameters.SmallIconHeight * winZoomLvl;
if (!usingSmallTaskbarIcons) myDbl *= 1.5d;
if (System.Windows.SystemParameters.PrimaryScreenWidth > 1600) myDbl *= 2.0d;
Log.Info = $"SmallIconHeight={System.Windows.SystemParameters.SmallIconHeight}";
Log.Info = $"WindowsZoomLevel={winZoomLvl * 100}";
Log.Info = $"UsingSmallTaskbarButtons={usingSmallTaskbarIcons}";
Log.Info = $"Guessed icon resolution={myDbl}x{myDbl}";
// find closes match to existing configs (do not allow 16, 20, 24, 32, 40, 48, 64, 128)
List<int> list = new List<int> { 256, 512 };
int closest = list.Aggregate((x, y) => Math.Abs(x - myDbl) < Math.Abs(y - myDbl) ? x : y);
Log.Info = $"Closest icon resolution={closest}x{closest}";
Settings.UpdateSetting(Resources.IconResolution, closest.ToString());
iconResolution = closest;
}
return iconResolution;
}
internal static Bitmap GetImage(int weekNumber)
{
Log.LogCaller();
int size = 256;
using (Bitmap bitmap = new Bitmap(size, size))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
try
{
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, size);
DrawWeekNumberOnGraphics(weekNumber, graphics, size);
return bitmap.Clone(new RectangleF(0, 0, size, size), System.Drawing.Imaging.PixelFormat.DontCare);
}
finally
{
graphics.Dispose();
bitmap?.Dispose();
}
}
}
internal static Icon GetIcon(int weekNumber, int size = 0)
{
Log.LogCaller();
if (size == 0)
{
Log.Info = $"Using default icon size {_defaultSize}x{_defaultSize}";
size = _defaultSize;
}
Icon icon = null;
using (Bitmap bitmap = new Bitmap(size, size))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.SmoothingMode = (System.Drawing.Drawing2D.SmoothingMode)Settings.GetIntSetting(Resources.SmoothingMode, (int)System.Drawing.Drawing2D.SmoothingMode.HighQuality);
graphics.CompositingQuality = (System.Drawing.Drawing2D.CompositingQuality)Settings.GetIntSetting(Resources.CompositingQuality, (int)System.Drawing.Drawing2D.CompositingQuality.HighQuality);
graphics.InterpolationMode = (System.Drawing.Drawing2D.InterpolationMode)Settings.GetIntSetting(Resources.InterpolationMode, (int)System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic);
graphics.TextContrast = Settings.GetIntSetting(Resources.TextContrast, 1);
DrawBackgroundOnGraphics(graphics, size);
DrawWeekNumberOnGraphics(weekNumber, graphics, size);
IntPtr bHicon = bitmap.GetHicon();
Icon newIcon = Icon.FromHandle(bHicon);
icon = new Icon(newIcon, size, size);
CleanupIcon(ref newIcon);
}
return icon;
}
internal static void CleanupIcon(ref Icon icon)
{
if (icon is null)
{
return;
}
NativeMethods.DestroyIcon(icon.Handle);
icon.Dispose();
}
internal static bool SaveIcon(int weekNumber, string fullPath)
{
bool result = true;
Icon icon = null;
try
{
Log.LogCaller();
icon = GetIcon(weekNumber, _saveSize);
using (FileStream fs = new FileStream(fullPath, FileMode.Create,
FileAccess.Write, FileShare.None))
{
icon.Save(fs);
Log.Info = $"Save app icon as '{fullPath}'";
}
}
catch (System.Exception ex)
{
Message.Show(Resources.UnhandledException, ex);
result = false;
}
finally
{
CleanupIcon(ref icon);
}
return result;
}
#endregion Internal static functions
#region Privare static helper methods
private static void DrawBackgroundOnGraphics(Graphics graphics, int size = 0)
{
if (size == 0)
{
size = _defaultSize;
}
Color backgroundColor = Color.FromArgb(Settings.GetIntSetting(Resources.IconBackgroundAlpha),
Settings.GetIntSetting(Resources.IconBackgroundRed),
Settings.GetIntSetting(Resources.IconBackgroundGreen),
Settings.GetIntSetting(Resources.IconBackgroundBlue));
Color foregroundColor = Color.FromArgb(
Settings.GetIntSetting(Resources.IconForegroundRed, 255),
Settings.GetIntSetting(Resources.IconForegroundGreen, 255),
Settings.GetIntSetting(Resources.IconForegroundBlue, 255));
using (SolidBrush foregroundBrush = new SolidBrush(foregroundColor))
using (SolidBrush backgroundBrush = new SolidBrush(backgroundColor))
{
float inset = (float)System.Math.Abs(size * .03125);
graphics?.FillRectangle(backgroundBrush, inset, inset, size - inset, size - inset);
using (Pen pen = new Pen(foregroundColor, inset * 2))
{
graphics?.DrawRectangle(pen, inset, inset, size - inset * 2, size - inset * 2);
}
float leftInset = (float)System.Math.Abs(size * .15625);
graphics?.FillRectangle(foregroundBrush, leftInset, inset / 2, inset * 3, inset * 5);
float rightInset = (float)System.Math.Abs(size * .75);
graphics?.FillRectangle(foregroundBrush, rightInset, inset / 2, inset * 3, inset * 5);
}
}
private static void DrawWeekNumberOnGraphics(int weekNumber, Graphics graphics, int size = 0)
{
if (size == 0) size = _defaultSize;
float fontSize = (float)System.Math.Abs(size * .78125);
// float insetX = (float)-(size > (int)IconSize.Icon16 ? System.Math.Abs(fontSize * .12) : System.Math.Abs(fontSize * .07));
float insetY = (float)(size > (int)IconSize.Icon16 ? System.Math.Abs(fontSize * .2) : System.Math.Abs(fontSize * .08));
Color foregroundColor = Color.FromArgb(
Settings.GetIntSetting(Resources.IconForegroundRed),
Settings.GetIntSetting(Resources.IconForegroundGreen),
Settings.GetIntSetting(Resources.IconForegroundBlue));
using (Font font = new Font("Aurulent Sans Mono", fontSize * 0.8f, FontStyle.Regular,
GraphicsUnit.Pixel, 0, false))
using (Brush brush = new SolidBrush(foregroundColor))
graphics?.DrawString(weekNumber.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0'), font, brush, 0.0f, insetY);
}
private static double GetWindowsZoom()
{
return (double)(Screen.PrimaryScreen.WorkingArea.Width / System.Windows.SystemParameters.PrimaryScreenWidth);
}
#endregion Private static helper methods
}
}