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

imp*_*imp 1 python windows winapi notifications pywin32

我正在使用气球弹出通知来显示用户通知消息。它仅持续5秒钟,因为默认情况下,Windows通知设置显示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.
Run Code Online (Sandbox Code Playgroud)

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

ica*_*bod 5

显示通知提示框的时间是用户设置,而不是应用程序设置。实际上,从Windows Vista开始,该NOTIFYICONDATA结构的超时值被主动忽略。这样做的原因是,通知气球将被忽略。如果您认为自己的信息太重要而不能被用户忽略,则说明您使用的是错误的界面。

话虽如此,它可以通过使用以编程方式更改设置SystemParametersInfoAPI,但我鼓励你这样做,因为它会影响在全系统级别的设置,这是不是你打个电话。