python实现windows上气泡提醒效果的方法

本文实例讲述了python实现windows上气泡提醒效果的方法。分享给大家供大家参考。具体实现方法如下:

# -*- encoding: gbk -*-
import sys
import os
import struct
import time
import win32con
from win32api import *
# try and use xp features, so we get alpha-blending etc.
try:
from winxpgui import *
except importerror:
from win32gui import *
class pynotifyicondata:
_struct_format = (
“i” # dword cbsize; 结构大小(字节)
“i” # hwnd hwnd; 处理消息的窗口的句柄
“i” # uint uid; 唯一的标识符
“i” # uint uflags;
“i” # uint ucallbackmessage; 处理消息的窗口接收的消息
“i” # hicon hicon; 托盘图标句柄
“128s” # tchar sztip[128]; 提示文本
“i” # dword dwstate; 托盘图标状态
“i” # dword dwstatemask; 状态掩码
“256s” # tchar szinfo[256]; 气泡提示文本
“i” # union {
# uint utimeout; 气球提示消失时间(毫秒)
# uint uversion; 版本(0 for v4, 3 for v5)
# } dummyunionname;
“64s” # tchar szinfotitle[64]; 气球提示标题
“i” # dword dwinfoflags; 气球提示图标
)
_struct = struct.struct(_struct_format)
hwnd = 0
uid = 0
uflags = 0
ucallbackmessage = 0
hicon = 0
sztip = ”
dwstate = 0
dwstatemask = 0
szinfo = ”
utimeoutorversion = 0
szinfotitle = ”
dwinfoflags = 0
def pack(self):
return self._struct.pack(
self._struct.size,
self.hwnd,
self.uid,
self.uflags,
self.ucallbackmessage,
self.hicon,
self.sztip,
self.dwstate,
self.dwstatemask,
self.szinfo,
self.utimeoutorversion,
self.szinfotitle,
self.dwinfoflags
)
def __setattr__(self, name, value):
# avoid wrong field names
if not hasattr(self, name):
raise nameerror, name
self.__dict__[name] = value
class mainwindow:
def __init__(self, title, msg, duration=3):
# register the window class.
wc = wndclass()
hinst = wc.hinstance = getmodulehandle(none)
wc.lpszclassname = “pythontaskbardemo”
# 字符串只要有值即可,下面3处也一样
wc.lpfnwndproc = { win32con.wm_destroy: self.ondestroy }
# could also specify a wndproc.
classatom = registerclass(wc)
# create the window.
style = win32con.ws_overlapped | win32con.ws_sysmenu
self.hwnd = createwindow(classatom, “taskbar demo”, style,
0, 0, win32con.cw_usedefault, win32con.cw_usedefault,
0, 0, hinst, none
)
updatewindow(self.hwnd)
iconpathname = os.path.abspath(os.path.join(sys.prefix, “pyc.ico”))
icon_flags = win32con.lr_loadfromfile | win32con.lr_defaultsize
try:
hicon = loadimage(hinst, iconpathname, win32con.image_icon, 0, 0, icon_flags)
except:
hicon = loadicon(0, win32con.idi_application)
flags = nif_icon | nif_message | nif_tip
nid = (self.hwnd, 0, flags, win32con.wm_user + 20, hicon, “balloon tooltip demo”)
shell_notifyicon(nim_add, nid)
self.show_balloon(title, msg)
time.sleep(duration)
destroywindow(self.hwnd)
def show_balloon(self, title, msg):
# for this message i can’t use the win32gui structure because
# it doesn’t declare the new, required fields
nid = pynotifyicondata()
nid.hwnd = self.hwnd
nid.uflags = nif_info
# type of balloon and text are random
nid.dwinfoflags = niif_info
nid.szinfo = msg[:64]
nid.szinfotitle = title[:256]
# call the windows function, not the wrapped one
from ctypes import windll
shell_notifyicon = windll.shell32.shell_notifyicona
shell_notifyicon(nim_modify, nid.pack())
def ondestroy(self, hwnd, msg, wparam, lparam):
nid = (self.hwnd, 0)
shell_notifyicon(nim_delete, nid)
postquitmessage(0) # terminate the app.
if __name__==’__main__’:
mainwindow(“您有一条短消息”, “您该睡觉了”)

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类

发表评论