|
| 1 | +""" |
| 2 | +This module contains the code for the notification using the Win32 api. |
| 3 | +
|
| 4 | +From https://gist.github.com/BoppreH/4000505 |
| 5 | +http://timgolden.me.uk/pywin32-docs/win32gui__Shell_NotifyIcon_meth.html |
| 6 | +http://timgolden.me.uk/pywin32-docs/PyNOTIFYICONDATA.html |
| 7 | +""" |
| 8 | +from win32api import GetModuleHandle |
| 9 | +from win32gui import WNDCLASS |
| 10 | +from win32gui import Shell_NotifyIcon |
| 11 | +from win32gui import RegisterClass |
| 12 | +from win32gui import CreateWindow |
| 13 | +from win32gui import UpdateWindow |
| 14 | +from win32gui import PostQuitMessage |
| 15 | +from win32gui import UnregisterClass |
| 16 | +from win32gui import DestroyWindow |
| 17 | +from win32gui import LoadIcon |
| 18 | +from win32gui import LoadImage |
| 19 | +from win32gui import ( |
| 20 | + NIF_ICON, |
| 21 | + NIF_MESSAGE, |
| 22 | + NIF_TIP, |
| 23 | + NIF_INFO, |
| 24 | + NIM_MODIFY, |
| 25 | + NIM_ADD, |
| 26 | + NIM_DELETE, |
| 27 | +) |
| 28 | + |
| 29 | +import win32con |
| 30 | +import sys |
| 31 | +import os |
| 32 | +import struct |
| 33 | +import time |
| 34 | + |
| 35 | + |
| 36 | +class Win32Notification: |
| 37 | + """Displays a notification using the Win32 API. |
| 38 | +
|
| 39 | + Paramters: |
| 40 | +
|
| 41 | + tip (str): Tooltip text (optional) |
| 42 | + timeout (int): Timeout for ballon tooltip in milliseconds (optional) |
| 43 | + title (str): Title for ballon tooltip (optional) |
| 44 | + message (str): Balloon tooltip text (optional) |
| 45 | + """ |
| 46 | + |
| 47 | + def __call__( |
| 48 | + self, message="", title="", tip="Balloon tooltip", timeout=200 |
| 49 | + ): |
| 50 | + |
| 51 | + message_map = {win32con.WM_DESTROY: self.OnDestroy} |
| 52 | + # Register the Window class. |
| 53 | + wc = WNDCLASS() |
| 54 | + hinst = wc.hInstance = GetModuleHandle(None) |
| 55 | + wc.lpszClassName = "PythonTaskbar" |
| 56 | + wc.lpfnWndProc = message_map # could also specify a wndproc. |
| 57 | + classAtom = RegisterClass(wc) |
| 58 | + # Create the Window. |
| 59 | + style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU |
| 60 | + self.hwnd = CreateWindow( |
| 61 | + classAtom, |
| 62 | + "Taskbar", |
| 63 | + style, |
| 64 | + 0, |
| 65 | + 0, |
| 66 | + win32con.CW_USEDEFAULT, |
| 67 | + win32con.CW_USEDEFAULT, |
| 68 | + 0, |
| 69 | + 0, |
| 70 | + hinst, |
| 71 | + None, |
| 72 | + ) |
| 73 | + UpdateWindow(self.hwnd) |
| 74 | + iconPathName = os.path.abspath( |
| 75 | + os.path.join(sys.path[0], "balloontip.ico") |
| 76 | + ) |
| 77 | + icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE |
| 78 | + try: |
| 79 | + hicon = LoadImage( |
| 80 | + hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags |
| 81 | + ) |
| 82 | + except: |
| 83 | + hicon = LoadIcon(0, win32con.IDI_APPLICATION) |
| 84 | + |
| 85 | + flags = NIF_ICON | NIF_MESSAGE | NIF_TIP |
| 86 | + nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, "tooltip") |
| 87 | + Shell_NotifyIcon(NIM_ADD, nid) |
| 88 | + Shell_NotifyIcon( |
| 89 | + NIM_MODIFY, |
| 90 | + ( |
| 91 | + self.hwnd, |
| 92 | + 0, |
| 93 | + NIF_INFO, |
| 94 | + win32con.WM_USER + 20, |
| 95 | + hicon, |
| 96 | + tip, |
| 97 | + message, |
| 98 | + timeout, |
| 99 | + title, |
| 100 | + ), |
| 101 | + ) |
| 102 | + time.sleep(10) |
| 103 | + DestroyWindow(self.hwnd) |
| 104 | + UnregisterClass(classAtom, hinst) |
| 105 | + |
| 106 | + def OnDestroy(self, hwnd, msg, wparam, lparam): |
| 107 | + nid = (self.hwnd, 0) |
| 108 | + Shell_NotifyIcon(NIM_DELETE, nid) |
| 109 | + PostQuitMessage(0) # Terminate the app. |
| 110 | + |
| 111 | + |
| 112 | +def run_balloon_tip(message, **kwargs): |
| 113 | + w = Win32Notification(message, **kwargs) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + run_balloon_tip(message="Here is a balloon tip", title="Demonstration") |
0 commit comments