在桌面建立快捷方式

2008-04-09 03:59:35来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

Harold Howe(翻译:抱雪)

API提供了一个COM接口:调用 IShellLink 将允许你建立一个快捷方式,要在桌面建立一个快捷方式,只要把这个快捷方式保存到桌面目录就可以了。

下面的示例代码演示怎样建立一个快捷方式,在这个例子里,快捷方式将保存在C:\ drive。

//----------------------------------------------------------------------
#include <shlobj.h>

void __fastcall TForm1::Button1Click(TObject *Sender)
{
// Allow the user to find a file with a common dialog box,
// then create a shortcut to that file.
if(OpenDialog1->Execute())
CreateShortCut(OpenDialog1->FileName);
}
//----------------------------------------------------------------------
void TForm1::CreateShortCut(const AnsiString &file)
{
// IShellLink allows us to create the shortcut.
// IPersistFile saves the link to the hard drive.
IShellLink* pLink;
IPersistFile* pPersistFile;

// First, we have to initialize the COM library
if(SUCCEEDED(CoInitialize(NULL)))
{
// If CoInitialize doesn't fail, then instantiate an
// IShellLink object by calling CoCreateInstance.
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **) &pLink)))
{
// if that succeeds, then fill in the shortcut attributes
pLink->SetPath(file.c_str());
pLink->SetDescription("Woo hoo, look at Homer's shortcut");
pLink->SetShowCmd(SW_SHOW);

// Now we need to save the shortcut to the hard drive. The
// IShellLink object also implements the IPersistFile interface.
// Get the IPersistFile part of the object using QueryInterface.
if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,
(void **)&pPersistFile)))
{
// If that succeeds, then call the Save method of the
// IPersistFile object to write the shortcut to the desktop.
WideString strShortCutLocation("C:\\bcbshortcut.lnk");
pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
pPersistFile->Release();
}
pLink->Release();
}

// Calls to CoInitialize need a corresponding CoUninitialize call
CoUninitialize();
}
}
//----------------------------------------------------------------------

如果你执行这个代码,你将在你的C:\ drive下看见一个新的快捷方式,这非常好,但我们的问题是在桌面建立快捷方式。其实这也很简单,只要把快捷方式保存在桌面目录中就可以了(译者注:参见本站收录的:判断Windows相关目录)。

//----------------------------------------------------------------------
void TForm1::CreateShortCut(const AnsiString &file)
{
IShellLink* pLink;
IPersistFile* pPersistFile;
LPMALLOC ShellMalloc;
LPITEMIDLIST DesktopPidl;
char DesktopDir[MAX_PATH];

// We are going to create a pidl, and it will need to be
// freed by the shell mallocator. Get the shell mallocator
// object using API SHGetMalloc function. Return if failure.
if(FAILED(SHGetMalloc(&ShellMalloc)))
return;

// use the API to get a pidl for the desktop directory
// if function fails, return without proceeding
if(FAILED(SHGetSpecialFolderLocation(NULL,
CSIDL_DESKTOPDIRECTORY,
&DesktopPidl)))
return;

// Now convert the pidl to a character string
// return if function fails
if(!SHGetPathFromIDList(DesktopPidl, DesktopDir))
{
ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();
return;
}

// At this point, we are done with the pidl and the
// mallocator, so free them up
ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();

if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **) &pLink)))
{
pLink->SetPath(file.c_str());
pLink->SetDescription("Woo hoo, look at Homer's shortcut");
pLink->SetShowCmd(SW_SHOW);

if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,
(void **)&pPersistFile)))
{

WideString strShortCutLocation(DesktopDir);
strShortCutLocation = "\\bcbshortcut.lnk";
pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C语言库函数(H类字母)2

下一篇:串行通信的基本原理及用MFC实现串口通信编程