1

快速问题:我在玩 Windows 7 中的一些新任务栏 API,并在我的应用程序跳转列表中显示了最近的项目,但我想以与文件名不同的标题显示它们(我的应用程序中的大多数文件将be opening 将具有非常相似的名称)。不过,我看不到任何使用 IShellItem 接口的方法。我是否必须使用自定义类别和 IShellLinks 来完成此操作?

作为参考,我当前的代码如下所示:

void AddRecentApp(const wchar_t* path, const wchar_t* title /* Can I even use this? */ ) {
    HRESULT hres;

    hres = CoInitialize(NULL);

    IShellItem* recentItem;
    hres = SHCreateItemFromParsingName(path, NULL, IID_PPV_ARGS(&recentItem));
    if(SUCCEEDED(hres)) {
        SHARDAPPIDINFO recentItemInfo;
        recentItemInfo.pszAppID = MY_APP_USER_MODEL_ID;
        recentItemInfo.psi = recentItem;

        SHAddToRecentDocs(SHARD_APPIDINFO, &recentItemInfo);

        recentItem->Release();
    }
}
4

1 回答 1

2

弄清楚了。IShellItems 只是一个文件的表示,因此它们只会提供该文件的信息(没有自定义标题等)。 IShellLink 本质上是一种快捷方式,并且在显示和启动时采取的操作方面更加灵活,所以更多适合这种情况。这是我的新代码:

void AddRecentApp(const wchar_t* path, const wchar_t* title) {
    HRESULT hres;
    hres = CoInitialize(NULL);

    // Shell links give us more control over how the item is displayed and run
    IShellLink* shell_link;
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell_link));
    if(SUCCEEDED(hres)) {
        // Set up the basic link properties
        shell_link->SetPath(path);
        shell_link->SetArguments(L"--some-command-line-here"); // command line to execute when item is opened here!
        shell_link->SetDescription(title); // This is what shows in the tooltip
        shell_link->SetIconLocation(L"/path/to/desired/icon", 0); // can be an icon file or binary

        // Open up the links property store and change the title
        IPropertyStore* prop_store;
        hres = shell_link->QueryInterface(IID_PPV_ARGS(&prop_store));
        if(SUCCEEDED(hres)) {
            PROPVARIANT pv;
            InitPropVariantFromString(title, &pv);

            // Set the title property.
            prop_store->SetValue(PKEY_Title, pv); // THIS is where the displayed title is actually set
            PropVariantClear(&pv);

            // Save the changes we made to the property store
            prop_store->Commit();
            prop_store->Release();
        }

        // The link must persist in the file system somewhere, save it here.
        IPersistFile* persist_file; 
        hres = shell_link->QueryInterface(IID_PPV_ARGS(&persist_file));
        if(SUCCEEDED(hres)) {
            hres = persist_file->Save(L"/link/save/directory", TRUE); 
            persist_file->Release(); 
        }

        // Add the link to the recent documents list
        SHARDAPPIDINFOLINK app_id_info_link;
        app_id_info_link.pszAppID = MY_APP_USER_MODEL_ID;
        app_id_info_link.psl = shell_link;
        SHAddToRecentDocs(SHARD_APPIDINFOLINK, &app_id_info_link);

        shell_link->Release();
    }
}  
于 2009-11-04T16:04:12.593 回答