Hauptknopf, Laeufer neu starten, Not-Aus, Tische nach vorn/anordnen, Streamer-Modus (Maus), ausblenden, Pille/Leiste, linker/rechter Rand, Einstellungen, beenden. Dieselbe Liste fuellt den neuen Knopf in der Titelleiste (MenuFlyout) und das Rechtsklick-Menue am Tray-Symbol. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
158 lines
6.7 KiB
C#
158 lines
6.7 KiB
C#
using System.Runtime.InteropServices;
|
|
|
|
namespace Pokerfreak.Cockpit.Kern;
|
|
|
|
/// <summary>
|
|
/// Das Tray-Symbol (wie cockpit/src/schale.rs): Minimieren versteckt das Cockpit ins Tray statt in die
|
|
/// Taskleiste; Klick aufs Symbol holt es zurueck, Rechtsklick zeigt das Cockpit-Menue (`Punkte`, wie die
|
|
/// Menueleiste am Mac: Laeufer neu starten, Not-Aus, Tische ...), ohne Punkte nur "Cockpit zeigen" und "Beenden".
|
|
/// Die Nachrichten kommen ueber eine Unterklasse des Hauptfensters (SetWindowSubclass).
|
|
/// </summary>
|
|
public sealed class Schale : IDisposable
|
|
{
|
|
private const uint WmTray = 0x8000 + 41; // WM_APP + 41
|
|
private const int MenueZeigen = 1, MenueBeenden = 2;
|
|
|
|
private readonly IntPtr _hwnd;
|
|
private readonly SubclassProc _proc; // haelt den Delegaten am Leben
|
|
private readonly Action _zeigen, _beenden;
|
|
private IntPtr _icon;
|
|
private bool _da;
|
|
|
|
/// <summary>Die Eintraege des Rechtsklick-Menues, beim Oeffnen frisch aus dem Zustand gebaut.</summary>
|
|
public Func<IReadOnlyList<MenuePunkt>>? Punkte { get; set; }
|
|
|
|
public Schale(IntPtr hwnd, string icoPfad, string tipp, Action zeigen, Action beenden)
|
|
{
|
|
_hwnd = hwnd;
|
|
_zeigen = zeigen;
|
|
_beenden = beenden;
|
|
_proc = Nachricht;
|
|
SetWindowSubclass(hwnd, _proc, (UIntPtr)41, UIntPtr.Zero);
|
|
_icon = LoadImageW(IntPtr.Zero, icoPfad, 1 /* IMAGE_ICON */, 16, 16, 0x10 /* LR_LOADFROMFILE */);
|
|
var d = Daten();
|
|
d.uFlags = 0x1 | 0x2 | 0x4; // NIF_MESSAGE | NIF_ICON | NIF_TIP
|
|
d.uCallbackMessage = WmTray;
|
|
d.hIcon = _icon;
|
|
d.szTip = tipp;
|
|
_da = Shell_NotifyIconW(0 /* NIM_ADD */, ref d);
|
|
}
|
|
|
|
/// <summary>Den Hinweistext am Symbol aendern (z. B. Laeufer-Zustand).</summary>
|
|
public void Tipp(string text)
|
|
{
|
|
if (!_da) return;
|
|
var d = Daten();
|
|
d.uFlags = 0x4;
|
|
d.szTip = text.Length > 127 ? text[..127] : text;
|
|
Shell_NotifyIconW(1 /* NIM_MODIFY */, ref d);
|
|
}
|
|
|
|
private NOTIFYICONDATAW Daten() => new()
|
|
{
|
|
cbSize = (uint)Marshal.SizeOf<NOTIFYICONDATAW>(),
|
|
hWnd = _hwnd,
|
|
uID = 1,
|
|
szTip = "",
|
|
szInfo = "",
|
|
szInfoTitle = "",
|
|
};
|
|
|
|
private IntPtr Nachricht(IntPtr h, uint msg, IntPtr wp, IntPtr lp, UIntPtr id, UIntPtr daten)
|
|
{
|
|
if (msg == WmTray)
|
|
{
|
|
switch ((uint)(lp.ToInt64() & 0xFFFF))
|
|
{
|
|
case 0x202: // WM_LBUTTONUP
|
|
_zeigen();
|
|
break;
|
|
case 0x205: // WM_RBUTTONUP
|
|
Menue();
|
|
break;
|
|
}
|
|
return IntPtr.Zero;
|
|
}
|
|
return DefSubclassProc(h, msg, wp, lp);
|
|
}
|
|
|
|
private const int ErsterPunkt = 100;
|
|
|
|
private void Menue()
|
|
{
|
|
var m = CreatePopupMenu();
|
|
var punkte = Punkte?.Invoke() ?? Array.Empty<MenuePunkt>();
|
|
if (punkte.Count == 0)
|
|
{
|
|
AppendMenuW(m, 0, (UIntPtr)MenueZeigen, "Cockpit zeigen");
|
|
AppendMenuW(m, 0, (UIntPtr)MenueBeenden, "Beenden");
|
|
}
|
|
for (int i = 0; i < punkte.Count; i++)
|
|
{
|
|
var pk = punkte[i];
|
|
uint flags = pk.IstTrenner ? 0x800u // MF_SEPARATOR
|
|
: (pk.An && pk.Tun != null ? 0u : 0x1u) // MF_GRAYED
|
|
| (pk.Haken ? 0x8u : 0u); // MF_CHECKED
|
|
AppendMenuW(m, flags, (UIntPtr)(ErsterPunkt + i), pk.Text ?? "");
|
|
}
|
|
GetCursorPos(out var p);
|
|
SetForegroundWindow(_hwnd);
|
|
var wahl = TrackPopupMenu(m, 0x2 | 0x100 | 0x80 /* RIGHTBUTTON|RETURNCMD|NONOTIFY */, p.X, p.Y, 0, _hwnd, IntPtr.Zero);
|
|
PostMessageW(_hwnd, 0, IntPtr.Zero, IntPtr.Zero);
|
|
DestroyMenu(m);
|
|
if (wahl == MenueZeigen) _zeigen();
|
|
else if (wahl == MenueBeenden) _beenden();
|
|
else if (wahl >= ErsterPunkt && wahl - ErsterPunkt < punkte.Count
|
|
&& punkte[wahl - ErsterPunkt] is { An: true, Tun: { } tun })
|
|
tun();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_da)
|
|
{
|
|
var d = Daten();
|
|
Shell_NotifyIconW(2 /* NIM_DELETE */, ref d);
|
|
_da = false;
|
|
}
|
|
RemoveWindowSubclass(_hwnd, _proc, (UIntPtr)41);
|
|
if (_icon != IntPtr.Zero) DestroyIcon(_icon);
|
|
_icon = IntPtr.Zero;
|
|
}
|
|
|
|
private delegate IntPtr SubclassProc(IntPtr h, uint msg, IntPtr wp, IntPtr lp, UIntPtr id, UIntPtr daten);
|
|
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
private struct NOTIFYICONDATAW
|
|
{
|
|
public uint cbSize;
|
|
public IntPtr hWnd;
|
|
public uint uID, uFlags, uCallbackMessage;
|
|
public IntPtr hIcon;
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string szTip;
|
|
public uint dwState, dwStateMask;
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string szInfo;
|
|
public uint uVersion;
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] public string szInfoTitle;
|
|
public uint dwInfoFlags;
|
|
public Guid guidItem;
|
|
public IntPtr hBalloonIcon;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)] private struct POINT { public int X, Y; }
|
|
|
|
[DllImport("shell32.dll", CharSet = CharSet.Unicode)] private static extern bool Shell_NotifyIconW(uint was, ref NOTIFYICONDATAW d);
|
|
[DllImport("comctl32.dll")] private static extern bool SetWindowSubclass(IntPtr h, SubclassProc p, UIntPtr id, UIntPtr daten);
|
|
[DllImport("comctl32.dll")] private static extern bool RemoveWindowSubclass(IntPtr h, SubclassProc p, UIntPtr id);
|
|
[DllImport("comctl32.dll")] private static extern IntPtr DefSubclassProc(IntPtr h, uint msg, IntPtr wp, IntPtr lp);
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)] private static extern IntPtr LoadImageW(IntPtr inst, string name, uint typ, int cx, int cy, uint flags);
|
|
[DllImport("user32.dll")] private static extern bool DestroyIcon(IntPtr h);
|
|
[DllImport("user32.dll")] private static extern IntPtr CreatePopupMenu();
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)] private static extern bool AppendMenuW(IntPtr m, uint flags, UIntPtr id, string text);
|
|
[DllImport("user32.dll")] private static extern bool DestroyMenu(IntPtr m);
|
|
[DllImport("user32.dll")] private static extern int TrackPopupMenu(IntPtr m, uint flags, int x, int y, int r, IntPtr h, IntPtr rect);
|
|
[DllImport("user32.dll")] private static extern bool GetCursorPos(out POINT p);
|
|
[DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr h);
|
|
[DllImport("user32.dll")] private static extern bool PostMessageW(IntPtr h, uint msg, IntPtr wp, IntPtr lp);
|
|
}
|