How to tell Windows Explorer to refresh its icons?

蹲街弑〆低调 提交于 2019-12-10 19:41:43

问题


Once my installer finishes installing new versions of my application's exe, I'd like to tell Explorer to use the new exe's icons for its shortcuts. However, I cannot figure out how to do this.

From reading online, it looks like the problem is that the system image list is caching an old version of the icon. I tried calling SHChangeNotify with a SHCNE_UPDATEIMAGE parameter. I tried calling SHUpdateImage. I even tried the sledgehammer approach of broadcasting WM_SETTINGCHANGE. Nothing seems to work.

It's entirely possible that I'm just doing something wrong. Any help would be appreciated.

Warning: Very ugly test code follows.

#if 1
    // First attempt: using shell functions
    wchar_t icon_path[MAX_PATH];
    int icon_index;
    UINT icon_flags;

    IShellFolder *desktop_folder;
    IShellFolder *sub_folder;
    IExtractIcon *extract_icon;
    LPITEMIDLIST pidl;

    SHGetDesktopFolder(&desktop_folder);

    wchar_t *folder_path = L"C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\MyCompany\\";
    desktop_folder->ParseDisplayName(NULL, NULL, folder_path, NULL, &pidl,
        NULL);
    desktop_folder->BindToObject(pidl, NULL, IID_IShellFolder,
        (void**) &sub_folder);
    sub_folder->ParseDisplayName(NULL, NULL, L"MyApp.lnk", NULL, &pidl,
        NULL);

    sub_folder->GetUIObjectOf(NULL, 1, (LPCITEMIDLIST*) &pidl,
        IID_IExtractIcon, NULL, (void**) &extract_icon);

    extract_icon->GetIconLocation(0, icon_path, MAX_PATH,
        &icon_index, &icon_flags);

    SHFILEINFO sfi;
    DWORD_PTR result = SHGetFileInfo(shortcut_path, 0, &sfi, sizeof(sfi), 
        SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
    SHUpdateImage(icon_path, icon_index, icon_flags, sfi.iIcon);
    // sfi.iIcon should be correct, but we'll try both, just for fun...
    SHChangeNotify(SHCNE_UPDATEIMAGE, SHCNF_DWORD, NULL,
        (LPCVOID) icon_index);
    SHChangeNotify(SHCNE_UPDATEIMAGE, SHCNF_DWORD, NULL,
        (LPCVOID) sfi.iIcon);
#else
    // Second attempt: broadcasting a settings change
    HKEY reg;
    RegCreateKeyEx(HKEY_CURRENT_USER,
        L"Control Panel\\Desktop\\WindowMetrics", 0, NULL, 0,
        KEY_SET_VALUE, NULL, &reg, NULL);
    DWORD value;
    value = 33;
    RegSetValueEx(reg, L"Shell Icon Size", 0, REG_DWORD, (BYTE*) &value,
        sizeof(value));
    value = 32;
    SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETNONCLIENTMETRICS,
        (int) L"WindowMetrics");
    RegSetValueEx(reg, L"Shell Icon Size", 0, REG_DWORD, (BYTE*) &value,
        sizeof(value));
    SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETNONCLIENTMETRICS,
        (int) L"WindowMetrics");
#endif

回答1:


Your sledge-hammer approach is the one I've seen used to get this done. An oops in your code though, the "Shell Icon Size" value is a REG_SZ, not a REG_DWORD. Always VERIFY() the API function return values...



来源:https://stackoverflow.com/questions/1757737/how-to-tell-windows-explorer-to-refresh-its-icons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!