Bien le bonjour !
Comme "beaucoup" je cherche une solution en python pour appeler une fonction lorsque je clique sur une notification windows !
J'ai trouvé quelques informations en Visual C++ mais je n'y comprends pas grand chose et surtout je ne vois pas comment adapter ça à Python...
Pour commencer, j'utilise le module plyer, voici le code utilisé par ce module pour générer une notification :
Ensuite, voici la documentation Windows sur le NOTIFYICONDATAW : Lien
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 # add icon and messages to window hicon = self._hicon flags = NIF_TIP | NIF_INFO icon_flag = 0 if hicon is not None: flags |= NIF_ICON # if icon is default app's one, don't display it in message if self._balloon_icon is not None: icon_flag = NIIF_USER | NIIF_LARGE_ICON notify_data = win_api_defs.get_NOTIFYICONDATAW( 0, self._hwnd, id(self), flags, 0, hicon, app_name, 0, 0, message, NOTIFYICON_VERSION_4, title, icon_flag, win_api_defs.GUID(), self._balloon_icon ) self._notify_data = notify_data if not win_api_defs.Shell_NotifyIconW(NIM_ADD, notify_data): raise Exception('Shell_NotifyIconW failed.') if not win_api_defs.Shell_NotifyIconW(NIM_SETVERSION, notify_data): raise Exception('Shell_NotifyIconW failed.')
Si je ne dis pas de connerie, l'argument intéressant est l'uCallbackMessage (cf. lien) ?
J'ai donc cherché une façon de l'exploité pour récupérer le clic et je suis tombé sur un poste de 2003 qui explique la manipe en Visual C++ : Lien
Et donc maintenant que dois-je faire pour adapter ça en python ?In your NOTIFYICONDATA make sure your uFlags contains NIF_MESSAGE and that the uCallbackMessage contains the message you want to be send to you when you click on the tray icon.
Example:
somewhere define the following:
Code C : Sélectionner tout - Visualiser dans une fenêtre à part #define MYWM_NOTIFYICON (WM_APP+100)
Now, to add your icon to the tray do:
Code C : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 NOTIFYICONDATA tnd = {0}; tnd.cbSize = sizeof(NOTIFYICONDATA); tnd.hWnd = hWnd; // Your hwnd tnd.uID = nID; // Some ID tnd.uFlags = NIF_MESSAGE | otherFlags; tnd.uCallbackMessage = MYWM_NOTIFYICON; tnd.hIcon = hIcon; // your icon handle Shell_NotifyIcon(NIM_ADD, &tnd);
In your WindowProc do:
Code C : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 switch (message) { case MYWM_NOTIFYICON: switch (lParam) { case WM_RBUTTONUP: // Right mouse button upped break; case WM_MBUTTONUP: // Middle button upped break; case WM_LBUTTONUP: // left button upped break; } break; }
J'ai trouvé que WM_APP = 0x8000 mais bon voila, je suis pas plus avancé... Le 2ème bout de code ça va, j'ai déjà ce qu'il faut ou presque mais le dernier ???
Merci à ceux qui pourraient nous aider dans cette quête qui dure depuis des années ! ^^
Partager