问题 1671793的答案中的代码是其中的一部分。你想要一个IShellLink
而不是一个IShellItem
. 我一点一点地尝试了那个代码。IPropertyStore
在使用设置标题之前,事情不会起作用。该IPersistFile
代码似乎没有必要。
综上所述,虽然我现在在我的应用程序的任务栏图标上单击鼠标右键时会出现项目,但我还没有让它们在开始菜单上显示为我的应用程序的子菜单(例如 word docs 所做的那样) ),所以我还不完全满意。我认为这是文档中警告的结果SHAddToRecentDocs
:
在 Windows XP 和更高版本中,从最近使用的文档列表中筛选出可执行 (.exe) 文件。尽管 SHAddToRecentDocs 将接受可执行文件的路径,但该文件不会出现在“最近的项目”列表中。
这是我的代码。由于我的开发环境使用的是较旧的 Windows SDK(所以我必须为自己创建 PKEY_Title)并且我的应用程序需要支持 Win2k(所以我不想绑定到InitPropVariantFromString
需要较新 Windows的函数),因此我跳过了一些障碍版本)。
HRESULT hr;
IShellLink* link;
// Get a pointer to the IShellLink interface.
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&link);
if (FAILED(hr))
return;
link->SetPath(path_to_app);
link->SetArguments(L"/some /args");
link->SetDescription(L"A description"); // Turns into tooltip
IPropertyStore* prop_store;
hr = link->QueryInterface(&prop_store);
if(SUCCEEDED(hr))
{
PROPVARIANT pv;
pv.vt=VT_LPWSTR;
pv.pwszVal=L"Name of item"; // Turns into actual item name
PROPERTYKEY PKEY_Title;
CLSIDFromString(L"{F29F85E0-4FF9-1068-AB91-08002B27B3D9}", &(PKEY_Title.fmtid));
PKEY_Title.pid=2;
// Set the title property.
hr = prop_store->SetValue(PKEY_Title, pv); // THIS is where the displayed title is actually set
// Save the changes we made to the property store
prop_store->Commit();
prop_store->Release();
}
SHARDAPPIDINFOLINK appinfo;
appinfo.pszAppID=L"Company.AppName"; // Previously registered using SetCurrentProcessExplicitAppUserModelID
appinfo.psl=link;
SHAddToRecentDocs(SHARD_APPIDINFOLINK, &appinfo);
link->Release();