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)
Esempio n. 2
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
Esempio n. 3
0
    def _RegisterWndClass(self):
        message_map = {}
        wc = win32gui.WNDCLASS()
        wc.SetDialogProc()  # Make it a dialog class.
        wc.hInstance = self.hinst
        wc.lpszClassName = self.className
        wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
        wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
        wc.hbrBackground = win32con.COLOR_WINDOW + 1
        wc.lpfnWndProc = message_map  # could also specify a wndproc.

        # C code: wc.cbWndExtra = DLGWINDOWEXTRA + sizeof(HBRUSH) +
        #                         (sizeof(COLORREF));
        wc.cbWndExtra = win32con.DLGWINDOWEXTRA + struct.calcsize("Pi")

        #  py.ico went away in python 2.5, load from executable instead
        this_app = win32api.GetModuleHandle(None)
        try:
            # python.exe and pythonw.exe
            wc.hIcon = win32gui.LoadIcon(this_app, 1)
        except win32gui.error:
            # pythonwin's icon
            wc.hIcon = win32gui.LoadIcon(this_app, 135)
        try:
            self.classAtom = win32gui.RegisterClass(wc)
        except win32gui.error, err_info:
            if err_info.winerror != winerror.ERROR_CLASS_ALREADY_EXISTS:
                raise
Esempio n. 4
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)
Esempio n. 5
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
Esempio n. 6
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)  # 添加、移除或改变任务栏图标
Esempio n. 7
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)
Esempio n. 8
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)
Esempio n. 9
0
 def __init__(self):
     message_map = {
         win32con.WM_DESTROY: self.OnDestroy,
         win32con.WM_COMMAND: self.OnCommand,
         win32con.WM_USER + 20: self.OnTaskbarNotify,
     }
     # Register the Window class.
     wc = gui.WNDCLASS()
     hinst = wc.hInstance = api.GetModuleHandle(None)
     wc.lpszClassName = "IdlerPopupHandler"
     wc.lpfnWndProc = message_map  # could also specify a wndproc.
     classAtom = gui.RegisterClass(wc)
     self.classAtom = classAtom
     # Create the Window.
     style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
     self.hwnd = gui.CreateWindow( classAtom, "Idling Notifications", style,  \
            0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,     \
            0, 0, hinst, None)
     gui.UpdateWindow(self.hwnd)
     iconPathName = os.path.abspath(os.path.join(sys.prefix, "pyc.ico"))
     icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
     try:
         hicon = gui.LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0,
                               0, icon_flags)
     except:
         hicon = gui.LoadIcon(0, win32con.IDI_APPLICATION)
     flags = gui.NIF_ICON | gui.NIF_MESSAGE | gui.NIF_TIP
     nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon,
            "Idling Notifications")
     gui.Shell_NotifyIcon(gui.NIM_ADD, nid)
Esempio n. 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:
			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
Esempio n. 11
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
Esempio n. 12
0
 def toast(self, title='', info='', levelname=None):
     if not self.enable_notifications.checked:
         return
     icon_map = {
         # 'INFO': win32con.IDI_INFORMATION,
         'WARNING': win32con.IDI_WARNING,
         'ERROR': win32con.IDI_ERROR,
         'CRITICAL': win32con.IDI_ERROR,
     }
     icon_id = icon_map.get(levelname)
     hicon = win32gui.LoadIcon(0, icon_id) if icon_id else None
     self.draw_notify_icon(title=title, info=info, hicon=hicon)
Esempio n. 13
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
Esempio n. 14
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)
Esempio n. 15
0
    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
Esempio n. 16
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)
Esempio n. 17
0
    def refresh_icon(self):
        # Try and find a custom icon
        #hinst = win32gui.GetModuleHandle(None)
        #TODO: also put this in NotifyIcon class
        if os.path.isfile(self._icon):
            icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
            self.hicon = win32gui.LoadImage(self.window_class.hInstance, #hinst, 
                                       self._icon, 
                                       win32con.IMAGE_ICON, 
                                       0, 
                                       0, 
                                       icon_flags)
        else:
            self.hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)

        self.notify_icon.hicon = self.hicon
        self.notify_icon.tooltip = self.hover_text
        self.notify_icon.show()
Esempio n. 18
0
 def set_icon(self, *_args):
     win32gui.Shell_NotifyIcon(
         win32gui.NIM_ADD,
         (self.hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE
          | win32gui.NIF_TIP, win32con.WM_USER + 20,
          win32gui.LoadIcon(self.hinstance, 1), self.HOVER_TEXT))