C++创建快捷方式并添加到开始菜单、桌面、任务栏

将快捷方式添加到任务栏:

调用ShellExecute(NULL, L"taskbarpin", lnkPath, NULL, NULL, 0);
lnkPath则是快捷方式文件的路径

将快捷方式添加到桌面和启动菜单:

只需要获取到桌面的文件夹路径和启动菜单的文件夹路径,将快捷方式创建到该目录即可

创建link快捷方式:

::CoInitialize(NULL);

IShellLink* pIShellLink;
HRESULT hRes = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pIShellLink);
if (!SUCCEEDED(hRes)) {
  return false;
}

pIShellLink->SetPath(L"x:\\xx\\xx.exe");
pIShellLink->SetDescription(L"这是一个测试的桌面快捷方式");
pIShellLink->SetHotkey(MAKEWORD('R', HOTKEYF_SHIFT | HOTKEYF_CONTROL));
IPersistFile* pIPersistFile;
hRes = pIShellLink->QueryInterface(IID_IPersistFile, (void**)&pIPersistFile);
if (!SUCCEEDED(hRes)) {
  return false;
}

hRes = pIPersistFile->Save(savePath.c_str(), TRUE);
if (!SUCCEEDED(hRes)) {
  return false;
}

pIPersistFile->Release();
pIShellLink->Release();

::CoUninitialize();

创建url快捷方式:

if (m_sIconFile.length() == 0) {
  return false;
}

IniFile iniFs;
iniFs.setFilename(m_sLnkFile.c_str());
std::string curUrl = iniFs.getStr("InternetShortcut", "URL");

if (curUrl == m_url) {//一样,则不用重写
  return true;
}

std::string str;
str += "[{000214A0-0000-0000-C000-000000000046}]\r\n";
str += "Prop3=19,2\r\n";
str += "[InternetShortcut]\r\n";
str += "URL=";
str += m_url;
str += "\r\n";
str += "IDList=\r\n";
str += "IconFile=";
str += m_sIconFile;
str += "\r\n";
str += "IconIndex=0\r\n";

// 设置文件为正常方式
DWORD dwAttribute = FILE_ATTRIBUTE_NORMAL;
SetFileAttributesA(m_sLnkFile.c_str() , dwAttribute);

FILE* fp = fopen(m_sLnkFile.c_str(), "wb");
fwrite(str.c_str(), str.length(), 1, fp);
fclose(fp);

本文为@艺文笔记原创文章,转载请注明出处:https://www.xuwenyan.com/archives/89,非常感谢!
上一篇:  下一篇: