import wx
import win32gui
import win32con
import time
from winxpgui import *
class SparkTaskBarIcon(wx.TaskBarIcon):
TBMENU_RESTORE = wx.NewId()
TBMENU_CLOSE = wx.NewId()
TBMENU_CHANGE = wx.NewId()
TBMENU_REMOVE = wx.NewId()
def __init__(self, frame,picon,icon):
wx.TaskBarIcon.__init__(self)
self.frame = frame
# Set the image
#icon = self.MakeIcon(images.WXPdemo.GetImage())
wc = win32gui.WNDCLASS()
hinst = wc.hInstance = win32gui.GetModuleHandle(None)
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
hicon = LoadImage(hinst, icon, win32con.IMAGE_ICON, 0, 0, icon_flags)
self.SetIcon(picon, "Spark")
self.imgidx = 1
# bind some events
self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarActivate)
self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=self.TBMENU_RESTORE)
self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=self.TBMENU_CLOSE)
#self.Bind(wx.EVT_MENU, self.OnTaskBarChange, id=self.TBMENU_CHANGE)
self.Bind(wx.EVT_MENU, self.OnTaskBarRemove, id=self.TBMENU_REMOVE)
#气泡提示注册win的窗口消息
wc.lpszClassName = "Spark"
wc.lpfnWndProc = {win32con.WM_DESTROY: self.OnDestroy,}
classAtom = win32gui.RegisterClass(wc)
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
self.hwnd = win32gui.CreateWindow( classAtom, "Spark", style,
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,
0, 0, hinst, None)
#hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
nid = (self.hwnd, 0, win32gui.NIF_ICON, win32con.WM_USER+20, hicon, "Spark")
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
def OnDestroy(self, hwnd, msg, wparam, lparam):
nid = (self.hwnd, 0)
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
win32gui.PostQuitMessage(0) # Terminate the app.
def showMsg(self, title, msg):
# 原作者使用Shell_NotifyIconA方法代替包装后的Shell_NotifyIcon方法
# 据称是不能win32gui structure, 我稀里糊涂搞出来了.
# 具体对比原代码.
nid = (self.hwnd, # 句柄
0, # 托盘图标ID
win32gui.NIF_INFO, # 标识
0, # 回调消息ID
0, # 托盘图标句柄
"Spark Message", # 图标字符串
msg, # 气球提示字符串
5, # 提示的显示时间
title, # 提示标题
win32gui.NIIF_INFO # 提示用到的图标
)
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid)
def CreatePopupMenu(self):
"""
This method is called by the base class when it needs to popup
the menu for the default EVT_RIGHT_DOWN event. Just create
the menu how you want it and return it from this function,
the base class takes care of the rest.
"""
menu = wx.Menu()
menu.Append(self.TBMENU_RESTORE, "Restore spark")
menu.Append(self.TBMENU_CLOSE, "Close spark")
menu.AppendSeparator()
#menu.Append(self.TBMENU_CHANGE, "Change the TB Icon")
menu.Append(self.TBMENU_REMOVE, "Remove Spark")
return menu
#def MakeIcon(self, img):
#"""
#The various platforms have different requirements for the
#icon size...
#"""
#if "wxMSW" in wx.PlatformInfo:
#img = img.Scale(16, 16)
#elif "wxGTK" in wx.PlatformInfo:
#img = img.Scale(22, 22)
## wxMac can be any size upto 128x128, so leave the source img alone....
#icon = wx.IconFromBitmap(img.ConvertToBitmap() )
#return icon
def OnTaskBarActivate(self, evt):
if self.frame.IsIconized():
self.frame.Iconize(False)
if not self.frame.IsShown():
self.frame.Show(True)
self.frame.Raise()
def OnTaskBarClose(self, evt):
wx.CallAfter(self.frame.OnClose)
#def OnTaskBarChange(self, evt):
#names = [ "WXPdemo", "Mondrian", "Pencil", "Carrot" ]
#name = names[self.imgidx]
#eImg = getattr(images, name)
#self.imgidx += 1
#if self.imgidx >= len(names):
#self.imgidx = 0
#icon = self.MakeIcon(eImg.Image)
#self.SetIcon(icon, "This is a new icon: " + name)
def OnTaskBarRemove(self, evt):
self.RemoveIcon()
借鉴网上例子和wxPython自带的例子,
还没有实现wxPython和win32gui的转换,会有两个图标,知道如何转换的请告之。
阅读(2474) | 评论(0) | 转发(0) |