Exemple #1
0
def read_icon(handle, icon):
    must_use_qt()
    resource = win32api.LoadResource(handle, win32con.RT_ICON, icon.id)
    pixmap = QPixmap()
    pixmap.loadFromData(resource)
    hicon = None
    if pixmap.isNull():
        if icon.width > 0 and icon.height > 0:
            hicon = ctypes.windll.user32.CreateIconFromResourceEx(
                resource, len(resource), True, 0x00030000, icon.width, icon.height, win32con.LR_DEFAULTCOLOR)
        else:
            hicon = win32gui.CreateIconFromResource(resource, True)
        pixmap = hicon_to_pixmap(hicon).copy()
        win32gui.DestroyIcon(hicon)
    return pixmap
Exemple #2
0
    def _DoCreateIcons(self):
        # Try and find a custom icon
        hinst = win32api.GetModuleHandle(None)

        hicon = win32gui.CreateIconFromResource(
            win32api.LoadResource(None, win32con.RT_ICON, 1), True)

        flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP
        nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon,
               "Python Demo")
        try:
            win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
        except win32gui.error:
            # This is common when windows is starting, and this code is hit
            # before the taskbar has been created.
            print "Failed to add the taskbar icon - is explorer running?"
Exemple #3
0
    def setMode(self,
                resolution,
                fullscreen=False,
                flags=pygame.OPENGL | pygame.DOUBLEBUF,
                multisamples=0):
        if fullscreen:
            flags |= pygame.FULLSCREEN

        self.flags = flags
        self.fullscreen = fullscreen
        self.multisamples = multisamples

        try:
            pygame.display.quit()
        except Exception:
            pass

        pygame.display.init()

        pygame.display.gl_set_attribute(pygame.GL_RED_SIZE, 8)
        pygame.display.gl_set_attribute(pygame.GL_GREEN_SIZE, 8)
        pygame.display.gl_set_attribute(pygame.GL_BLUE_SIZE, 8)
        pygame.display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8)

        if multisamples:
            pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1)
            pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES,
                                            multisamples)

        # evilynux - Setting window icon for platforms other than MS Windows.
        #            pygame claims that some window manager requires this to be
        #            before display.set_mode(), so there it is!
        #            Note: For the MS Windows icon, see below.
        if not os.name == "nt" and self.icon is not None:
            pygame.display.set_icon(pygame.image.load(self.icon))

        try:
            self.screen = pygame.display.set_mode(resolution, flags)
        except Exception as e:
            errortype = str(e)
            if "video mode" in errortype:
                self.resolutionReset()
            else:  # "Couldn't find matching GLX visual"
                self.multisampleReset(resolution)

        pygame.display.set_caption(self.caption)
        pygame.mouse.set_visible(False)

        #stump: fix the window icon under Windows
        # We would use pygame.display.set_icon(), but due to what appears to be
        # a bug in SDL, the alpha channel is lost and some parts of the image are
        # corrupted.  As a result, we go the long way and load and set the icon
        # by hand to work around the bug.
        if os.name == 'nt' and self.icon:
            import win32api
            import win32gui
            import win32con
            hwnd = pygame.display.get_wm_info()['window']

            # The Windows icon functions want the byte order in memory to be "BGRx"
            # for some reason.  Use the alpha channel as a placeholder for the "x"
            # and swap the channels to fit.  Also turn the image upside down as the
            # API wants.
            icon = Image.open(self.icon)
            icon.load()  #fculpo - needed for PIL 1.1.7 stable
            iconFixedUp = Image.merge('RGBA',
                                      [icon.split()[i]
                                       for i in (2, 1, 0, 3)]).transpose(
                                           Image.FLIP_TOP_BOTTOM)

            # Scale the images to the icon sizes needed.
            bigIcon = iconFixedUp.resize((32, 32), Image.BICUBIC)
            smallIcon = iconFixedUp.resize((16, 16), Image.BICUBIC)

            # The icon resources hold two bitmaps: the first is 32-bit pixel data
            # (which for some reason doesn't hold the alpha, though we used the alpha
            # channel to fill up that space - the fourth channel is ignored) and the
            # second is a 1-bit alpha mask.  For some reason, we need to invert the
            # alpha channel before turning it into the mask.
            bigIconColorData = bigIcon.tobytes()
            bigIconMaskData = bigIcon.split()[3].point(
                lambda p: 255 - p).convert('1').tobytes()
            smallIconColorData = smallIcon.tobytes()
            smallIconMaskData = smallIcon.split()[3].point(
                lambda p: 255 - p).convert('1').tobytes()

            # Put together icon resource structures - a header, then the pixel data,
            # then the alpha mask.  See documentation for the BITMAPINFOHEADER
            # structure for what the fields mean.  Not all fields are used -
            # http://msdn.microsoft.com/en-us/library/ms997538.aspx says which ones
            # don't matter and says to set them to zero.  We double the height for
            # reasons mentioned on that page, too.
            bigIconData = struct.pack('<LllHHLLllLL', 40, 32, 64, 1, 32, 0,
                                      len(bigIconColorData + bigIconMaskData),
                                      0, 0, 0,
                                      0) + bigIconColorData + bigIconMaskData
            smallIconData = struct.pack(
                '<LllHHLLllLL', 40, 16, 32, 1, 32, 0,
                len(smallIconColorData + smallIconMaskData), 0, 0, 0,
                0) + smallIconColorData + smallIconMaskData

            # Finally actually create the icons from the icon resource structures.
            hIconBig = win32gui.CreateIconFromResource(bigIconData, True,
                                                       0x00030000)
            hIconSmall = win32gui.CreateIconFromResource(
                smallIconData, True, 0x00030000)

            # And set the window's icon to our fresh new icon handles.
            win32api.SendMessage(hwnd, win32con.WM_SETICON, win32con.ICON_BIG,
                                 hIconBig)
            win32api.SendMessage(hwnd, win32con.WM_SETICON,
                                 win32con.ICON_SMALL, hIconSmall)

        if multisamples:
            try:
                glEnable(GL_MULTISAMPLE_ARB)
            except Exception:
                pass

        return bool(self.screen)
Exemple #4
0
    def __call__(self,
                 title="",
                 msg="",
                 payload=None,
                 iconOpt=ICON_EG,
                 sound=True):
        """
        Show a tip balloon in the Windows Event Center.

        title: Bold text to show in the title of the tip.
        msg: Detail text to show in the tip.
        payload: Python data to include with events triggered from this tip.
        iconOpt: an int or a string:
          0 = No icon
          1 = Info icon
          2 = Warning icon
          3 = Error icon
          4 = EventGhost icon
          string = *full path* to an icon file, a semicolon, and the icon number
            (eg: "C:\\Windows\\system32\\shell32.dll;13")
        sound: Whether to play the notification sound.
        """
        if iconOpt is None:
            iconOpt = self.ICON_EG
        title = eg.ParseString(title or "EventGhost")
        msg = eg.ParseString(msg or "This is a notification from EventGhost.")
        if payload and isinstance(payload, basestring):
            payload = eg.ParseString(payload)

        # https://stackoverflow.com/a/17262942/6692652
        # Create the window.
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
        hwnd = win32gui.CreateWindow(self.plugin.classAtom, "TaskBar", style,
                                     0, 0, win32con.CW_USEDEFAULT,
                                     win32con.CW_USEDEFAULT, 0, 0,
                                     self.plugin.hinst, None)
        win32gui.UpdateWindow(hwnd)
        self.plugin.setPayload(hwnd, payload)

        # Icons management
        # Default to no icon if something goes weird
        hicon = None
        dwInfoFlags = 0x00
        try:
            if iconOpt == self.ICON_INFO:
                dwInfoFlags = NIIF_INFO | NIIF_LARGE_ICON
            elif iconOpt == self.ICON_WARNING:
                dwInfoFlags = NIIF_WARNING | NIIF_LARGE_ICON
            elif iconOpt == self.ICON_ERROR:
                dwInfoFlags = NIIF_ERROR | NIIF_LARGE_ICON
            elif iconOpt == self.ICON_EG:
                # Get the first icon from the EventGhost executable
                hicon = win32gui.CreateIconFromResource(
                    win32api.LoadResource(None, win32con.RT_ICON, 1), True)
                dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON
            elif isinstance(iconOpt, basestring):
                filename, idx = iconOpt.split(";", 1)
                filename = expandvars(filename)
                dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON
                if filename[-4:].upper() == ".ICO":
                    hicon = win32gui.LoadImage(
                        win32api.GetModuleHandle(None),
                        filename,
                        win32con.IMAGE_ICON,
                        0,
                        0,
                        win32con.LR_LOADFROMFILE | win32con.LR_LOADREALSIZE,
                    )
                else:
                    lib = win32api.LoadLibrary(filename)
                    hicon = win32gui.LoadIcon(lib, int(idx) + 1)
                    win32api.FreeLibrary(lib)
        except Exception as ex:
            eg.PrintError(str(ex))
            hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
            dwInfoFlags = 0x00
        if not sound:
            dwInfoFlags |= NIIF_NOSOUND
        flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP
        nid = (hwnd, 0, flags, WM_TRAYICON, hicon, 'Tooltip')

        # Notify
        win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
        win32gui.Shell_NotifyIcon(
            win32gui.NIM_MODIFY,
            (hwnd, 0, win32gui.NIF_INFO, WM_TRAYICON, hicon, 'Balloon Tooltip',
             msg, 200, title, dwInfoFlags))
        if hicon is not None:
            win32gui.DestroyIcon(hicon)