您当前的位置: 首页 > 

蓝不蓝编程

暂无认证

  • 0浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

在桌面和快速启动中创建快捷方式

蓝不蓝编程 发布时间:2011-02-01 22:54:00 ,浏览量:0

//---------------------------------------------------------------------------

#ifndef ShortCutCreatorH#define ShortCutCreatorH#include #pragma hdrstop

#include

struct TShortcutCfg{    // 构造函数    TShortcutCfg()    {        nShowCmd = SW_SHOWNORMAL;        wHotKey = 0;        nIconIndex = 0;    }    // 结构成员:    AnsiString  strShortcutName; //    AnsiString  strLnkDir;       //    AnsiString  strDestFile;     //    AnsiString  strArguments;    //    AnsiString  strIconFile;     //    int         nIconIndex;      //    AnsiString  strWorkingDir;   //    AnsiString  strDescription;  //    WORD        wHotKey;         //    int         nShowCmd;        //};class ShortCutCreator{public: bool CreateDesktopShortcut(String& destFile,String& shortCutName,String& arguments); bool CreateQuickLaunchShortcut(String& destFile,String& shortCutName,String& arguments);private: bool CreateShortcut(String& destFile,String& shortCutName,String& dir,String& arguments);};

#endif

//---------------------------------------------------------------------------

#include "ShortCutCreator.h"

#pragma argsused// 在快速启动栏创建快捷方式bool ShortCutCreator::CreateDesktopShortcut(String& destFile,String& shortCutName,String& arguments){ char szBuf[MAX_PATH]; LPITEMIDLIST lpItemIdList; SHGetSpecialFolderLocation(0, CSIDL_DESKTOPDIRECTORY, &lpItemIdList); SHGetPathFromIDList(lpItemIdList, szBuf); String dir = String(szBuf)+"//"; bool result = CreateShortcut(destFile,shortCutName,dir,arguments); return result;}bool ShortCutCreator::CreateQuickLaunchShortcut(String& destFile,String& shortCutName,String& arguments){    char szBuf[MAX_PATH]; LPITEMIDLIST lpItemIdList; SHGetSpecialFolderLocation(0, CSIDL_APPDATA, &lpItemIdList); SHGetPathFromIDList(lpItemIdList, szBuf); String dir = String(szBuf) + "//Microsoft//Internet Explorer//Quick Launch//"; bool result = CreateShortcut(destFile,shortCutName,dir,arguments); return result;}bool ShortCutCreator::CreateShortcut(String& destFile,String& shortCutName,String& dir,String& arguments){ TShortcutCfg scConfig; scConfig.strDestFile = destFile; scConfig.strShortcutName = shortCutName; scConfig.strArguments = arguments; bool bReturn = true;    wchar_t wszBuf[MAX_PATH];    IShellLink *pShellLink; AnsiString strShortcutFile;

 strShortcutFile = dir + shortCutName + ".lnk"; strShortcutFile.WideChar(wszBuf, MAX_PATH);

    if(bReturn)    {        bReturn = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,                IID_IShellLink, (void **)&pShellLink) >= 0;

        if(bReturn)        {            IPersistFile *ppf;            bReturn = pShellLink->QueryInterface(IID_IPersistFile, (void **)&ppf) >= 0;            if(bReturn)            {                // 目标文件    if(scConfig.strDestFile != EmptyStr)     bReturn = pShellLink->SetPath(scConfig.strDestFile.c_str()) >= 0;                // 参数                if(bReturn && scConfig.strArguments != EmptyStr)                 bReturn = pShellLink->SetArguments(scConfig.strArguments.c_str()) >= 0;                // 显示图标                if(bReturn && scConfig.strIconFile != EmptyStr && FileExists(scConfig.strIconFile))                    pShellLink->SetIconLocation(scConfig.strIconFile.c_str(),                            scConfig.nIconIndex);                // 起始位置                if(bReturn && scConfig.strWorkingDir != EmptyStr)                    pShellLink->SetWorkingDirectory(scConfig.strWorkingDir.c_str());                // 备注                if(bReturn && scConfig.strDescription != EmptyStr)                    pShellLink->SetDescription(scConfig.strDescription.c_str());                // 快捷键                if(bReturn && scConfig.wHotKey != 0)                    pShellLink->SetHotkey(scConfig.wHotKey);                // 运行方式                if(bReturn && scConfig.nShowCmd != 0)                    pShellLink->SetShowCmd(scConfig.nShowCmd);

                if(bReturn)                    bReturn = (ppf->Save(wszBuf, TRUE) >= 0);

                ppf->Release ();            }            pShellLink->Release ();        }    }    return bReturn;}

 

int main(int argc,char* argv[]){ UnicodeString strDestFile = L"C://Program Files//Internet Explorer//iexplore.exe"; UnicodeString strShortcutName = L"test125"; UnicodeString arguments = L"fdafdafd";    ShortCutCreator shortCutCreator; shortCutCreator.CreateDesktopShortcut(strDestFile,strShortcutName,arguments);}

关注
打赏
1639405877
查看更多评论
立即登录/注册

微信扫码登录

0.0403s