当前位置:首页 >> 编程开发 >> Visual C++ >> 内容

如何在快速启动栏创建快捷方式

时间:2008/1/5 作者:平凡之路 来源:xuhantao.com 浏览:

#define NO_WIN32_LEAN_AND_MEAN
#include <shlobj.hpp>
#include <vcl.h>
// 以上三行放在单元文件最开始
//---------------------------------------------------------------------------
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;    //
};
//---------------------------------------------------------------------------
// 在快速启动栏创建快捷方式
bool CreateQuickLaunchShortcut(TShortcutCfg *scConfig)
{
  char szBuf[MAX_PATH];
  bool bReturn = true;
  wchar_t wszBuf[MAX_PATH];
  IShellLink *pShellLink;
  AnsiString strShortcutFile;
  LPITEMIDLIST lpItemIdList;
  SHGetSpecialFolderLocation(0, CSIDL_APPDATA, &lpItemIdList);
  SHGetPathFromIDList(lpItemIdList, szBuf);
  if(DirectoryExists(AnsiString(szBuf)))
  {
    strShortcutFile = AnsiString(szBuf)
      + "\\Microsoft\\Internet Explorer\\Quick Launch\\"
      + scConfig->strShortcutName + ".lnk";
    strShortcutFile.WideChar(wszBuf, MAX_PATH);
  }
  else
    bReturn = false;
  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;
}
// 调用代码举例:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TShortcutCfg scShortcut;
  scShortcut.strDestFile = "C:\\123\\123.exe";
  scShortcut.strShortcutName = "test";
  if(CreateQuickLaunchShortcut(&scShortcut))
    ShowMessage("在快速启动栏创建快捷方式成功!");
}

相关文章
  • 没有相关文章
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 徐汉涛(www.xuhantao.com) © 2024 版权所有 All Rights Reserved.
  • 部分内容来自网络,如有侵权请联系站长尽快处理 站长QQ:965898558(广告及站内业务受理) 网站备案号:蒙ICP备15000590号-1