怎样创建桌面快捷方式?
创建桌面快捷方式,哪位大侠给段代码?
网上搜到的代码通过不了, http://www.newasp.net/tech/program/20802.html
不知道“创建IShellLink对象”是怎么回事?
[解决办法]
如果用C++Builder6,将以下三行放到单元文件.cpp的最顶部
#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 CrnCreateShortcut(TShortcutCfg sc, int nFolder)
{
char szBuf[MAX_PATH];
bool bResult = true;
wchar_t wszBuf[MAX_PATH];
IShellLink *pShellLink;
AnsiString strShortcutFile;
if (nFolder >= 0)
{
LPITEMIDLIST lpItemIdList;
SHGetSpecialFolderLocation(0, nFolder, &lpItemIdList);
SHGetPathFromIDList(lpItemIdList, szBuf);
if (DirectoryExists(AnsiString(szBuf)))
{
strShortcutFile = AnsiString(szBuf) + "\" + sc.strShortcutName + ".lnk";
strShortcutFile.WideChar(wszBuf, MAX_PATH);
}
else
bResult = false;
}
else if (DirectoryExists(sc.strLnkDir))
{
strShortcutFile = IncludeTrailingBackslash(sc.strLnkDir) +
sc.strShortcutName + ".lnk";
strShortcutFile.WideChar(wszBuf, MAX_PATH);
}
else
bResult = false;
if (bResult)
{
bResult = (CoInitialize(NULL) == S_OK);
if (bResult)
{
bResult = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **)&pShellLink) >= 0;
if (bResult)
{
IPersistFile *ppf;
bResult = pShellLink->QueryInterface(IID_IPersistFile, (void **)&ppf) >= 0;
if (bResult)
{
// 目标文件
if (sc.strDestFile != EmptyStr)
bResult = pShellLink->SetPath(sc.strDestFile.c_str()) >= 0;
// 参数
if (bResult && sc.strArguments != EmptyStr)
bResult = pShellLink->SetArguments(sc.strArguments.c_str()) >= 0;
// 显示图标
if (bResult && sc.strIconFile != EmptyStr && FileExists(sc.strIconFile))
pShellLink->SetIconLocation(sc.strIconFile.c_str(),
sc.nIconIndex);
// 起始位置
if (bResult && sc.strWorkingDir != EmptyStr)
pShellLink->SetWorkingDirectory(sc.strWorkingDir.c_str());
// 备注
if (bResult && sc.strDescription != EmptyStr)
pShellLink->SetDescription(sc.strDescription.c_str());
// 快捷键
if (bResult && sc.wHotKey != 0)
pShellLink->SetHotkey(sc.wHotKey);
// 运行方式
if (bResult && sc.nShowCmd != 0)
pShellLink->SetShowCmd(sc.nShowCmd);
if (bResult)
bResult = (ppf->Save(wszBuf, TRUE) >= 0);
ppf->Release ();
}
pShellLink->Release ();
}
CoUninitialize();
}
}
return bResult;
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char szBuf[MAX_PATH];
GetSystemDirectory(szBuf, sizeof(szBuf));
TShortcutCfg sc;
sc.strDestFile = ParamStr(0);
sc.strShortcutName = "快捷方式名称";
sc.strArguments = "/n";
sc.strIconFile = AnsiString(szBuf) + "\\shell32.dll";
sc.nIconIndex = 5;
sc.strWorkingDir = ExtractFilePath(ParamStr(0));
sc.wHotKey = MAKEWORD(VK_F8, HOTKEYF_CONTROL
[解决办法]
HOTKEYF_SHIFT);
sc.nShowCmd = SW_SHOWMAXIMIZED;
if (!CrnCreateShortcut(sc, CSIDL_DESKTOP))
ShowMessage("创建快捷方式失败!");
else
ShowMessage("创建快捷方式成功!");
}