def refresh_icon(self, recreate=False):
        """
        Refresh the icon. To be called after updating the icon.
        :param recreate: Recreate the icon
        """
        # Try and find a custom icon
        hinst = win32gui.GetModuleHandle(None)
        if isinstance(self.icon, str):
            if os.path.isfile(self.icon):
                icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
                hicon = win32gui.LoadImage(hinst, self.icon,
                                           win32con.IMAGE_ICON, 0, 0,
                                           icon_flags)
            else:
                hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
        else:
            hinst = win32api.GetModuleHandle(None)
            hicon = win32gui.LoadIcon(hinst, int(self.icon))

        if not self.notify_id or recreate:
            message = win32gui.NIM_ADD
        else:
            message = win32gui.NIM_MODIFY
        self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON
                          | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
                          win32con.WM_USER + 20, hicon, self.hover_text)
        win32gui.Shell_NotifyIcon(message, self.notify_id)
Example #2
0
	def refresh_icon(self):
		# Try and find a custom icon
		hinst = win32gui.GetModuleHandle(None)
		if os.path.isfile(self.icon):
			icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
			hicon = win32gui.LoadImage(hinst,
									   self.icon,
									   win32con.IMAGE_ICON,
									   0,
									   0,
									   icon_flags)
		else:
			print ("Can't find icon file - using default.")
			hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)

		if self.notify_id: message = win32gui.NIM_MODIFY  # NIM_MODIFY sends a message to modify an icon in the taskbar.
		else: message = win32gui.NIM_ADD  # NIM_ADD sends a message to add an icon in the taskbar.
		
		# This is a NOTIFYICONDATA structure as defined here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb773352(v=vs.85).aspx
		#   The fourth argument sets uCallbackMessage = 'win32con.WM_USER+self.OFFSET'
		#   uCallbackMessage: "The system uses this identifier to send notification messages to the window identified in hWnd."
		self.notify_id = (self.hwnd,
						  0,
						  win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
						  win32con.WM_USER+self.OFFSET,
						  hicon,
						  self.hover_text)
		win32gui.Shell_NotifyIcon(message, self.notify_id)  # Registers the message 'win32con.WM_USER+self.OFFSET' to trigger the 'notify' function on mouse-event
 def start_icon(self):
     message_map = {win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
                    win32con.WM_DESTROY: self.destroy,
                    win32con.WM_COMMAND: self.command,
                    win32con. WM_USER +20 : self.notify ,}
     # Register the Window class.
     window_class = win32gui.WNDCLASS()
     hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
     window_class.lpszClassName = self.window_class_name
     window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
     window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
     window_class.hbrBackground = win32con.COLOR_WINDOW
     window_class.lpfnWndProc = message_map # could also specify a wndproc.
     classAtom = win32gui.RegisterClass(window_class)
     # Create the Window.
     style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
     self.hwnd = win32gui.CreateWindow(classAtom,
                                       self.window_class_name,
                                       style,
                                       0,
                                       0,
                                       win32con.CW_USEDEFAULT,
                                       win32con.CW_USEDEFAULT,
                                       0,
                                       0,
                                       hinst,
                                       None)
     win32gui.UpdateWindow(self.hwnd)
     self.notify_id = None
     self.refresh_icon()
     win32gui.PumpMessages()
Example #4
0
 def create_window(self):
     message_map = {
         win32gui.RegisterWindowMessage('TaskbarCreated'): self.on_restart,
         win32con.WM_DESTROY: self.on_destroy,
         win32con.WM_COMMAND: self.on_command,
         win32con.WM_USER + 20: self.on_notify_icon,
     }
     # Register the Window class.
     window_class = win32gui.WNDCLASS()
     hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
     window_class.lpszClassName = self.WINDOW_CLASS_NAME
     # window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW  # 垂直/水平方向变化自动重绘,似乎没啥用
     window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
     window_class.hbrBackground = win32con.COLOR_WINDOW
     window_class.lpfnWndProc = message_map
     classAtom = win32gui.RegisterClass(window_class)
     # Create the Window.
     style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
     self.hwnd = win32gui.CreateWindow(
         classAtom,
         self.WINDOW_CLASS_NAME,
         style,
         0,
         0,
         win32con.CW_USEDEFAULT,
         win32con.CW_USEDEFAULT,
         0,
         0,
         hinst,
         None,
     )
     win32gui.UpdateWindow(self.hwnd)
Example #5
0
    def refresh_icon(self):
        """
        Refresh the icon being used by the application
        """
        hinst = win32gui.GetModuleHandle(None)
        if os.path.isfile(self.icon):
            icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
            hicon = win32gui.LoadImage(hinst,
                                       self.icon,
                                       win32con.IMAGE_ICON,
                                       0,
                                       0,
                                       icon_flags)
        else:
            print("Can't find icon file - using default.")
            hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)

        if self.notify_id: message = win32gui.NIM_MODIFY
        else: message = win32gui.NIM_ADD
        self.notify_id = (self.hwnd,
                          0,
                          win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
                          win32con.WM_USER+20,
                          hicon,
                          self.hover_text)
        win32gui.Shell_NotifyIcon(message, self.notify_id)
Example #6
0
    def refresh_icon_handler(self):
        # Try and find a custom icon
        if not self.hwnd:
            return

        hinst = win32gui.GetModuleHandle(None)
        hicon = self.icon_cache.get(self.icon, None)
        if hicon is None:
            if os.path.isfile(self.icon):
                hicon = win32gui.LoadImage(
                    hinst, self.icon, win32con.IMAGE_ICON, 0, 0,
                    win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE)
            else:
                print "Can't find icon file - using default."
                hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
            self.icon_cache[self.icon] = hicon

        if self.notify_id:
            message = win32gui.NIM_MODIFY
        else:
            message = win32gui.NIM_ADD

        self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON
                          | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
                          win32con.WM_USER + 20, hicon,
                          self.tooltip_text.encode(os_encoding))

        try:
            win32gui.Shell_NotifyIcon(message, self.notify_id)
        except:
            if message == win32gui.NIM_ADD:
                self.notify_id = None
            raise
Example #7
0
    def refresh_icon(self):
        # Try and find a custom icon
        self.hinst = win32gui.GetModuleHandle(None)  # 获取句柄
        if os.path.isfile(self.icon):
            icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
            hicon = win32gui.LoadImage(self.hinst,  # 载入图像  #句柄
                                       self.icon,  # icon
                                       win32con.IMAGE_ICON,  # 类型
                                       0,  # cxDesired 参数,控制像素
                                       0,  # cyDesired
                                       icon_flags)  # 载入图像大小
        else:
            # print "Can't find icon file - using default."
            # 没有找到图标,使用默认图标
            hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)

        if self.notify_id:
            message = win32gui.NIM_MODIFY  # 修改状态去的图标
        else:
            message = win32gui.NIM_ADD  # 添加图标到状态区
        self.notify_id = (self.hwnd,
                          0,
                          win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
                          win32con.WM_USER + 20,
                          hicon,
                          self.hover_text)
        win32gui.Shell_NotifyIcon(message, self.notify_id)  # 添加、移除或改变任务栏图标
Example #8
0
 def run(self):
     message_map = {
         win32con.WM_DESTROY: self.destroy,
         win32con.WM_COMMAND: self.command,
         self.MSG_NOTIFYICON: self.notify,
         self.MSG_POOLER: self.msg_pooler
     }
     # Register the Window class.
     window_class = win32gui.WNDCLASS()
     hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
     window_class.lpszClassName = self.controller.CLASSNAME
     window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
     window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
     window_class.hbrBackground = win32con.COLOR_WINDOW
     window_class.lpfnWndProc = message_map  # could also specify a wndproc.
     classAtom = win32gui.RegisterClass(window_class)
     # Create the Window.
     style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
     self.hwnd = win32gui.CreateWindow(classAtom, self.controller.CLASSNAME,
                                       style, 0, 0, win32con.CW_USEDEFAULT,
                                       win32con.CW_USEDEFAULT, 0, 0, hinst,
                                       None)
     win32gui.UpdateWindow(self.hwnd)
     self.notify_id = None
     self.refresh_icon()
     self.evt_gui_ready.set()
     win32gui.PumpMessages()
Example #9
0
	def refresh_icon(self, recreate=False):
		# Try and find a custom icon
		hinst = win32gui.GetModuleHandle(None)
		if isinstance(self.icon, str):
			if os.path.isfile(self.icon):
				icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
				hicon = win32gui.LoadImage(hinst,
					self.icon,
					win32con.IMAGE_ICON,
					0,
					0,
					icon_flags)
			else:
				hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
		else:
			hinst = win32api.GetModuleHandle(None)
			hicon = win32gui.LoadIcon(hinst, int(self.icon))

		if not self.notify_id or recreate:
			message = win32gui.NIM_ADD
		else:
			message = win32gui.NIM_MODIFY
		self.notify_id = (self.hwnd,
						  0,
						  win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
						  win32con.WM_USER+20,
						  hicon,
						  self.hover_text)
		try:
			win32gui.Shell_NotifyIcon(message, self.notify_id)
		except: # just catch strange error
			pass
Example #10
0
	def refresh_icon(self):
		# Try and find a custom icon
		hinst = win32gui.GetModuleHandle(None)
		if os.path.isfile(self.icon):
			icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
			hicon = win32gui.LoadImage(hinst,
									   self.icon,
									   win32con.IMAGE_ICON,
									   0,
									   0,
									   icon_flags)
		else:
			hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
			if self.error_on_no_icon:
				raise FileNotFoundError("Can't find icon file")
			else:
				print("Can't find icon file - set error_on_no_icon to True to recieve an error")

		if self.notify_id: message = win32gui.NIM_MODIFY
		else: message = win32gui.NIM_ADD
		self.notify_id = (self.hwnd,
						  0,
						  win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
						  win32con.WM_USER+20,
						  hicon,
						  self.hover_text)
		win32gui.Shell_NotifyIcon(message, self.notify_id)
Example #11
0
    def __init__(self,
                 icon,
                 gui,
                 hover_text,
                 menu_options,
                 on_quit=None,
                 default_menu_index=None,
                 window_class_name=None,):

        self.icon = icon
        self.gui = gui  # 增加了这个参数,是产生托盘的GUI类的实例,参见后面代码
        self.hover_text = hover_text
        self.on_quit = on_quit

        # 生成菜单
        # 改了选项的文字,把'quit'替换成自己想用的代表'退出'的文字
        menu_options = menu_options + ((u'退出', None, self.QUIT),)
        self._next_action_id = self.FIRST_ID
        self.menu_actions_by_id = set()
        self.menu_options = self._add_ids_to_menu_options(list(menu_options))
        self.menu_actions_by_id = dict(self.menu_actions_by_id)
        del self._next_action_id

        self.default_menu_index = (default_menu_index or 0)
        self.window_class_name = window_class_name or "SysTrayIconPy"

        message_map = {win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,  # 注册窗体消息,传递给相同名字的窗体
                       win32con.WM_DESTROY: self.destroy,
                       win32con.WM_COMMAND: self.command,
                       win32con.WM_USER + 20: self.notify, }
        # Register the Window class.
        window_class = win32gui.WNDCLASS()  # 生成窗口结构对象
        self.hinst = window_class.hInstance = win32gui.GetModuleHandle(
            None)  # 实例化
        window_class.lpszClassName = self.window_class_name  # 窗口类名
        window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW         # 窗口类型
        window_class.hCursor = win32gui.LoadCursor(
            0, win32con.IDC_ARROW)  # 窗口鼠标光标
        window_class.hbrBackground = win32con.COLOR_WINDOW  # 窗口背景色
        # could also specify a wndproc.   #定义窗口处理函数
        window_class.lpfnWndProc = message_map
        self.classAtom = win32gui.RegisterClass(window_class)  # 注册窗口类
        # Create the Window.
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
        self.hwnd = win32gui.CreateWindow(self.classAtom,  # 生成窗口   类名
                                          self.window_class_name,  # 窗口title
                                          style,  # 类型
                                          0,  # x
                                          0,  # y
                                          win32con.CW_USEDEFAULT,  # 宽度
                                          win32con.CW_USEDEFAULT,  # 高度
                                          0,  # 父窗口
                                          0,  # 菜单
                                          self.hinst,  # 句柄实例
                                          None)  # 必须为None,未知项
        win32gui.UpdateWindow(self.hwnd)  # 更新窗口
        self.notify_id = None
        self.refresh_icon()

        win32gui.PumpMessages()  # 消息循环直到窗口退出
Example #12
0
    def refresh_icon(self):
        # Try and find a custom icon
        hinst = win32gui.GetModuleHandle(None)
        if os.path.isfile(self.icon):
            icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
            self.hicon = win32gui.LoadImage(hinst,
                                            self.icon,
                                            win32con.IMAGE_ICON,
                                            0,
                                            0,
                                            icon_flags)
        else:
            print("Can't find icon file - using default.")
            self.hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)

        if self.notify_id:
            message = win32gui.NIM_MODIFY
        else:
            message = win32gui.NIM_ADD

        self.notify_id = (self.shell.main_frame.hwnd,
                          0,
                          (win32gui.NIF_ICON | win32gui.NIF_MESSAGE |
                           win32gui.NIF_TIP),
                          win32con.WM_USER + 20,
                          self.hicon,
                          self.hover_text)
        win32gui.Shell_NotifyIcon(message, self.notify_id)
Example #13
0
 def __init__(self,
              icon,
              hover_text,
              menu_options,
              on_quit=None,
              default_menu_index=None,
              window_class_name=None,
              passClass=True):
     
     self.icon = icon
     self.hover_text = hover_text
     self.on_quit = on_quit
     self.passClass = passClass
     
     menu_options =(('退出程序', None, self.QUIT),) + menu_options
     self._next_action_id = self.FIRST_ID
     self.menu_actions_by_id = set()
     self.menu_options = self._add_ids_to_menu_options(list(menu_options))
     self.menu_actions_by_id = dict(self.menu_actions_by_id)
     del self._next_action_id
     
     
     self.default_menu_index = (default_menu_index or 0)
     self.window_class_name = window_class_name or "SysTrayIconPy"
     
     message_map = {win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
                    win32con.WM_DESTROY: self.destroy,
                    win32con.WM_COMMAND: self.command,
                    win32con.WM_USER+20: self.notify,}
     # Register the Window class.
     window_class = win32gui.WNDCLASS()
     self.window_class = window_class
     hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
     self.hinst = hinst
     window_class.lpszClassName = self.window_class_name
     window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
     window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
     window_class.hbrBackground = win32con.COLOR_WINDOW
     window_class.lpfnWndProc = message_map # could also specify a wndproc.
     classAtom = win32gui.RegisterClass(window_class)
     self.classAtom = classAtom
     # Create the Window.
     style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
     self.hwnd = win32gui.CreateWindow(classAtom,
                                       self.window_class_name,
                                       style,
                                       0,
                                       0,
                                       win32con.CW_USEDEFAULT,
                                       win32con.CW_USEDEFAULT,
                                       0,
                                       0,
                                       hinst,
                                       None)
     win32gui.UpdateWindow(self.hwnd)
     self.notify_id = None
     self.refresh_icon()
     
     win32gui.PumpMessages()
Example #14
0
    def __init__(self,
                 icon,
                 hover_text,
                 menu_options,
                 on_quit=None,
                 default_menu_index=None,
                 window_class_name=None,
                 lock=None,
                 init_callback=None):

        self.icon = icon
        self.hover_text = hover_text
        self.on_quit = on_quit

        self.hmenu = None
        self.lock = lock

        self.init_menu_options(menu_options)

        self.default_menu_index = (default_menu_index or 0)
        self.window_class_name = window_class_name or "SysTrayIconPy"

        try:
            message_map = {
                win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
                win32con.WM_DESTROY: self.destroy,
                win32con.WM_COMMAND: self.command,
                win32con.WM_USER + 20: self.notify,
                win32con.WM_USER + 21: self._refresh_icon_event
            }
            # Register the Window class.
            window_class = win32gui.WNDCLASS()
            hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
            window_class.lpszClassName = self.window_class_name
            window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
            window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
            window_class.hbrBackground = win32con.COLOR_WINDOW
            window_class.lpfnWndProc = message_map  # could also specify a wndproc.
            classAtom = win32gui.RegisterClass(window_class)
            # Create the Window.
            style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
            self.hwnd = win32gui.CreateWindow(classAtom,
                                              self.window_class_name, style, 0,
                                              0, win32con.CW_USEDEFAULT,
                                              win32con.CW_USEDEFAULT, 0, 0,
                                              hinst, None)
            win32gui.UpdateWindow(self.hwnd)
            self.notify_id = None
            self.refresh_icon()
            atexit.register(self.destroy, None, None, None, None)
            SysTrayIcon.instance = self
            self.threadid = win32api.GetCurrentThreadId()
        finally:
            if init_callback is not None:
                init_callback(self)
        win32gui.PumpMessages()
Example #15
0
 def register_wnd_class(self, message_map):
     # Register the Window class.
     window_class = win32gui.WNDCLASS()
     self.hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
     window_class.lpszClassName = self.window_class_name
     window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
     window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
     window_class.hbrBackground = win32con.COLOR_WINDOW
     window_class.lpfnWndProc = message_map  # could also specify a wndproc.
     return win32gui.RegisterClass(window_class)
Example #16
0
 def register_window_class(self, message_map, window_class_name=None):
     #self.window_class_name = window_class_name or "SysTrayIconPy"                
     self.window_class = win32gui.WNDCLASS()
     self.window_class.hInstance = win32gui.GetModuleHandle(None)
     self.window_class.lpszClassName = window_class_name or "SysTrayIconPy"        #self.window_class_name
     self.window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
     self.window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
     self.window_class.hbrBackground = win32con.COLOR_WINDOW
     self.window_class.lpfnWndProc = message_map # could also specify a wndproc.        
     self.classAtom = win32gui.RegisterClass(self.window_class)
Example #17
0
 def _get_icon(self):
     # Try and find a custom icon
     hinst = win32gui.GetModuleHandle(None)
     if os.path.isfile(self.icon):
         icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
         hicon = win32gui.LoadImage(hinst, self.icon, win32con.IMAGE_ICON,
                                    0, 0, icon_flags)
     else:
         print "Can't find icon file - using default."
         hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
     return hicon
Example #18
0
 def __init__(self, apiServer):
     
     self.apiServer = apiServer
     
     self.icon = AppFolders.iconsPath("logo.ico")
     print self.icon
     self.hover_text = "iBrew"
         
     menu_options = (
                     ('Interface', None, self.show),
                    )
     
     menu_options = menu_options + (('Quit', None, self.QUIT),)
     self._next_action_id = self.FIRST_ID
     self.menu_actions_by_id = set()
     self.menu_options = self._add_ids_to_menu_options(list(menu_options))
     self.menu_actions_by_id = dict(self.menu_actions_by_id)
     del self._next_action_id
     
     
     self.default_menu_index = 1
     self.window_class_name = "ComicStreamerTrayIcon"
     
     message_map = {win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
                    win32con.WM_DESTROY: self.destroy,
                    win32con.WM_COMMAND: self.command,
                    win32con.WM_USER+20 : self.notify,}
     # Register the Window class.
     window_class = win32gui.WNDCLASS()
     hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
     window_class.lpszClassName = self.window_class_name
     window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
     window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
     window_class.hbrBackground = win32con.COLOR_WINDOW
     window_class.lpfnWndProc = message_map # could also specify a wndproc.
     classAtom = win32gui.RegisterClass(window_class)
     # Create the Window.
     style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
     self.hwnd = win32gui.CreateWindow(classAtom,
                                       self.window_class_name,
                                       style,
                                       0,
                                       0,
                                       win32con.CW_USEDEFAULT,
                                       win32con.CW_USEDEFAULT,
                                       0,
                                       0,
                                       hinst,
                                       None)
     win32gui.UpdateWindow(self.hwnd)
     self.notify_id = None
     self.refresh_icon()
Example #19
0
 def init_icon(self, iconpath=None):
     if not iconpath:
         python_path = os.path.dirname(os.path.abspath(sys.executable))
         iconpath = os.path.join(python_path, 'DLLs', 'pyd.ico')
     if os.path.isfile(iconpath):
         hinst = win32gui.GetModuleHandle(None)
         icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
         hicon = win32gui.LoadImage(hinst, iconpath, win32con.IMAGE_ICON, 0,
                                    0, icon_flags)
     else:
         logger.warning("Can't find icon file - using default.")
         hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
     self.hicon = hicon
Example #20
0
    def __init__(
        self,
        icon,
        hover_text,
        static_options,
        left_click_callback=None,
        addition_menu_callback=None,
        on_restart=None,
        default_menu_index=None,
        window_class_name=None,
    ):

        self.icon = icon
        self.hover_text = hover_text
        self.on_restart = on_restart
        self.LCC = left_click_callback
        self.AMC = addition_menu_callback

        self._next_action_id = self.FIRST_ID
        self.static_actions = dict()
        self.static_options = self._add_ids_to_menu_options(
            list(static_options), self.static_actions)

        self.default_menu_index = (default_menu_index or 0)
        self.window_class_name = window_class_name or "SysTrayIconPy"

        message_map = {
            win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
            win32con.WM_DESTROY: self.destroy,
            win32con.WM_COMMAND: self.command,
            win32con.WM_USER + 20: self.notify,
        }
        # Register the Window class.
        window_class = win32gui.WNDCLASS()
        hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
        window_class.lpszClassName = self.window_class_name
        window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
        window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
        window_class.hbrBackground = win32con.COLOR_WINDOW
        window_class.lpfnWndProc = message_map  # could also specify a wndproc.
        classAtom = win32gui.RegisterClass(window_class)
        # Create the Window.
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
        self.hwnd = win32gui.CreateWindow(classAtom, self.window_class_name,
                                          style, 0, 0, win32con.CW_USEDEFAULT,
                                          win32con.CW_USEDEFAULT, 0, 0, hinst,
                                          None)
        win32gui.UpdateWindow(self.hwnd)
        self.notify_id = None
        self.refresh_icon()
Example #21
0
    def refresh_icon(self):
      # Try and find a custom icon
      hinst = win32gui.GetModuleHandle(None)
      if isinstance(self.icon, str) and os.path.isfile(self.icon):
        icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
        hicon = win32gui.LoadImage(hinst, self.icon, win32con.IMAGE_ICON, 0, 0, icon_flags)
      else:
        hicon = win32gui.LoadIcon(hinst, int(self.icon))

      if self.notify_id: message = win32gui.NIM_MODIFY
      else: message = win32gui.NIM_ADD
      self.notify_id = (self.hwnd, 0,
        win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
         win32con.WM_USER+20, hicon, self.hover_text)
      win32gui.Shell_NotifyIcon(message, self.notify_id)
    def get_icon(self, path):
        hicon = self.icons.get(path)
        if hicon != None: return hicon

        # Try and find a custom icon
        hinst = win32gui.GetModuleHandle(None)
        if os.path.isfile(path):
            icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
            hicon = win32gui.LoadImage(hinst, path, win32con.IMAGE_ICON, 0, 0,
                                       icon_flags)
        else:
            print "Can't find icon file - using default."
            hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)

        self.icons[path] = hicon
        return hicon
Example #23
0
 def __init__(self,
              icon,
              hover_text,
              menu_cb,
              on_quit=None,
              default_menu_index=None,
              window_class_name=None,):
     
     self.icon = icon
     self.hover_text = hover_text
     self.on_quit = on_quit
     
     self.menu_cb = menu_cb
     self.default_menu_index = (default_menu_index or 0)
     self.window_class_name = window_class_name or "SysTrayIconPy"
     
     message_map = {win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
                    win32con.WM_DESTROY: self.destroy,
                    win32con.WM_COMMAND: self.command,
                    win32con.WM_USER+20 : self.notify,}
     # Register the Window class.
     window_class = win32gui.WNDCLASS()
     hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
     window_class.lpszClassName = self.window_class_name
     window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
     window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
     window_class.hbrBackground = win32con.COLOR_WINDOW
     window_class.lpfnWndProc = message_map # could also specify a wndproc.
     classAtom = win32gui.RegisterClass(window_class)
     # Create the Window.
     style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
     self.hwnd = win32gui.CreateWindow(classAtom,
                                       self.window_class_name,
                                       style,
                                       0,
                                       0,
                                       win32con.CW_USEDEFAULT,
                                       win32con.CW_USEDEFAULT,
                                       0,
                                       0,
                                       hinst,
                                       None)
     win32gui.UpdateWindow(self.hwnd)
     self.notify_id = None
     self.refresh_icon()
     
     win32gui.PumpMessages()
Example #24
0
File: win32.py Project: djon3s/APAF
    def __init__(self, reactor, callback):
        self.icon = os.path.join(config.drawable_dir, 'systray.ico')
        self.hover_text = config.description
        self.window_class_name = config.appname
        self.default_menu_index = 1
        self.reactor = reactor

        self.menu_actions_by_id = dict()
        self._next_action_id = 0
        self.SPECIAL_ACTIONS = list()
        self.menu_options = self._add_ids_to_menu_options((
            ('Open Panel service in Browser', None, base.open_panel_browser),
            # services list submenu
            ('Services', None, (('Fooo', None, self.bye), ('bar', None,
                                                           self.bye))),
            ('Quit', None, self.bye),
        ))

        message_map = {
            win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
            win32con.WM_DESTROY: self.destroy,
            win32con.WM_COMMAND: self.command,
            win32con.WM_USER + 20: self.notify,
        }
        # Register the Window class.
        window_class = win32gui.WNDCLASS()
        hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
        window_class.lpszClassName = self.window_class_name
        window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
        window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
        window_class.hbrBackground = win32con.COLOR_WINDOW
        window_class.lpfnWndProc = message_map  # could also specify a wndproc.
        classAtom = win32gui.RegisterClass(window_class)
        # Create the Window.
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
        self.hwnd = win32gui.CreateWindow(classAtom, self.window_class_name,
                                          style, 0, 0, win32con.CW_USEDEFAULT,
                                          win32con.CW_USEDEFAULT, 0, 0, hinst,
                                          None)
        win32gui.UpdateWindow(self.hwnd)
        self.notify_id = None
        self.refresh_icon()

        ## finally start the reactor
        callback()
        reactor.run()
        win32gui.PumpMessages()
Example #25
0
 def __init__(self):
     self.window_class = win32gui.WNDCLASS()
     self.hinstance = self.window_class.hInstance = win32gui.GetModuleHandle(
         None)
     self.window_class.lpszClassName = self.CLASS_NAME
     self.window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
     self.window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
     self.window_class.hbrBackground = win32con.COLOR_WINDOW
     self.window_class.lpfnWndProc = self.dispatch
     self.class_atom = win32gui.RegisterClass(self.window_class)
     self.hwnd = win32gui.CreateWindow(
         self.class_atom, self.CLASS_NAME,
         win32con.WS_OVERLAPPED | win32con.WS_SYSMENU, 0, 0,
         win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0,
         self.hinstance, None)
     win32gui.UpdateWindow(self.hwnd)
     self.set_icon()
    def __init__(self, autostart):
        self.autostart = autostart
        self.thread_id = win32api.GetCurrentThreadId()
        if not self.autostart:
            launch(self)
        name = "Open Media Library"
        self.icon = "ico/oml.ico"
        self.hover_text = name
        self.on_quit = quit

        menu_options = (('Open Media Library', None, None),
                        ('Launch', None, launch), ('Quit', None, self.QUIT))
        self._next_action_id = self.FIRST_ID
        self.menu_actions_by_id = set()
        self.menu_options = self._add_ids_to_menu_options(list(menu_options))
        self.menu_actions_by_id = dict(self.menu_actions_by_id)
        del self._next_action_id

        self.default_menu_index = 1
        self.window_class_name = name

        message_map = {
            win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
            win32con.WM_DESTROY: self.destroy,
            win32con.WM_COMMAND: self.command,
            win32con.WM_USER + 20: self.notify,
        }
        # Register the Window class.
        window_class = win32gui.WNDCLASS()
        hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
        window_class.lpszClassName = self.window_class_name
        window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
        window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
        window_class.hbrBackground = win32con.COLOR_WINDOW
        window_class.lpfnWndProc = message_map  # could also specify a wndproc.
        classAtom = win32gui.RegisterClass(window_class)
        # Create the Window.
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
        self.hwnd = win32gui.CreateWindow(classAtom, self.window_class_name,
                                          style, 0, 0, win32con.CW_USEDEFAULT,
                                          win32con.CW_USEDEFAULT, 0, 0, hinst,
                                          None)
        win32gui.UpdateWindow(self.hwnd)
        self.notify_id = None
        self.refresh_icon()
        win32gui.PumpMessages()
Example #27
0
    def refresh_icon(self):
        # Try and find a custom icon
        hinst = win32gui.GetModuleHandle(None)
        if os.path.isfile(self.icon):
            icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
            hicon = win32gui.LoadImage(hinst, self.icon, win32con.IMAGE_ICON,
                                       0, 0, icon_flags)
        else:
            raise Exception("winsystray: Can't find icon file.")
            hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)

        if self.notify_id: message = win32gui.NIM_MODIFY
        else: message = win32gui.NIM_ADD
        self.notify_id = (self.hwnd, 0, win32gui.NIF_ICON
                          | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
                          win32con.WM_USER + 20, hicon, self.hover_text)
        win32gui.Shell_NotifyIcon(message, self.notify_id)
    def __init__(
        self,
        keyboard_handler,
        icon,
        hover_text,
        menu_options,
        on_quit=None,
        default_menu_index=None,
        window_class_name=None,
    ):

        threading.Thread.__init__(self)

        self.icon = icon
        self.hover_text = hover_text
        self.on_quit = on_quit
        self.KbH = keyboard_handler

        menu_options = menu_options + (('Quit', None, self.QUIT), )
        self._next_action_id = self.FIRST_ID
        self.menu_actions_by_id = set()
        self.menu_options = self._add_ids_to_menu_options(list(menu_options))
        self.menu_actions_by_id = dict(self.menu_actions_by_id)
        del self._next_action_id

        self.default_menu_index = (default_menu_index or 0)
        self.window_class_name = window_class_name or "SysTrayIconPy"

        message_map = {
            win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
            win32con.WM_DESTROY: self.destroy,
            win32con.WM_COMMAND: self.command,
            win32con.WM_USER + 20: self.notify,
        }
        # Register the Window class.
        window_class = win32gui.WNDCLASS()
        self.hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
        window_class.lpszClassName = self.window_class_name
        window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
        window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
        window_class.hbrBackground = win32con.COLOR_WINDOW
        window_class.lpfnWndProc = message_map  # could also specify a wndproc.
        self.classAtom = win32gui.RegisterClass(window_class)
Example #29
0
	def __init__(self,
				 icon,
				 hover_text,
				 menu_options,
				 on_quit=None,
				 default_menu_index=None,
				 window_class_name=None,
				 data_feedback=None,
				 extra_icon_paths=None):
		
		self.icon = icon
		self.hover_text = hover_text
		self.on_quit = on_quit
		self.extra_icon_paths = extra_icon_paths
		
		menu_options = menu_options + (('Quit', None, self.QUIT),)
		self._next_action_id = self.FIRST_ID
		self.menu_actions_by_id = set()
		self.menu_options = self._add_ids_to_menu_options(list(menu_options))
		self.menu_actions_by_id = dict(self.menu_actions_by_id)
		del self._next_action_id
		
		
		self.default_menu_index = (default_menu_index or 0)
		self.window_class_name = window_class_name or "SysTrayIconPy"
		
		# This dict sets up a coorespondance from received win32gui messages (in the form of standard integer constants as defined in win32con)
		#   and the desired action to take upon receiving the message.
		#   WM_USER is the beginning of a reserved block of user-definable messages.
		message_map = {win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
					   win32con.WM_DESTROY: self.destroy,
					   win32con.WM_COMMAND: self.command,
					   win32con.WM_USER+self.OFFSET : self.notify,
					   win32con.WM_USER+self.OFFSET+1 : self.change_icon,}
		# Register the Window class.
		window_class = win32gui.WNDCLASS()
		hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
		window_class.lpszClassName = self.window_class_name
		window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
		window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
		window_class.hbrBackground = win32con.COLOR_WINDOW
		window_class.lpfnWndProc = message_map # could also specify a wndproc.
		classAtom = win32gui.RegisterClass(window_class)
		# Create the Window.
		style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
		self.hwnd = win32gui.CreateWindow(classAtom,
										  self.window_class_name,
										  style,
										  0,
										  0,
										  win32con.CW_USEDEFAULT,
										  win32con.CW_USEDEFAULT,
										  0,
										  0,
										  hinst,
										  None)
		win32gui.UpdateWindow(self.hwnd)
		self.notify_id = None
		self.refresh_icon()
		
		# Send data to calling script. Used to retrieve hwnd in order to send messages.
		if(data_feedback): data_feedback(self)
		
		# Runs the main loop that waits for messages in this thread
		win32gui.PumpMessages()
Example #30
0
    def __init__(
        self,
        icon,
        hover_text,
        menu_options,
        on_quit=None,
        default_menu_index=None,
        window_class_name=None,
    ):

        self.icon = icon
        self.hover_text = hover_text
        self.on_quit = on_quit

        menu_options = menu_options + (('Quit', None, self.QUIT), )
        self._next_action_id = self.FIRST_ID
        self.menu_actions_by_id = set()
        self.menu_options = self._add_ids_to_menu_options(list(menu_options))
        self.menu_actions_by_id = dict(self.menu_actions_by_id)
        del self._next_action_id

        self.default_menu_index = (default_menu_index or 0)
        self.window_class_name = window_class_name or "SysTrayIconPy"

        message_map = {
            win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
            win32con.WM_DESTROY: self.destroy,
            win32con.WM_COMMAND: self.command,
            # owner-draw related handlers.
            win32con.WM_MEASUREITEM: self.OnMeasureItem,
            win32con.WM_DRAWITEM: self.OnDrawItem,
            win32con.WM_USER + 20: self.notify,
        }
        # Register the Window class.
        window_class = win32gui.WNDCLASS()
        hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
        window_class.lpszClassName = self.window_class_name
        window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
        window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
        window_class.hbrBackground = win32con.COLOR_WINDOW
        window_class.lpfnWndProc = message_map  # could also specify a wndproc.
        classAtom = win32gui.RegisterClass(window_class)
        # Create the Window.
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
        self.hwnd = win32gui.CreateWindow(classAtom, self.window_class_name,
                                          style, 0, 0, win32con.CW_USEDEFAULT,
                                          win32con.CW_USEDEFAULT, 0, 0, hinst,
                                          None)
        win32gui.UpdateWindow(self.hwnd)
        self.notify_id = None
        self.refresh_icon()

        # Load up some information about menus needed by our owner-draw code.
        # The font to use on the menu.
        ncm = win32gui.SystemParametersInfo(win32con.SPI_GETNONCLIENTMETRICS)
        self.font_menu = win32gui.CreateFontIndirect(ncm['lfMenuFont'])
        # spacing for our ownerdraw menus - not sure exactly what constants
        # should be used (and if you owner-draw all items on the menu, it
        # doesn't matter!)
        self.menu_icon_height = win32api.GetSystemMetrics(
            win32con.SM_CYMENU) - 4
        self.menu_icon_width = self.menu_icon_height
        self.icon_x_pad = 8  # space from end of icon to start of text.
        # A map we use to stash away data we need for ownerdraw.  Keyed
        # by integer ID - that ID will be set in dwTypeData of the menu item.
        self.menu_item_map = {}

        win32gui.PumpMessages()