From fa1e862259dd0f0e1d0ca0e0c00b0db0fe333c4c Mon Sep 17 00:00:00 2001 From: Yadciel Date: Mon, 2 Feb 2026 21:15:52 +0100 Subject: [PATCH] =?UTF-8?q?F=C3=BCgt=20Unterst=C3=BCtzung=20f=C3=BCr=20Lin?= =?UTF-8?q?ksklick-Aktionen=20hinzu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ermöglicht die Ausführung einer Callback-Funktion beim Linksklick auf das Tray-Icon. Dies bietet erweiterte Interaktionsmöglichkeiten mit der Anwendung über das Tray-Icon. Zusätzlich wird das Problem behoben, dass das Icon nicht freigegeben wird und somit nicht korrekt zerstört wird. --- tray.h | 4 ++++ tray_windows.c | 34 +++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/tray.h b/tray.h index 26d1426..4183536 100644 --- a/tray.h +++ b/tray.h @@ -12,6 +12,10 @@ struct tray { const char *icon; char *tooltip; struct tray_menu *menu; + void *icon_handle; + int icon_is_shared; + void (*left_click_cb)(void *context); + void *left_click_context; }; struct tray_menu { diff --git a/tray_windows.c b/tray_windows.c index b28424a..ecd2221 100644 --- a/tray_windows.c +++ b/tray_windows.c @@ -10,6 +10,8 @@ static WNDCLASSEX wc; static NOTIFYICONDATA nid; static HWND hwnd; static HMENU hmenu = NULL; +static struct tray *g_tray = NULL; +static int g_icon_is_shared = 0; static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { @@ -21,7 +23,13 @@ static LRESULT CALLBACK _tray_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, PostQuitMessage(0); return 0; case WM_TRAY_CALLBACK_MESSAGE: - if (lparam == WM_LBUTTONUP || lparam == WM_RBUTTONUP) { + if (lparam == WM_LBUTTONUP) { + if (g_tray && g_tray->left_click_cb) { + g_tray->left_click_cb(g_tray->left_click_context); + } + return 0; + } + if (lparam == WM_RBUTTONUP) { POINT p; GetCursorPos(&p); SetForegroundWindow(hwnd); @@ -106,6 +114,7 @@ int tray_init(struct tray *tray) { nid.uCallbackMessage = WM_TRAY_CALLBACK_MESSAGE; Shell_NotifyIcon(NIM_ADD, &nid); + g_tray = tray; tray_update(tray); return 0; } @@ -126,18 +135,29 @@ int tray_loop(int blocking) { } void tray_update(struct tray *tray) { + g_tray = tray; HMENU prevmenu = hmenu; UINT id = ID_TRAY_FIRST; hmenu = _tray_menu(tray->menu, &id); SendMessage(hwnd, WM_INITMENUPOPUP, (WPARAM)hmenu, 0); - HICON icon; - ExtractIconEx(tray->icon, 0, NULL, &icon, 1); - if (nid.hIcon) { + HICON icon = NULL; + int icon_is_shared = 0; + if (tray->icon_handle) { + icon = (HICON)tray->icon_handle; + icon_is_shared = tray->icon_is_shared; + } else if (tray->icon) { + ExtractIconEx(tray->icon, 0, NULL, &icon, 1); + } + if (nid.hIcon && !g_icon_is_shared) { DestroyIcon(nid.hIcon); } - nid.hIcon = icon; + if (icon) { + nid.hIcon = icon; + g_icon_is_shared = icon_is_shared; + } if(tray->tooltip != 0 && strlen(tray->tooltip) > 0) { - strncpy(nid.szTip, tray->tooltip, sizeof(nid.szTip)); + strncpy(nid.szTip, tray->tooltip, sizeof(nid.szTip) - 1); + nid.szTip[sizeof(nid.szTip) - 1] = '\0'; nid.uFlags |= NIF_TIP; } Shell_NotifyIcon(NIM_MODIFY, &nid); @@ -149,7 +169,7 @@ void tray_update(struct tray *tray) { void tray_exit(void) { Shell_NotifyIcon(NIM_DELETE, &nid); - if (nid.hIcon != 0) { + if (nid.hIcon != 0 && !g_icon_is_shared) { DestroyIcon(nid.hIcon); } if (hmenu != 0) {