Exemplo n.º 1
0
    def prep_menu_icon(self, icon):
        """
		Ref: https://stackoverflow.com/a/45890829/973425
		:param icon:
		:return:
		"""
        # First load the icon.
        ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
        ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
        hIcon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y,
                                   win32con.LR_LOADFROMFILE)

        hwndDC = win32gui.GetWindowDC(self.hwnd)
        dc = win32ui.CreateDCFromHandle(hwndDC)
        memDC = dc.CreateCompatibleDC()
        iconBitmap = win32ui.CreateBitmap()
        iconBitmap.CreateCompatibleBitmap(dc, ico_x, ico_y)
        oldBmp = memDC.SelectObject(iconBitmap)
        brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU)

        win32gui.FillRect(memDC.GetSafeHdc(), (0, 0, ico_x, ico_y), brush)
        win32gui.DrawIconEx(memDC.GetSafeHdc(), 0, 0, hIcon, ico_x, ico_y, 0,
                            0, win32con.DI_NORMAL)

        memDC.SelectObject(oldBmp)
        memDC.DeleteDC()
        win32gui.ReleaseDC(self.hwnd, hwndDC)

        return iconBitmap.GetHandle()
Exemplo n.º 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
            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)
Exemplo n.º 3
0
 def prep_menu_icon(self, icon):
     # First load the icon.
     ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
     ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
     hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y,
                                win32con.LR_LOADFROMFILE)
     return hicon
Exemplo n.º 4
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
Exemplo n.º 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)
Exemplo n.º 6
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)
Exemplo 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)
Exemplo n.º 8
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
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    def prep_menu_icon(self, icon):
        # First load the icon.
        ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
        ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
        hicon = win32gui.LoadImage(
            0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE
        )

        hdcBitmap = win32gui.CreateCompatibleDC(0)
        hdcScreen = win32gui.GetDC(0)
        hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y)
        hbmOld = win32gui.SelectObject(hdcBitmap, hbm)
        # Fill the background.
        brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU)
        win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush)
        # unclear if brush needs to be feed.  Best clue I can find is:
        # "GetSysColorBrush returns a cached brush instead of allocating a new
        # one." - implies no DeleteObject
        # draw the icon
        win32gui.DrawIconEx(
            hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL
        )
        win32gui.SelectObject(hdcBitmap, hbmOld)
        win32gui.DeleteDC(hdcBitmap)

        return hbm
Exemplo n.º 11
0
	def prep_menu_icon(self, icon):
		# Version of prep_menu_icon using win32ui package
		# Source: https://stackoverflow.com/questions/45716730/add-image-in-window-tray-menu/45890829
		
		# First load the icon.
		ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
		ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
		hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE)

		hwndDC = win32gui.GetWindowDC(self.hwnd)
		dc = win32ui.CreateDCFromHandle(hwndDC)
		memDC = dc.CreateCompatibleDC()
		iconBitmap = win32ui.CreateBitmap()
		iconBitmap.CreateCompatibleBitmap(dc, ico_x, ico_y)
		oldBmp = memDC.SelectObject(iconBitmap)
		brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU)

		win32gui.FillRect(memDC.GetSafeHdc(), (0, 0, ico_x, ico_y), brush)
		win32gui.DrawIconEx(memDC.GetSafeHdc(), 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL)

		memDC.SelectObject(oldBmp)
		memDC.DeleteDC()
		win32gui.ReleaseDC(self.hwnd, hwndDC)

		return iconBitmap.GetHandle()
Exemplo n.º 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
			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
Exemplo n.º 13
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)  # 添加、移除或改变任务栏图标
Exemplo n.º 14
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
Exemplo n.º 15
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
Exemplo n.º 16
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)
Exemplo n.º 17
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
Exemplo n.º 18
0
    def prep_menu_icon(self, icon):
        # First load the icon.
        ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
        ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
        color = win32gui.GetSysColor(win32con.COLOR_MENU)
        icon = icon(ico_x, ico_y, color)

        if not os.path.exists(icon):
            print "WARNING: error loading icon {}".format(icon)
            return

        if icon.endswith('.bmp'):
            return win32gui.LoadImage(0, icon, win32con.IMAGE_BITMAP, ico_x,
                                      ico_y, win32con.LR_LOADFROMFILE)
        else:
            raise NotImplementedError("Require BMP")
Exemplo n.º 19
0
 def prep_menu_icon(self, icon):
     ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
     ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
     hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y,
                                win32con.LR_LOADFROMFILE)
     hdcBitmap = win32gui.CreateCompatibleDC(0)
     hdcScreen = win32gui.GetDC(0)
     hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y)
     hbmOld = win32gui.SelectObject(hdcBitmap, hbm)
     brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU)
     win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush)
     win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0,
                         win32con.DI_NORMAL)
     win32gui.SelectObject(hdcBitmap, hbmOld)
     win32gui.DeleteDC(hdcBitmap)
     return hbm
Exemplo n.º 20
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)
Exemplo n.º 21
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()