-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTaskbarGui.cs
299 lines (243 loc) · 10.1 KB
/
TaskbarGui.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#region Using statements
using Microsoft.Win32;
using System;
using System.Threading;
using System.Windows.Forms;
#region Test code
/*
using System.Reflection;
using System.Runtime.InteropServices;
using System.Drawing;
*/
#endregion Test code
#endregion Using statements
namespace WeekNumber
{
internal class TaskbarGui : IDisposable, IGui
{
#region Public event handler (update icon request)
public event EventHandler UpdateRequest;
#endregion Public event handler (update icon request)
#region Private variables
private NotifyIcon _notifyIcon;
private readonly WeekNumberContextMenu _contextMenu;
private int _latestWeek;
#endregion Private variables
#region Constructor
internal TaskbarGui(int week, int iconResolution = (int)IconSize.Icon256)
{
Log.LogCaller();
_latestWeek = week;
_contextMenu = new WeekNumberContextMenu();
_notifyIcon = GetNotifyIcon(_contextMenu.ContextMenu);
UpdateIcon(week, ref _notifyIcon, iconResolution);
_notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
#region Test code
/* test code
Rectangle rect =NotifyIconHelper.GetIconRect(_notifyIcon);*/
#endregion Test code
if (Settings.SettingIsValue(Resources.DisplayStartupNotification, "True"))
{
DisplayUserInfoBalloonTip($"{_notifyIcon.Text}\r\n{Resources.StartupMessageText}");
}
_contextMenu.SettingsChangedHandler += OnSettingsChange;
}
#endregion Constructor
#region Display NotifyIcon BalloonTip
private void DisplayUserInfoBalloonTip(string message)
{
Log.LogCaller();
bool siletMsg = Settings.SettingIsValue(Resources.UseSilentNotifications, "True");
object currentSound = null;
if (siletMsg)
{
using (RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings", true))
{
currentSound = regKey.GetValue("NOC_GLOBAL_SETTING_ALLOW_NOTIFICATION_SOUND");
regKey.SetValue("NOC_GLOBAL_SETTING_ALLOW_NOTIFICATION_SOUND", 0);
regKey.Flush();
regKey.Close();
}
}
_notifyIcon.ShowBalloonTip(10000, Message.CAPTION, message, ToolTipIcon.None);
if (siletMsg)
{
using (RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings", true))
{
System.Threading.Thread.Sleep(1000);
if (currentSound is null)
{
regKey.DeleteValue("NOC_GLOBAL_SETTING_ALLOW_NOTIFICATION_SOUND");
}
else
{
regKey.SetValue("NOC_GLOBAL_SETTING_ALLOW_NOTIFICATION_SOUND", currentSound);
}
regKey.Flush();
regKey.Close();
}
}
}
#endregion Display NotifyIcon BalloonTip
#region Private event handlers
private void OnSettingsChange(object sender, EventArgs e)
{
UpdateRequest?.Invoke(null, null);
}
private void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
Forms.DateForm.Display();
}
#endregion Private event handlers
#region Public UpdateIcon method
/// <summary>
/// Updates icon on GUI with given week number
/// </summary>
/// <param name="weekNumber">The week number to display on icon</param>
/// <param name="iconResolution">The width and height of the icon</param>
/// <param name="redrawContextMenu">Redraw context menu</param>
public void UpdateIcon(int weekNumber, int iconResolution = (int)IconSize.Icon256, bool redrawContextMenu = false)
{
UpdateIcon(weekNumber, ref _notifyIcon, iconResolution);
if (redrawContextMenu)
{
_contextMenu.CreateContextMenu();
_notifyIcon.ContextMenu = _contextMenu.ContextMenu;
}
}
#endregion Public UpdateIcon method
#region Private UpdateIcon method
private void UpdateIcon(int weekNumber, ref NotifyIcon notifyIcon, int iconResolution)
{
Log.LogCaller();
try
{
string weekDayPrefix = string.Empty;
string longDateString = DateTime.Now.ToLongDateString();
const string SWEDISH_LONG_DATE_PREFIX_STRING = "den ";
if (Thread.CurrentThread.CurrentUICulture.Name == Resources.Swedish || longDateString.StartsWith(SWEDISH_LONG_DATE_PREFIX_STRING))
{
weekDayPrefix = Message.SWEDISH_DAY_OF_WEEK_PREFIX[(int)DateTime.Now.DayOfWeek];
}
notifyIcon.Text = $"{Resources.Week} {weekNumber}\r\n{weekDayPrefix}{longDateString}";
System.Drawing.Icon prevIcon = notifyIcon.Icon;
notifyIcon.Icon = WeekIcon.GetIcon(weekNumber, iconResolution);
WeekIcon.CleanupIcon(ref prevIcon);
}
finally
{
if (_latestWeek != weekNumber)
{
if (Settings.SettingIsValue(Resources.DisplayWeekChangedNotification, "True"))
{
DisplayUserInfoBalloonTip(notifyIcon.Text);
}
_latestWeek = weekNumber;
}
}
}
#endregion Private UpdateIcon method
#region Private helper property to create NotifyIcon
private static NotifyIcon GetNotifyIcon(ContextMenu contextMenu)
{
return new NotifyIcon { Visible = true, ContextMenu = contextMenu };
}
#endregion Private helper property to create NotifyIcon
#region IDisposable methods
/// <summary>
/// Disposes the GUI resources
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing)
{
return;
}
CleanupNotifyIcon();
_contextMenu.Dispose();
}
private void CleanupNotifyIcon()
{
if (_notifyIcon != null)
{
_notifyIcon.Visible = false;
if (_notifyIcon.Icon != null)
{
NativeMethods.DestroyIcon(_notifyIcon.Icon.Handle);
_notifyIcon.Icon?.Dispose();
}
_notifyIcon.ContextMenu?.MenuItems.Clear();
_notifyIcon.ContextMenu?.Dispose();
_notifyIcon.Dispose();
_notifyIcon = null;
}
}
#endregion IDisposable methods
}
/* Use this to clear icon area instead, inspiration code only, need modification, but with area of icon then maybe move mouse over it programmatically instead of current more complex solution
sealed class NotifyIconHelper
{
public static Rectangle GetIconRect(NotifyIcon icon)
{
RECT rect = new RECT();
NOTIFYICONIDENTIFIER notifyIcon = new NOTIFYICONIDENTIFIER();
notifyIcon.cbSize = Marshal.SizeOf(notifyIcon);
//use hWnd and id of NotifyIcon instead of guid is needed
notifyIcon.hWnd = GetHandle(icon);
notifyIcon.uID = GetId(icon);
int hresult = Shell_NotifyIconGetRect(ref notifyIcon, out rect);
//rect now has the position and size of icon
return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
}
private static int GetIconID(NotifyIcon icon)
{
RECT rect = new RECT();
NOTIFYICONIDENTIFIER notifyIcon = new NOTIFYICONIDENTIFIER();
notifyIcon.cbSize = Marshal.SizeOf(notifyIcon);
//use hWnd and id of NotifyIcon instead of guid is needed
notifyIcon.hWnd = GetHandle(icon);
notifyIcon.uID = GetId(icon);
int hresult = Shell_NotifyIconGetRect(ref notifyIcon, out rect);
//rect now has the position and size of icon
return notifyIcon.uID;
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public Int32 left;
public Int32 top;
public Int32 right;
public Int32 bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct NOTIFYICONIDENTIFIER
{
public Int32 cbSize;
public IntPtr hWnd;
public Int32 uID;
public Guid guidItem;
}
[DllImport("shell32.dll", SetLastError = true)]
private static extern int Shell_NotifyIconGetRect([In] ref NOTIFYICONIDENTIFIER identifier, [Out] out RECT iconLocation);
private static FieldInfo windowField = typeof(NotifyIcon).GetField("window", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
private static IntPtr GetHandle(NotifyIcon icon)
{
if (windowField == null) throw new InvalidOperationException("[Useful error message]");
NativeWindow window = windowField.GetValue(icon) as NativeWindow;
if (window == null) throw new InvalidOperationException("[Useful error message]"); // should not happen?
return window.Handle;
}
private static FieldInfo idField = typeof(NotifyIcon).GetField("id", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
private static int GetId(NotifyIcon icon)
{
if (idField == null) throw new InvalidOperationException("[Useful error message]");
return (int)idField.GetValue(icon);
}
}
*/
}