This repository was archived by the owner on Sep 29, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathSystemTrayIconWindow.cs
More file actions
84 lines (71 loc) · 2.2 KB
/
Copy pathSystemTrayIconWindow.cs
File metadata and controls
84 lines (71 loc) · 2.2 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
// Copyright (c) Files Community
// Licensed under the MIT License.
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
namespace AutoPowerTimeOut;
/// <summary>
/// Represents an icon window of Notification Area so-called System Tray.
/// <br/>
/// This is provided to handle context menu and retrieve mouse events, not a regular window class.
/// </summary>
public sealed partial class SystemTrayIconWindow : IDisposable
{
private SystemTrayIcon? _trayIcon;
private readonly WNDPROC _windowProcedure;
private HWND _windowHandle;
internal HWND WindowHandle => _windowHandle;
public unsafe SystemTrayIconWindow(SystemTrayIcon icon)
{
_windowProcedure = WindowProc;
_trayIcon = icon;
var text = "FilesTrayIcon_" + _trayIcon.Id;
fixed (char* ptr = text)
{
WNDCLASSEXW param = new()
{
cbSize = (uint)Marshal.SizeOf<WNDCLASSEXW>(),
style = WNDCLASS_STYLES.CS_DBLCLKS,
lpfnWndProc = _windowProcedure,
cbClsExtra = 0,
cbWndExtra = 0,
hInstance = PInvoke.GetModuleHandle(default(PCWSTR)),
lpszClassName = ptr
};
PInvoke.RegisterClassEx(in param);
}
_windowHandle = PInvoke.CreateWindowEx(
WINDOW_EX_STYLE.WS_EX_LEFT,
text,
string.Empty,
WINDOW_STYLE.WS_OVERLAPPED,
0,
0,
1,
1,
default,
null,
null,
null);
if (_windowHandle == default)
{
throw new Win32Exception("ERR: Message window handle was not a valid pointer.");
}
}
private LRESULT WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam)
{
return _trayIcon!.WindowProc(hWnd, uMsg, wParam, lParam);
}
public void Dispose()
{
if (_windowHandle != default)
{
PInvoke.DestroyWindow(_windowHandle);
_windowHandle = default;
}
_trayIcon = null;
}
}