def set_mouse_platform_visible(self, platform_visible=None): if platform_visible is None: platform_visible = (self._mouse_visible and not self._exclusive_mouse and not self._mouse_cursor.drawable) or \ (not self._mouse_in_window or not self._has_focus) if platform_visible and not self._mouse_cursor.drawable: if isinstance(self._mouse_cursor, Win32MouseCursor): cursor = self._mouse_cursor.cursor else: cursor = _user32.LoadCursorW(None, MAKEINTRESOURCE(IDC_ARROW)) _user32.SetClassLongPtrW(self._view_hwnd, GCL_HCURSOR, cursor) _user32.SetCursor(cursor) if platform_visible == self._mouse_platform_visible: return # Avoid calling ShowCursor with the current visibility (which would # push the counter too far away from zero). global _win32_cursor_visible if _win32_cursor_visible != platform_visible: _user32.ShowCursor(platform_visible) _win32_cursor_visible = platform_visible self._mouse_platform_visible = platform_visible
def set_icon(self, *images): # XXX Undocumented AFAICT, but XP seems happy to resize an image # of any size, so no scaling necessary. def best_image(width, height): # A heuristic for finding closest sized image to required size. image = images[0] for img in images: if img.width == width and img.height == height: # Exact match always used return img elif img.width >= width and \ img.width * img.height > image.width * image.height: # At least wide enough, and largest area image = img return image def get_icon(image): # Alpha-blended icon: see http://support.microsoft.com/kb/318876 format = 'BGRA' pitch = len(format) * image.width header = BITMAPV5HEADER() header.bV5Size = sizeof(header) header.bV5Width = image.width header.bV5Height = image.height header.bV5Planes = 1 header.bV5BitCount = 32 header.bV5Compression = BI_BITFIELDS header.bV5RedMask = 0x00ff0000 header.bV5GreenMask = 0x0000ff00 header.bV5BlueMask = 0x000000ff header.bV5AlphaMask = 0xff000000 hdc = _user32.GetDC(None) dataptr = c_void_p() bitmap = _gdi32.CreateDIBSection(hdc, byref(header), DIB_RGB_COLORS, byref(dataptr), None, 0) _user32.ReleaseDC(None, hdc) image = image.get_image_data() data = image.get_data(format, pitch) memmove(dataptr, data, len(data)) mask = _gdi32.CreateBitmap(image.width, image.height, 1, 1, None) iconinfo = ICONINFO() iconinfo.fIcon = True iconinfo.hbmMask = mask iconinfo.hbmColor = bitmap icon = _user32.CreateIconIndirect(byref(iconinfo)) _gdi32.DeleteObject(mask) _gdi32.DeleteObject(bitmap) return icon # Set large icon image = best_image(_user32.GetSystemMetrics(SM_CXICON), _user32.GetSystemMetrics(SM_CYICON)) icon = get_icon(image) _user32.SetClassLongPtrW(self._hwnd, GCL_HICON, icon) # Set small icon image = best_image(_user32.GetSystemMetrics(SM_CXSMICON), _user32.GetSystemMetrics(SM_CYSMICON)) icon = get_icon(image) _user32.SetClassLongPtrW(self._hwnd, GCL_HICONSM, icon)