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

发布于 2024-08-09 20:03:30 字数 755 浏览 8 评论 0原文

快速问题:我正在尝试 Windows 7 中的一些新任务栏 API,并且已在我的应用程序跳转列表上显示最近的项目,但我想以与文件名不同的标题显示它们(我的应用程序的大多数文件将将开放将具有非常相似的名称)。不过,我没有看到任何方法可以使用 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();
    }
}

Quickie question: I'm toying with some of the new taskbar APIs in Windows 7 and have gotten Recent Items on my Apps jumplist to show up, but I would like to display them under a different title than the filename (most files my app will be opening will have very similar names). I don't see any way to do that with the IShellItem interface, though. Would I have to use custom categories and IShellLinks to accomplish this?

For reference, my current code looks like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅倚清风 2024-08-16 20:03:30

想通了。 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();
    }
}  

Figured it out. IShellItems are just a representation of a file, so they only will provide that file's information (no custom title, etc.) An IShellLink is essentially a shortcut, and is much more flexible in terms of display and actions taken when launched, so are more appropriate in this situation. Here's my new code:

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();
    }
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文