快捷方式属性表如何立即更改图标?

问题描述

在快捷方式属性表中更改图标时,快捷方式图标会立即更改。

Shortcut icon change

当您多次运行Python脚本时,您会看到快捷方式图标每次都更改为一个随机图标。但是,当您注释掉create_internet_shortcut中的main呼叫时,手动编写的Internet快捷方式“ Bing.url”的图标停止更改。在SHChangeNotify之后,COM方法中必须进行一些其他处理,而这仅仅是不够的。

在不使用COM的情况下手动编写URL文件时,我需要怎么做才能更改Internet快捷方式图标?

我还尝试了4次不同的时间致电SHChangeNotify,但这没有用。请参见update_all_icons函数并在main中注释掉调用

安装:

pip install pywin32

代码

#!/usr/bin/env python

from typing import Any,Tuple

import os
import random
import traceback
from pathlib import Path

from win32com import storagecon
from win32comext.shell import shell,shellcon
import win32gui
import pythoncom
import pywintypes

system_icon_file = r"%systemRoot%\System32\shell32.dll"
system_icon_max_index = 326

desktop_path = Path(r"~\Desktop").expanduser()


def create_internet_shortcut(path: str,target: str = None) -> None:
    shortcut = pythoncom.CoCreateInstance(
        shell.CLSID_InternetShortcut,None,pythoncom.CLSCTX_INPROC_SERVER,shell.IID_IUniformResourceLocator
    )
    persist_file = shortcut.QueryInterface(pythoncom.IID_IPersistFile)
    persist_file.Load(path)

    if not isinstance(target,str):
        target = shortcut.GetURL()
        if target is None:
            target = "https://www.bing.com/"

    shortcut.SetURL(target)

    property_set_storage = shortcut.QueryInterface(pythoncom.IID_IPropertySetStorage)
    property_storage = property_set_storage.Open(shell.FMTID_Intshcut,storagecon.STGM_READWRITE)
    icon_file = system_icon_file
    icon_index = random.randint(0,system_icon_max_index)
    property_storage.WriteMultiple((shellcon.PID_IS_ICONFILE,shellcon.PID_IS_ICONINDEX),(icon_file,icon_index))
    property_storage.Commit(storagecon.STGC_DEFAULT)
    persist_file.Save(path,0)


def create_link_shortcut(path: str,target: str = "%systemDrive%") -> None:
    shortcut = pythoncom.CoCreateInstance(
        shell.CLSID_ShellLink,shell.IID_IShellLink
    )
    persist_file = shortcut.QueryInterface(pythoncom.IID_IPersistFile)

    try:
        persist_file.Load(path)
    except pywintypes.com_error:
        traceback.print_exc()

    shortcut.SetPath(target)
    icon_file = system_icon_file
    icon_index = random.randint(0,system_icon_max_index)
    shortcut.SetIconLocation(icon_file,icon_index)
    persist_file.Save(path,0)


def write_internet_shortcut(path: str,target: str = None) -> None:
    if not isinstance(target,str):
        target = "https://www.bing.com/"

    icon_file = system_icon_file
    icon_index = random.randint(0,system_icon_max_index)

    text = f"""[InternetShortcut]
URL={target}
IconFile={icon_file}
IconIndex={icon_index}"""

    with open(path,"w",encoding="utf8") as file:
        file.write(text)


def update_icon(path: str) -> None:
    shell.SHChangeNotify(shellcon.SHCNE_UPDATEITEM,shellcon.SHCNF_PATHW | shellcon.SHCNF_FLUSHNowAIT,path,None)


def update_all_icons(path: str) -> None:
    result,info = shell.SHGetFileInfo(
        path,shellcon.SHGFI_ICON | shellcon.SHGFI_disPLAYNAME | shellcon.SHGFI_TYPENAME
    )
    hIcon,iIcon,dwAttributes,displayName,typeName = info

    print("Info:",info)

    if iIcon > 0:
        shell.SHChangeNotify(shellcon.SHCNE_UPDATEIMAGE,shellcon.SHCNF_DWORD | shellcon.SHCNF_FLUSHNowAIT,None)
        shell.SHChangeNotify(shellcon.SHCNE_ASSOCCHANGED,None)

    shell.SHChangeNotify(shellcon.SHCNE_UPDATEITEM,None)

    shell.SHChangeNotify(shellcon.SHCNE_UPDATEDIR,str(desktop_path.resolve()),None)

    win32gui.DestroyIcon(hIcon)


def main(*args: Tuple[Any,...]) -> None:
    create_internet_shortcut(str(desktop_path.joinpath("COM Internet.url").resolve()))
    create_link_shortcut(str(desktop_path.joinpath("COM link.lnk").resolve()))

    bing_file_path = str(desktop_path.joinpath("Bing.url").resolve())
    write_internet_shortcut(bing_file_path)
    update_icon(bing_file_path)
    # update_all_icons(bing_file_path)


if __name__ == "__main__":
    main()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)