代码之家  ›  专栏  ›  技术社区  ›  Toji

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

  •  1
  • Toji  · 技术社区  · 15 年前

    quickie问题:我在玩弄Windows7中的一些新的任务栏API,并在我的应用程序JumpList上显示最近的项目,但我想用与文件名不同的标题显示它们(我的应用程序将要打开的大多数文件将具有非常相似的名称)。不过,我看不出用ishelitem接口实现这一点的任何方法。我是否需要使用自定义类别和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();
        }
    }
    
    1 回复  |  直到 15 年前
        1
  •  2
  •   Toji    15 年前

    明白了。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();
        }
    }  
    
    推荐文章