windows/Linux创建桌面快捷方式

很多时候为了方便应用程序的快捷使用,我们会创建桌面快捷方式用来便捷调用。如果应用程序没有加密调用参数的话,我们直接创建一个文件快捷方式的链接就可以了。但是如果应用程序添加了加密调用参数,在动态创建桌面快捷方式的时候就比较麻烦了。这里介绍一下通过windowsAPI动态创建桌面快捷方式的方法。希望对你有帮助。

Windows创建桌面快捷方式

通过WindowsAPI创建带参数的桌面快捷方式

/**@brief 通过windowsAPI创建桌面快捷方式
* @param[in]  target_exe_path 应用程序的位置
* @param[in]  lnk_path 快捷方式的位置
* @param[in]  working_path 应用程序的工作目录
* @param[in]  argument 应用程序的调用参数
* @param[in]  shortcut 是否有快捷方式
* @param[in]  isSHowCmd 是否显示终端
* @param[in]  describe 快捷方式的描述信息
* @param[in]  icon_path 快捷方式图标的位置
* @return  函数执行结果
* - 1  创建成功
* - 0  创建失败
*/
//函数调用的时候有两点需要注意
//1.快捷方式的后缀名称是lnk
//2.应用程序是无法通过外部文件指定的,需要编译进程序的资源列表中
bool create_desktop_shortcut(const wchar_t *target_exe_path, const wchar_t *lnk_path,const wchar_t *working_path, const wchar_t *argument,int shortcut,bool isShowCmd,const wchar_t* describe,const wchar_t* icon_path)
{if((target_exe_path == NULL) || (lnk_path == NULL) || (working_path == NULL) || (argument == NULL)){return false;}HRESULT hr;IShellLink     *pLink;IPersistFile   *ppf;hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pLink);if (FAILED(hr))return false;hr = pLink->QueryInterface(IID_IPersistFile, (void**)&ppf);if (FAILED(hr)){pLink->Release();return false;}//设置目标文件的地址pLink->SetPath(target_exe_path);//设置工作目录pLink->SetWorkingDirectory(working_path);//pLink->SetIconLocation(target_exe_path,0);pLink->SetArguments(argument);if (shortcut != 0)pLink->SetHotkey(shortcut);//设置描述信息pLink->SetDescription(describe);//是否显示终端pLink->SetShowCmd(isShowCmd);hr = ppf->Save(lnk_path, TRUE);ppf->Release();pLink->Release();return SUCCEEDED(hr);
}

Linux创建桌面快捷方式

Linux创建桌面快捷方式不需要调用对应的API,只需要创建对应的快捷方式文件就可以了。创建方法如下:

#include 
#include 
#include 
#include 
#include /**@brief Linux下创建桌面快捷方式
* @param[in]  shortcut_name 应用程序的位置
* @param[in]  exe_path 执行程序的位置
* @param[in]  argument 程序的调用参数
* @param[in]  icon_path 图标位置
* @param[in]  version 程序版本信息
*/
void create_desktop_shortcut(QString shortcut_name, QString exe_path, QString argument, QString icon_path, QString version)
{//首先获取桌面的文件路径QString desktop_path = QDir::toNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));//获取桌面快捷方式的位置QString file_name = QString("%1.desktop").arg(shortcut_name);QString file_path = QDir::toNativeSeparators(QString("%1/%2").arg(desktop_path).arg(file_name));QFile file(file_path);if(file.open(QIODevice::WriteOnly | QIODevice::Truncate)){QTextStream txt_output_stream(&file);txt_output_stream << "[Desktop Entry]" << endl;txt_output_stream << "Type=Application" << endl;//指定版本信息txt_output_stream << QString("Version=%1").arg(version)<< endl;//快捷方式的名称txt_output_stream << QString("Name=%1").arg(shortcut_name)<< endl;//描述信息可以为空txt_output_stream << QString("Comment=Run %1").arg("")<< endl;//icon pathtxt_output_stream << QString("Icon=%1").arg(icon_path)<< endl;//执行文件和调用参数txt_output_stream << QString("Exec=%1 %2").arg(exe_path).arg(argument)<< endl;//是否调用终端txt_output_stream << "Terminal=false" << endl;file.close();//修改文件的可执行权限QString check_command = QString("chmod 777 %1").arg(file_path);FILE* ptr = NULL;if((ptr = popen(check_command.toStdString().c_str(), "r")) == NULL){return;}pclose(ptr);}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部