如何延长Windows气球弹出通知的时间
我正在使用气球弹出通知来显示用户的通知消息。默认情况下,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.
我想知道怎么让这个气球弹出通知显示更长时间。此外,还要考虑到鼠标移动时,通知也应该保持显示很久。还有,在弹出通知后,程序应该结束,但通知应该继续显示。
1 个回答
5
显示通知气泡的时间是用户自己设置的,而不是应用程序的设置。实际上,从Windows Vista开始,NOTIFYICONDATA
结构中的超时时间设置会被忽略。这样做的原因是,通知气泡本来就是为了让用户忽视的。如果你觉得你的信息太重要,用户不应该忽视,那你可能一开始就选错了界面。
不过,确实可以通过使用SystemParametersInfo
这个API来程序化地改变这些设置,但我建议你不要这样做,因为这会影响到整个系统的设置,而这并不是你能决定的事情。