VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C/C++语言编程 >
  • c语言入门之用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(); 
相关教程