争怎路由网:是一个主要分享无线路由器安装设置经验的网站,汇总WiFi常见问题的解决方法。

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

时间:2024/7/13作者:未知来源:争怎路由网人气:

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(); 

关键词:用BCB在windows桌面创建快捷方式




Copyright © 2012-2018 争怎路由网(http://www.zhengzen.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版