如何在Windows上增加Balloon弹出通知的时间

时间:2014-03-28 07:05:43

标签: python windows winapi notifications pywin32

我正在使用气球弹出通知来显示用户通知消息。 它只持续5秒,因为默认情况下,窗口通知设置会在5秒内完成。

我的代码是:

from win32api import *
from win32con import NULL
from win32gui import *
import win32com.client as com
import win32con
import win32file
import time


class WindowsBalloonTip:
    def __init__(self, title, msg,notlivelong):
        message_map = {
                win32con.WM_DESTROY: self.OnDestroy,
        }
        # Register the Window class.
        iconPathName= "D:\cc.ico"

        wc = WNDCLASS()
        hinst = wc.hInstance = GetModuleHandle(None)
        wc.lpszClassName = "PythonTaskbar"
        wc.lpfnWndProc = message_map # could also specify a wndproc.
        classAtom = RegisterClass(wc)
        # Create the Window.
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
        self.hwnd = CreateWindow( classAtom, "Taskbar", style, \
                0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \
                0, 0, hinst, None)
        UpdateWindow(self.hwnd) 
        print iconPathName
        icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
        try:
            hicon = LoadImage(hinst,iconPathName, win32con.IMAGE_ICON, 16, 16,icon_flags)
        except:
            hicon = LoadIcon(0, win32con.IDI_APPLICATION)
#             logging.debug("Image adding fail")
        flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
        nid = (self.hwnd, 0, flags, win32con.WM_USER+20, hicon, "TITLE")
        Shell_NotifyIcon(NIM_ADD, nid)
        Shell_NotifyIcon(NIM_MODIFY, \
                         (self.hwnd, 0, NIF_INFO, win32con.WM_USER+20,\
                          hicon, "Balloon  tooltip",msg,200,title))
        # self.show_balloon(title, msg)

        global sleep
        time.sleep(2)
        if notlivelong:
            DestroyWindow(self.hwnd)
            UnregisterClass(wc.lpszClassName, None)
    def OnDestroy(self, hwnd, msg, wparam, lparam):
        nid = (self.hwnd, 0)
        Shell_NotifyIcon(NIM_MODIFY, nid)
        PostQuitMessage(0)
        # Terminate the app.

def balloon_tip(title, msg,notlivelong):
    w=WindowsBalloonTip(title, msg,notlivelong)


balloon_tip("title1", "msg1",False) #iF True, then last for 2 sec.

如何使此气球弹出以保持更长时间。同样考虑这种情况,弹出窗口也应该在鼠标移动的情况下保持很长时间。此外,在调用弹出窗口后,进程应该终止,但弹出窗口应该保留。

1 个答案:

答案 0 :(得分:4)

显示通知气球的时间是用户设置,而不是应用程序设置。实际上,从Windows Vista开始,NOTIFYICONDATA结构的超时值会被忽略。原因是通知气球存在to be ignored。如果您认为您的信息太重要而且用户无法忽略,那么您将使用错误的界面开始。

话虽如此, 可以使用SystemParametersInfo API以编程方式更改设置,但我鼓励您不要那样做,因为它会影响系统级别的设置,而且这不是您的号召。