首页 | 社区 | 博客 | 招聘 | 文章 | 新闻 | 下载 | 读书 | 代码
亲,您未登录哦! 登录 | 注册

用BCB在windows桌面创建快捷方式

打印文章

分享到:
API提供了一个叫做IShellLink的COM接口允许我们创建快捷方式。为在桌面创建快捷方式,我们创建一个IShellLink对象,设置它的属性,然后把这个link保存到desktop目录。

下面的例子代码演示了怎样创建一个快捷方式。在这个例子里,这个快捷方式保存在C:\Drive目录下。

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

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    if(OpenDialog1->Execute())
        CreateShortCut(OpenDialog1->FileName);
}
//----------------------------------------------------------------------
void TForm1::CreateShortCut(const AnsiString &file)
{
    IShellLink* pLink;
    IPersistFile* pPersistFile;
    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("C:\\bcbshortcut.lnk");
                pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
                pPersistFile->Release();
            }
            pLink->Release();
        }

        CoUninitialize();
    }
}
//----------------------------------------------------------------------
上面的例子只是把快捷方式文件保存到了c:\drive目录下,但没保存到desktop目录下。

要让快捷方式出现在桌面上,只须把快捷方式文件保存到desktop目录下。首先我们要找到windows的desktop目录,请参阅判断windows的Desktop及相关目录这一节。一旦我们知道了desktop所在的目录,我们就能将快捷方式文件保存到desktop目录下。然后windows就能将快捷方式图标显示到桌面上。下面是经过改进了的例子:

//----------------------------------------------------------------------
void TForm1::CreateShortCut(const AnsiString &file)
{
    IShellLink* pLink;
    IPersistFile* pPersistFile;
    LPMALLOC      ShellMalloc;
    LPITEMIDLIST  DesktopPidl;
    char DesktopDir[MAX_PATH];
    if(FAILED(SHGetMalloc(&ShellMalloc)))
        return;
    if(FAILED(SHGetSpecialFolderLocation(NULL,
                                        CSIDL_DESKTOPDIRECTORY,
                                        &DesktopPidl)))
        return;
    if(!SHGetPathFromIDList(DesktopPidl, DesktopDir))
    {
        ShellMalloc->Free(DesktopPidl);
        ShellMalloc->Release();
        return;
    }
    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);
                pPersistFile->Release();
            }
            pLink->Release();
        }
        CoUninitialize();
    }
}
//----------------------------------------------------------------------
不要让你陷入到COM的复杂之中。COM只是创建和使用对象的一种方法。在这个例子里我们可以考虑不使用COM而是用等价的C++技术。

COM code                                  C++ psuedo-equivalent
IShellLink*  pLink;                      TShellLink  *Link;
IPersistFile* pPersistFile;                TPersistFile *PersistFile;

CoInitialize();
                                         
CoCreateInstance(CLSID_ShellLink,          Link = new TShellLink;
                                  NULL,
                                CLSCTX_INPROC_SERVER,
                                IID_IShellLink,
                               (void **) &pLink)
pLink->SetPath(file.c_str());              
Link->SetPath(file.c_str());
pLink->SetShowCmd(SW_SHOW);               
Link->SetShowCmd(SW_SHOW);
pLink->QueryInterface(IID_IPersistFile    PersistFile =
              (void **)&pPersistFile)))      dynamic_cast<TPersistFile*>(Link);

pPersistFile->Save("C:\\", TRUE);          
PersistFile->Save("C:\\");
pPersistFile->Release();                  
delete PersistFile
pLink->Release();                          
delete Link;
CoUninitialize(); 

本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )

编程爱好者论坛

本栏最新文章