如何线程系统托盘气球通知

时间:2013-10-02 07:12:05

标签: multithreading winapi python-2.7 win32gui systray

我正在将系统托盘气球通知作为线程。我正在使用win32api和win32gui。我希望每当在代码中调用balloon_tip(title,msg)时,就会出现通知气球并同时执行代码。我在代码中多次调用balloon_tip(title,msg)来显示通知。但由于myThread1类中的time.sleep(1),控件正在等待一秒钟,这会减慢代码的整个进程执行速度。此外,classAtom = RegisterClass(wc)错误的错误:(1410,'RegisterClass','类已经存在。')即将到来,但你可以看到,我也在做 UnregisterClass(wc) .lpszClassName,无)即可。我认为问题是 myThread1类没有正确的线程

class myThread1(threading.Thread):
    def __init__(self, title, msg):
        threading.Thread.__init__(self)
        self.title=title
        self.msg=msg
        self.daemon = True

    def run(self):  
        message_map = {
                win32con.WM_DESTROY: self.OnDestroy,
        }  
        # Register the Window class.
        iconPathName= D:\\cc.ico
        f.close();

        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",self.msg,200,self.title))
        # self.show_balloon(title, msg)
        time.sleep(1)
        DestroyWindow(self.hwnd)
        UnregisterClass(wc.lpszClassName,None )
    def OnDestroy(self, hwnd, msg, wparam, lparam):
        nid = (self.hwnd, 0)
        Shell_NotifyIcon(NIM_DELETE, nid)
        PostQuitMessage(0)
        # Terminate the app.

def balloon_tip(title, msg):
    thread1 = myThread1(title, msg)
    thread1.start()

0 个答案:

没有答案
相关问题