def make_ICONINFO(w, h, rgb_data, rgb_format="BGRA"): log("make_ICONINFO(%i, %i, %i bytes, %s)", w, h, len(rgb_data), rgb_format) bitmap = 0 mask = 0 try: bytes_per_pixel = len(rgb_format) bitmap = rgb_to_bitmap(rgb_data, bytes_per_pixel, w, h) log("rgb_to_bitmap(%i bytes, %i, %i, %i)=%s", len(rgb_data), bytes_per_pixel, w, h, bitmap) mask = CreateBitmap(w, h, 1, 1, None) log("CreateBitmap(%i, %i, 1, 1, None)=%#x", w, h, mask or 0) if not mask: raise ctypes.WinError(ctypes.get_last_error()) iconinfo = ICONINFO() iconinfo.fIcon = True iconinfo.hbmMask = mask iconinfo.hbmColor = bitmap hicon = CreateIconIndirect(byref(iconinfo)) log("CreateIconIndirect()=%#x", hicon or 0) if not hicon: raise ctypes.WinError(ctypes.get_last_error()) return hicon except Exception: log.error("Error: failed to set tray icon", exc_info=True) return FALLBACK_ICON finally: if mask: DeleteObject(mask) if bitmap: DeleteObject(bitmap)
def set_icon_from_data(self, pixels, has_alpha, w, h, rowstride, options={}): #this is convoluted but it works.. log("set_icon_from_data%s", ("%s pixels" % len(pixels), has_alpha, w, h, rowstride, options)) from PIL import Image #@UnresolvedImport if has_alpha: img_format = "RGBA" else: img_format = "RGBX" rgb_format = options.get("rgb_format", "RGBA") img = Image.frombuffer(img_format, (w, h), pixels, "raw", rgb_format, rowstride, 1) assert img, "failed to load image from buffer (%i bytes for %ix%i %s)" % ( len(pixels), w, h, rgb_format) #apparently, we have to use SM_CXSMICON (small icon) and not SM_CXICON (regular size): icon_w = GetSystemMetrics(win32con.SM_CXSMICON) icon_h = GetSystemMetrics(win32con.SM_CYSMICON) if w != icon_w or h != icon_h: log("resizing tray icon to %ix%i", icon_w, icon_h) img = img.resize((w, h), Image.ANTIALIAS) bitmap = 0 mask = 0 try: from xpra.codecs.argb.argb import rgba_to_bgra #@UnresolvedImport bgra = memoryview_to_bytes(rgba_to_bgra(img.tobytes())) bitmap = rgba_to_bitmap(bgra, icon_w, icon_h) mask = CreateBitmap(icon_w, icon_h, 1, 1, None) iconinfo = ICONINFO() iconinfo.fIcon = True iconinfo.hbmMask = mask iconinfo.hbmColor = bitmap hicon = CreateIconIndirect(byref(iconinfo)) log("CreateIconIndirect()=%s", hicon) if not hicon: raise ctypes.WinError(ctypes.get_last_error()) except Exception: log.error("Error: failed to set tray icon", exc_info=True) hicon = FALLBACK_ICON finally: if mask: DeleteObject(mask) if bitmap: DeleteObject(bitmap) self.do_set_icon(hicon) UpdateWindow(self.hwnd) self.reset_function = (self.set_icon_from_data, pixels, has_alpha, w, h, rowstride)
def make_ICONINFO(w, h, bgra): bitmap = 0 mask = 0 try: bitmap = rgba_to_bitmap(bgra, w, h) mask = CreateBitmap(w, h, 1, 1, None) iconinfo = ICONINFO() iconinfo.fIcon = True iconinfo.hbmMask = mask iconinfo.hbmColor = bitmap hicon = CreateIconIndirect(byref(iconinfo)) log("CreateIconIndirect()=%#x", hicon) if not hicon: raise ctypes.WinError(ctypes.get_last_error()) return hicon except Exception: log.error("Error: failed to set tray icon", exc_info=True) return FALLBACK_ICON finally: if mask: DeleteObject(mask) if bitmap: DeleteObject(bitmap)