-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTrayIcon.cs
More file actions
179 lines (145 loc) · 5.97 KB
/
TrayIcon.cs
File metadata and controls
179 lines (145 loc) · 5.97 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
using System.Runtime.InteropServices;
using Microsoft.UI.Xaml;
using WinRT.Interop;
namespace NoteUI;
internal sealed class TrayIcon : IDisposable
{
private const int GWL_WNDPROC = -4;
private const int WM_APP = 0x8000;
private const int WM_TRAYICON = WM_APP + 1;
private const int WM_LBUTTONDBLCLK = 0x0203;
private const int WM_RBUTTONUP = 0x0205;
private const int NIM_ADD = 0x00;
private const int NIM_DELETE = 0x02;
private const int NIF_MESSAGE = 0x01;
private const int NIF_ICON = 0x02;
private const int NIF_TIP = 0x04;
private const uint TPM_RETURNCMD = 0x0100;
private const uint TPM_NONOTIFY = 0x0080;
private const uint MF_STRING = 0x0000;
private const uint MF_SEPARATOR = 0x0800;
private readonly IntPtr _hwnd;
private readonly IntPtr _icon;
private readonly WndProcDelegate _wndProcDelegate;
private readonly IntPtr _prevWndProc;
private bool _disposed;
public event Action? ShowRequested;
public event Action? ExitRequested;
private delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
public TrayIcon(Window window, string tooltip)
{
_hwnd = WindowNative.GetWindowHandle(window);
// Load icon from exe resource (embedded via ApplicationIcon)
var exePath = Environment.ProcessPath ?? "";
_icon = ExtractIcon(IntPtr.Zero, exePath, 0);
// Subclass window to receive tray messages
_wndProcDelegate = WndProc;
_prevWndProc = SetWindowLongPtr(_hwnd, GWL_WNDPROC,
Marshal.GetFunctionPointerForDelegate(_wndProcDelegate));
var nid = new NOTIFYICONDATA
{
cbSize = Marshal.SizeOf<NOTIFYICONDATA>(),
hWnd = _hwnd,
uID = 1,
uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP,
uCallbackMessage = WM_TRAYICON,
hIcon = _icon,
szTip = tooltip,
szInfo = "",
szInfoTitle = ""
};
Shell_NotifyIcon(NIM_ADD, ref nid);
}
private IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
if (msg == WM_TRAYICON)
{
var mouseMsg = (int)lParam;
if (mouseMsg == WM_LBUTTONDBLCLK)
ShowRequested?.Invoke();
else if (mouseMsg == WM_RBUTTONUP)
ShowContextMenu();
return IntPtr.Zero;
}
return CallWindowProc(_prevWndProc, hWnd, msg, wParam, lParam);
}
private void ShowContextMenu()
{
var menu = CreatePopupMenu();
AppendMenu(menu, MF_STRING, 1, "Notes");
AppendMenu(menu, MF_SEPARATOR, 0, null);
AppendMenu(menu, MF_STRING, 2, "Quitter");
GetCursorPos(out var pt);
SetForegroundWindow(_hwnd);
var cmd = TrackPopupMenuEx(menu, TPM_RETURNCMD | TPM_NONOTIFY,
pt.X, pt.Y, _hwnd, IntPtr.Zero);
DestroyMenu(menu);
if (cmd == 1) ShowRequested?.Invoke();
else if (cmd == 2) ExitRequested?.Invoke();
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
var nid = new NOTIFYICONDATA
{
cbSize = Marshal.SizeOf<NOTIFYICONDATA>(),
hWnd = _hwnd,
uID = 1,
szTip = "",
szInfo = "",
szInfoTitle = ""
};
Shell_NotifyIcon(NIM_DELETE, ref nid);
if (_icon != IntPtr.Zero)
DestroyIcon(_icon);
if (_prevWndProc != IntPtr.Zero)
SetWindowLongPtr(_hwnd, GWL_WNDPROC, _prevWndProc);
}
// ── Structures ──────────────────────────────────────────────
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct NOTIFYICONDATA
{
public int cbSize;
public IntPtr hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szTip;
public int dwState;
public int dwStateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szInfo;
public int uTimeoutOrVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string szInfoTitle;
public int dwInfoFlags;
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT { public int X, Y; }
// ── P/Invoke ────────────────────────────────────────────────
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern bool Shell_NotifyIcon(int dwMessage, ref NOTIFYICONDATA lpData);
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);
[DllImport("user32.dll")]
private static extern bool DestroyIcon(IntPtr hIcon);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll")]
private static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern IntPtr CreatePopupMenu();
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern bool AppendMenu(IntPtr hMenu, uint uFlags, nuint uIDNewItem, string? lpNewItem);
[DllImport("user32.dll")]
private static extern int TrackPopupMenuEx(IntPtr hMenu, uint fuFlags, int x, int y, IntPtr hwnd, IntPtr lptpm);
[DllImport("user32.dll")]
private static extern bool DestroyMenu(IntPtr hMenu);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool GetCursorPos(out POINT lpPoint);
}