使win32com覆盖图标示例工作

时间:2014-09-26 10:01:39

标签: python pywin32 win32com shell-icons

我尝试制作与Dropbox文件夹同步类似的内容,但我在添加叠加图标方面遇到了问题。我检查了以下指南:

http://timgolden.me.uk/python/win32_how_do_i/add-my-own-icon-overlays.html http://msdn.microsoft.com/en-us/library/bb776858%28VS.85%29.aspx?topic=306117

之后我对Golden的例子做了一些修改: 1.在IsMemberOf方法中我想在我的桌面中只有一个文件夹添加一个叠加图标,其中包含一个文件' kala.txt'。 2.在GetOverlayInfo方法中,我将路径更改为指向我下载的图标。

运行代码后,我检查了注册表,密钥就在那里,但图标不会显示。 我是在一个32位的Windows XP虚拟机上。

代码:

import os
from win32com.shell import shell, shellcon
import winerror

class IconOverlay:
    _reg_clsid_ = '{642A09BF-DE34-4251-A0C2-588CCE0DB935}'
    _reg_progid_ = 'TJG.PythonPackagesOverlayHandler'
    _reg_desc_ = 'Icon Overlay Handler to indicate Python packages'
    _public_methods_ = ['GetOverlayInfo', 'GetPriority', 'IsMemberOf']
    _com_interfaces_ = [shell.IID_IShellIconOverlayIdentifier]

    def GetOverlayInfo(self):
        return (r'C:\Users\Administrator\Downloads\netvibes.ico', 0, shellcon.ISIOI_ICONFILE)

    def GetPriority(self):
        return 1

    def IsMemberOf(self, fname, attributes):
        if os.path.exists(os.path.join(fname, 'kala.txt')):
            return winerror.S_OK
        return winerror.E_FAIL

if __name__=='__main__':
    import win32api
    import win32con
    import win32com.server.register
    win32com.server.register.UseCommandLine (IconOverlay)
    keyname = r'Software\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\PyPackageOverlay'
    key = win32api.RegCreateKey (win32con.HKEY_LOCAL_MACHINE, keyname)
    win32api.RegSetValue (key, None, win32con.REG_SZ, IconOverlay._reg_clsid_)

2 个答案:

答案 0 :(得分:0)

我无法看到您的代码存在任何问题,但有以下几点需要检查:

  1. 查看示例中的原始代码是否有效(下载示例中使用的图标等);如果没有,那么你的windows版本会有所不同。
  2. 增加优先级以匹配示例,它使用50并且不清楚该值如何影响功能。
  3. 通过在图标编辑器中打开图标和示例来验证图标的格式是否正确。

答案 1 :(得分:0)

我解决了这个问题:

在注册处理程序之前,必须删除现有的相同命名处理程序。这可以通过运行>完成。 regedit>浏览到Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ ShellIconOverlayIdentifiers \>删除相同的命名处理程序。

在此之后,可以添加新的处理程序。

要使处理程序正常工作,必须终止进程explorer.exe并重新启动它。