|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | | -using System.Diagnostics.CodeAnalysis; |
4 | 3 | using System.Linq; |
5 | | -using System.Runtime.InteropServices; |
6 | 4 | using System.Text; |
7 | 5 | using System.Threading.Tasks; |
8 | | -using static FluentFlyouts.Flyouts.Helpers.Win32; |
| 6 | +using TerraFX.Interop.Windows; |
| 7 | +using static TerraFX.Interop.Windows.WM; |
| 8 | +using static TerraFX.Interop.Windows.Windows; |
9 | 9 |
|
10 | 10 | namespace FluentFlyouts.Flyouts |
11 | 11 | { |
12 | | - public partial class TrayIcon : IDisposable |
| 12 | + /* |
| 13 | + * TO-DO: Comment how the code works |
| 14 | + */ |
| 15 | + public unsafe partial class TrayIcon : IDisposable |
13 | 16 | { |
14 | | - private IntPtr windowHandle; |
15 | | - private IntPtr notifyIconHandle; |
16 | | - private NotifyIconData notifyIconData; |
17 | | - private WndProcDelegate wndProcDelegate; |
| 17 | + private HWND windowHandle; |
| 18 | + private HICON notifyIconHandle; |
| 19 | + private NOTIFYICONDATAW notifyIconData; |
18 | 20 |
|
19 | 21 | public event EventHandler LeftClicked; |
20 | 22 | public event EventHandler RightClicked; |
21 | 23 |
|
22 | | - private uint Id; // Used for callback to recieve clicked messages |
| 24 | + public uint Id; // Used for callback to get clicked messages |
23 | 25 |
|
24 | 26 | public TrayIcon(uint Id, string Icon, string ToolTip) |
25 | 27 | { |
26 | | - this.Id = Id; |
27 | | - wndProcDelegate = new WndProcDelegate(WndProc); |
28 | | - windowHandle = CreateWindow(Icon); |
29 | | - SetWndProc(); |
30 | | - notifyIconHandle = LoadIcon(Icon); |
31 | | - notifyIconData = new NotifyIconData |
| 28 | + unsafe |
32 | 29 | { |
33 | | - cbSize = (uint)Marshal.SizeOf<NotifyIconData>(), |
34 | | - hWnd = windowHandle, |
35 | | - uID = Id, |
36 | | - uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP, |
37 | | - uCallbackMessage = WM_APP + Id, |
38 | | - hIcon = notifyIconHandle, |
39 | | - szTip = ToolTip.PadRight(128, '\0') |
40 | | - }; |
| 30 | + this.Id = Id; |
| 31 | + windowHandle = CreateWindow(Icon); |
| 32 | + notifyIconHandle = LoadIcon(Icon); |
| 33 | + notifyIconData = new NOTIFYICONDATAW |
| 34 | + { |
| 35 | + cbSize = (uint)sizeof(NOTIFYICONDATAW), |
| 36 | + hWnd = windowHandle, |
| 37 | + uID = Id, |
| 38 | + uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP, |
| 39 | + uCallbackMessage = WM_APP + Id, |
| 40 | + hIcon = notifyIconHandle, |
| 41 | + szTip = GetSzTip(ToolTip) |
| 42 | + }; |
| 43 | + |
| 44 | + // Add the icon |
| 45 | + fixed (NOTIFYICONDATAW* pNotifyIconData = ¬ifyIconData) |
| 46 | + Shell_NotifyIcon(NIM_ADD, pNotifyIconData); |
41 | 47 |
|
42 | | - // Add the icon |
43 | | - Shell_NotifyIcon(NIM_ADD, ref notifyIconData); |
| 48 | + TrayIcon.AddIcon(Id, this); |
| 49 | + } |
44 | 50 | } |
45 | 51 |
|
46 | 52 | public void UpdateIcon(string Icon) |
47 | 53 | { |
48 | 54 | notifyIconHandle = LoadIcon(Icon); |
49 | 55 | notifyIconData.hIcon = notifyIconHandle; |
50 | | - |
51 | | - Shell_NotifyIcon(NIM_MODIFY, ref notifyIconData); |
| 56 | + fixed (NOTIFYICONDATAW* pNotifyIconData = ¬ifyIconData) |
| 57 | + Shell_NotifyIcon(NIM_MODIFY, pNotifyIconData); |
52 | 58 | } |
53 | 59 |
|
54 | 60 | public void UpdateTooltip(string ToolTip) |
55 | 61 | { |
56 | | - notifyIconData.szTip = ToolTip.PadRight(128, '\0'); |
57 | | - |
58 | | - Shell_NotifyIcon(NIM_MODIFY, ref notifyIconData); |
59 | | - } |
60 | | - |
61 | | - private void SetWndProc() |
62 | | - { |
63 | | - GCHandle.Alloc(wndProcDelegate); |
64 | | - SetWindowLong(windowHandle, 0, Marshal.GetFunctionPointerForDelegate(wndProcDelegate)); |
| 62 | + notifyIconData.szTip = GetSzTip(ToolTip); |
| 63 | + fixed (NOTIFYICONDATAW* pNotifyIconData = ¬ifyIconData) |
| 64 | + Shell_NotifyIcon(NIM_MODIFY, pNotifyIconData); |
65 | 65 | } |
66 | 66 |
|
67 | | - public void Dispose() |
| 67 | + ~TrayIcon() => Dispose(); |
| 68 | + public unsafe void Dispose() |
68 | 69 | { |
69 | | - Shell_NotifyIcon(NIM_DELETE, ref notifyIconData); |
70 | | - DestroyIcon(notifyIconHandle); |
71 | | - DestroyWindow(windowHandle); |
| 70 | + fixed (NOTIFYICONDATAW* pNotifyIconData = ¬ifyIconData) |
| 71 | + { |
| 72 | + TrayIcon.RemoveIcon(Id); |
| 73 | + Shell_NotifyIcon(NIM_DELETE, pNotifyIconData); |
| 74 | + DestroyIcon(notifyIconHandle); |
| 75 | + DestroyWindow(windowHandle); |
| 76 | + } |
72 | 77 | } |
73 | 78 | } |
74 | 79 | } |
0 commit comments