Fügt Unterstützung für Linksklick-Aktionen hinzu
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.
This commit is contained in:
4
tray.h
4
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 {
|
||||
|
||||
@@ -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;
|
||||
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) {
|
||||
}
|
||||
if (nid.hIcon && !g_icon_is_shared) {
|
||||
DestroyIcon(nid.hIcon);
|
||||
}
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user