Question

Je cherche un moyen d'extraire une icône d'un fichier .exe à l'aide de Python. Je sais que vous pouvez utiliser la fonction ExtractIconEx de win32gui pour récupérer l’icône d’un fichier .exe, mais elle renvoie un descripteur de ressource HIcon qui n’est pas utile car je souhaite peindre l’icône à l’aide de PyQt.

De plus, le seul exemple que j'ai vu avec win32gui n'a aucune transparence et les icônes ne sont pas lisses.

Quelle serait la meilleure façon de procéder en utilisant Python & amp; PyQt?

- Modifier -

Merci à Luk & # 225; & # 353; Lalinsk & # 253; ce problème est maintenant résolu, voici le code final si quelqu'un cherche à faire quelque chose de similaire à moi:

import sys
import win32ui
import win32gui
from PyQt4 import QtCore
from PyQt4 import QtGui

class testWindow(QtGui.QMainWindow):
    def __init__(self):
        super(testWindow, self).__init__()
        self.setGeometry(180.0, 130.0, 280.0, 400.0)
        self.setMouseTracking(True)

        large, small = win32gui.ExtractIconEx('C:\\Users\\Blank\\Apps\\Web Browsers\\Firefox\\Firefox.exe', 0)
        win32gui.DestroyIcon(small[0])

        self.pixmap = QtGui.QPixmap.fromWinHBITMAP(self.bitmapFromHIcon(large[0]), 2)
    def bitmapFromHIcon(self, hIcon):
        hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
        hbmp = win32ui.CreateBitmap()
        hbmp.CreateCompatibleBitmap(hdc, 32, 32)
        hdc = hdc.CreateCompatibleDC()
        hdc.SelectObject(hbmp)
        hdc.DrawIcon((0, 0), hIcon)
        hdc.DeleteDC()
        return hbmp.GetHandle()
    def paintEvent(self, event):
        painter = QtGui.QPainter()
        painter.begin(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setPen(QtCore.Qt.NoPen)
        painter.setBrush(QtGui.QBrush(QtGui.QColor(255.0, 255.0, 255.0, 255.0), QtCore.Qt.SolidPattern))
        painter.drawRect(QtCore.QRect(0.0, 0.0, 280.0, 400.0))
        painter.drawPixmap(QtCore.QRect(0.0, 0.0, 32.0, 32.0), self.pixmap)
        painter.end()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    mainWindow = testWindow()
    mainWindow.show()
    app.exec_()
Était-ce utile?

La solution

Il existe une méthode pour créer QPixmap à partir d'un HBITMAP , le seul problème est donc de savoir comment convertir HICON en HBITMAP . Cela peut être fait en utilisant GetIconInfo .

icons = win32gui.ExtractIconEx('C:/Program Files/Internet Explorer/iexplore.exe', 0, 10)
info = win32gui.GetIconInfo(icons[0][0])
pixmap = QtGui.QPixmap.fromWinHBITMAP(info[4])
info[3].close()
info[4].close()
# call win32gui.DestroyIcon on all the icons returned by ExtractIconEx

MODIFIER: ce code ne vous aidera pas avec l'antialiasing et le canal alpha. Votre nouveau code est presque correct, mais vous devez dire à Qt de charger le canal alpha. Si vous remplacez:

self.pixmap = QtGui.QPixmap.fromWinHBITMAP(self.bitmapFromHIcon(large[0]))

avec:

self.pixmap = QtGui.QPixmap.fromWinHBITMAP(self.bitmapFromHIcon(large[0]), 2)

il fera la bonne chose. La "magie" Le numéro 2 doit être techniquement QtGui.QPixmap.Alpha mais pour une raison quelconque, Qt ne fournit pas la constante.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top