This repository was archived by the owner on Sep 29, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemTrayIcon.cs
More file actions
273 lines (225 loc) · 7.12 KB
/
Copy pathSystemTrayIcon.cs
File metadata and controls
273 lines (225 loc) · 7.12 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
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
// Copyright (c) Files Community
// Licensed under the MIT License.
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using Windows.Foundation;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.Shell;
using Windows.Win32.UI.WindowsAndMessaging;
using RoutedEventArgs = System.Windows.RoutedEventArgs;
using RoutedEventHandler = System.Windows.RoutedEventHandler;
namespace AutoPowerTimeOut;
/// <summary>
/// Represents a tray icon of Notification Area so-called System Tray.
/// </summary>
public sealed partial class SystemTrayIcon : IDisposable
{
// Constants
private const uint WM_FILES_UNIQUE_MESSAGE = 2048u;
// Fields
private static readonly Guid _trayIconGuid = new("7B44A6B1-AE8E-4857-975A-E5C9B0C10626");
private readonly SystemTrayIconWindow _IconWindow;
private readonly uint _taskbarRestartMessageId;
private bool _notifyIconCreated;
// Properties
public Guid Id { get; private set; }
private bool _IsVisible;
public bool IsVisible
{
get
{
return _IsVisible;
}
private set
{
if (_IsVisible != value)
{
_IsVisible = value;
if (!value)
{
DeleteNotifyIcon();
}
else
{
CreateOrModifyNotifyIcon();
}
}
}
}
private string _Tooltip = string.Empty;
public string Tooltip
{
get
{
return _Tooltip;
}
set
{
if (_Tooltip != value)
{
_Tooltip = value;
CreateOrModifyNotifyIcon();
}
}
}
private Icon _Icon = null!;
public Icon Icon
{
get
{
return _Icon;
}
set
{
if (_Icon != value)
{
_Icon = value;
CreateOrModifyNotifyIcon();
}
}
}
public Rect Position
{
get
{
if (!IsVisible)
{
return default;
}
NOTIFYICONIDENTIFIER identifier = default;
identifier.cbSize = (uint)Marshal.SizeOf<NOTIFYICONIDENTIFIER>();
identifier.hWnd = _IconWindow.WindowHandle;
identifier.guidItem = Id;
// Get RECT
PInvoke.Shell_NotifyIconGetRect(in identifier, out var _IconLocation);
return new Rect(
_IconLocation.left,
_IconLocation.top,
_IconLocation.right - _IconLocation.left,
_IconLocation.bottom - _IconLocation.top);
}
}
public event RoutedEventHandler? LeftClick;
public event RoutedEventHandler? RightClick;
// Constructor
/// <summary>
/// Initializes an instance of <see cref="SystemTrayIcon"/>.
/// </summary>
/// <remarks>
/// Note that initializing an instance won't make the icon visible.
/// </remarks>
public SystemTrayIcon()
{
_taskbarRestartMessageId = PInvoke.RegisterWindowMessage("TaskbarCreated");
Id = _trayIconGuid;
_IconWindow = new SystemTrayIconWindow(this);
CreateOrModifyNotifyIcon();
}
// Public Methods
/// <summary>
/// Shows the tray icon.
/// </summary>
public SystemTrayIcon Show()
{
IsVisible = true;
return this;
}
/// <summary>
/// Hides the tray icon.
/// </summary>
public SystemTrayIcon Hide()
{
IsVisible = false;
return this;
}
// Private Methods
private void CreateOrModifyNotifyIcon()
{
if (IsVisible)
{
NOTIFYICONDATAW lpData = default;
lpData.cbSize = (uint)Marshal.SizeOf<NOTIFYICONDATAW>();
lpData.hWnd = _IconWindow.WindowHandle;
lpData.uCallbackMessage = WM_FILES_UNIQUE_MESSAGE;
lpData.hIcon = (Icon != null) ? new HICON(Icon.Handle) : default;
lpData.guidItem = Id;
lpData.uFlags = NOTIFY_ICON_DATA_FLAGS.NIF_MESSAGE | NOTIFY_ICON_DATA_FLAGS.NIF_ICON | NOTIFY_ICON_DATA_FLAGS.NIF_TIP | NOTIFY_ICON_DATA_FLAGS.NIF_GUID | NOTIFY_ICON_DATA_FLAGS.NIF_SHOWTIP;
lpData.szTip = _Tooltip ?? string.Empty;
if (!_notifyIconCreated)
{
// Delete the existing icon
PInvoke.Shell_NotifyIcon(NOTIFY_ICON_MESSAGE.NIM_DELETE, in lpData);
_notifyIconCreated = true;
// Add a new icon
PInvoke.Shell_NotifyIcon(NOTIFY_ICON_MESSAGE.NIM_ADD, in lpData);
lpData.Anonymous.uVersion = 4u;
// Set the icon handler version
// NOTE: Do not omit this code. If you remove, the icon won't be shown.
PInvoke.Shell_NotifyIcon(NOTIFY_ICON_MESSAGE.NIM_SETVERSION, in lpData);
}
else
{
// Modify the existing icon
PInvoke.Shell_NotifyIcon(NOTIFY_ICON_MESSAGE.NIM_MODIFY, in lpData);
}
}
}
private void DeleteNotifyIcon()
{
if (_notifyIconCreated)
{
_notifyIconCreated = false;
NOTIFYICONDATAW lpData = default;
lpData.cbSize = (uint)Marshal.SizeOf<NOTIFYICONDATAW>();
lpData.hWnd = _IconWindow.WindowHandle;
lpData.guidItem = Id;
lpData.uFlags = NOTIFY_ICON_DATA_FLAGS.NIF_MESSAGE | NOTIFY_ICON_DATA_FLAGS.NIF_ICON | NOTIFY_ICON_DATA_FLAGS.NIF_TIP | NOTIFY_ICON_DATA_FLAGS.NIF_GUID | NOTIFY_ICON_DATA_FLAGS.NIF_SHOWTIP;
// Delete the existing icon
PInvoke.Shell_NotifyIcon(NOTIFY_ICON_MESSAGE.NIM_DELETE, in lpData);
}
}
internal LRESULT WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_FILES_UNIQUE_MESSAGE:
{
switch ((uint)(lParam.Value & 0xFFFF))
{
case PInvoke.WM_LBUTTONUP:
{
LeftClick?.Invoke(this, new RoutedEventArgs());
break;
}
case PInvoke.WM_RBUTTONUP:
{
RightClick?.Invoke(this, new RoutedEventArgs());
break;
}
}
break;
}
case PInvoke.WM_DESTROY:
{
DeleteNotifyIcon();
break;
}
default:
{
if (uMsg == _taskbarRestartMessageId)
{
DeleteNotifyIcon();
CreateOrModifyNotifyIcon();
}
return PInvoke.DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
return default;
}
public void Dispose()
{
_IconWindow.Dispose();
}
}