如何告诉Windows资源管理器刷新其图标?

时间:2009-11-18 17:35:21

标签: c icons windows-explorer windows-shell

一旦我的安装程序完成了我的应用程序的exe的新版本的安装,我想告诉Explorer使用新exe的图标作为其快捷方式。但是,我无法弄清楚如何做到这一点。

通过在线阅读,看起来问题是system image list正在缓存旧版本的图标。我尝试使用SHCNE_UPDATEIMAGE参数调用SHChangeNotify。我试着打电话给SHUpdateImage。我甚至试过sledgehammer approach广播WM_SETTINGCHANGE。似乎没什么用。

我完全有可能做错事。任何帮助将不胜感激。

警告:非常难看的测试代码。

#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 个答案:

答案 0 :(得分:2)

你的大锤方法是我用来完成这项工作的方法。但是代码中有一个oops,“Shell图标大小”值是REG_SZ,而不是REG_DWORD。始终VERIFY()API函数返回值...