首页 > 解决方案 > 从外部应用程序获取图标

问题描述

我正在尝试准备一个类似于“Windows 开始菜单搜索”的应用程序。

这就是为什么我需要每个应用程序都有自己的图标。

C:\ProgramData\Start Menu\Programs\ 文件路径中,我将现有应用程序(QListWidget)及其名称和路径添加到列表中。

我得到这样的图标: https ://forum.qt.io/topic/62866/getting-icon-from-external-applications

provider = QFileIconProvider()
info = QFileInfo("program_path")
icon = QIcon(provider.icon(info))

结果自然是这样的: 图片 1

但我不希望出现这个“快捷方式图标”。

在此处输入图像描述

然后,我在想,我得出了这个结论:

shell = win32com.client.Dispatch("WScript.Shell")
provider = QFileIconProvider()
shortcut = shell.CreateShortCut(programPath)
info = QFileInfo(shortcut.targetPath)
icon = QIcon(provider.icon(info))

这个解决方案奏效了。但是,它为某些应用程序带来了问题。所以我正在寻找替代解决方案。

标签: pythonpython-3.xpyqtpyqt5

解决方案


你快到了。

浏览菜单目录树实际上是正确的路径,但您还必须确保链接的图标实际上与目标相同,因为它可能不一样。
shortcut.iconlocation是一个字符串,表示“元组”(某种),包括图标路径和索引(因为图标资源可能包含多个图标)。

>>> shortcut = shell.createShortCut(linkPath)
>>> print(shortcut.iconlocation)
# most links will return this:
> ",0"
# some might return this:
> ",4"
# or this:
> "C:\SomePath\SomeProgram\SomeExe.exe,5"

只要图标索引为 0,您就可以使用带有targetPath或的 QFileIconProvider 获取图标iconLocation(如果逗号前有东西)。

当图标索引的值不同于 0 时,问题就出现了,因为 Qt 没有处理这个问题。

我整理了一个简单的函数(基于 StackOverflow 上的一些研究

def getIcon(self, shortcut):
    iconPath, iconId = shortcut.iconLocation.split(',')
    iconId = int(iconId)
    if not iconPath:
        iconPath = shortcut.targetPath
    iconPath = os.path.expandvars(iconPath)
    if not iconId:
        return QICon(self.iconProvider.icon(QFileInfo(iconPath)))

    iconRes = win32gui.ExtractIconEx(iconPath, iconId)
    hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
    hbmp = win32ui.CreateBitmap()
    # I think there's a way to find available icon sizes, I'll leave it up to you
    hbmp.CreateCompatibleBitmap(hdc, 32, 32)
    hdc = hdc.CreateCompatibleDC()
    hdc.SelectObject(hbmp)
    hdc.DrawIcon((0, 0), iconRes[0][0])
    hdc.DeleteDC()
    # the original QtGui.QPixmap.fromWinHBITMAP is now part of the
    # QtWin sub-module
    return QtGui.QIcon(QtWin.fromWinHBITMAP(hbmp.GetHandle(), 2))

推荐阅读