Windows 7跳转列表的自定义标题最近的项目

时间:2009-11-04 05:15:55

标签: windows-7 jump-list recent-documents

快速提问:我正在使用Windows 7中的一些新任务栏API,并且已经在我的应用程序跳转列表中显示了最近的项目,但是我希望以不同于文件名的标题显示它们(大多数文件)我的应用程序将打开将具有非常相似的名称)。但是,我没有看到使用IShellItem接口的任何方法。我是否必须使用自定义类别和IShellLink来完成此任务?

作为参考,我目前的代码如下:

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();
    }
}

1 个答案:

答案 0 :(得分:2)

想出来。 IShellItem只是文件的一种表示,因此它们只提供该文件的信息(没有自定义标题等).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();
    }
}