Unit1.cpp
上传用户:lhxd_sz
上传日期:2014-10-02
资源大小:38814k
文件大小:3k
源码类别:

VC书籍

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #include <shlobj.h>
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma resource "*.dfm"
  9. TForm1 *Form1;
  10. //---------------------------------------------------------------------------
  11. __fastcall TForm1::TForm1(TComponent* Owner)
  12.         : TForm(Owner)
  13. {
  14. }
  15. //---------------------------------------------------------------------------
  16. void __fastcall TForm1::CreateDeskTopA(AnsiString file,AnsiString name)
  17. {
  18.   AnsiString ass;
  19.   int ii,ll;
  20.   IShellLink* pLink;
  21.   IPersistFile* pPersistFile;
  22.   LPMALLOC      ShellMalloc;
  23.   LPITEMIDLIST  pDesktop;
  24.   char DesktopDir[MAX_PATH];
  25.   if(FAILED(SHGetMalloc(&ShellMalloc)))
  26.     return;
  27.   if(FAILED(SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOPDIRECTORY,&pDesktop)))
  28.     return;
  29.   if(!SHGetPathFromIDList(pDesktop, DesktopDir))
  30.   {
  31.     ShellMalloc->Free(pDesktop);
  32.     ShellMalloc->Release();
  33.     return;
  34.   }
  35.   ShellMalloc->Free(pDesktop);
  36.   ShellMalloc->Release();
  37.   if(SUCCEEDED(CoInitialize(NULL)))
  38.   {
  39.     if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,CLSCTX_INPROC_SERVER,IID_IShellLink, (void **) &pLink)))
  40.     {
  41.       pLink->SetPath(file.c_str());
  42.       ll=file.Length();
  43.       for(ii=ll;ii>0;ii--)
  44.       {
  45.         ass=file.SubString(ii,1);
  46.         if(ass=="\")break;
  47.       }
  48.       ass=file.SubString(1,ii-1);
  49.       pLink->SetWorkingDirectory(ass.c_str());
  50.       pLink->SetDescription((name+" shortcut").c_str());
  51.       pLink->SetShowCmd(SW_SHOW);
  52.       if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,(void **)&pPersistFile)))
  53.       {
  54.         WideString strShortCutLocation(DesktopDir);
  55.         strShortCutLocation += "\"+name+".lnk";
  56.         pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
  57.         pPersistFile->Release();
  58.       }
  59.       pLink->Release();
  60.     }
  61.     CoUninitialize();
  62.   }
  63. }
  64. //----------------------------------------------------------------------
  65. void __fastcall TForm1::CreateDeskTopB(AnsiString ProgGroup,AnsiString ProgItem)
  66. {
  67.   AnsiString WinDir,ass_dst,ass_src;
  68.   char buff[MAX_PATH];
  69.   GetWindowsDirectory(buff,MAX_PATH);
  70.   WinDir=buff;
  71.   ass_src =WinDir+"\Start Menu\Programs\"+ProgGroup+"\"+ProgItem+".lnk";
  72.   ass_dst =WinDir+"\DESKTOP\"+ProgItem+".lnk";
  73.   CopyFile(ass_src.c_str(),ass_dst.c_str(),False);
  74. }
  75. //----------------------------------------------------------------------
  76. void __fastcall TForm1::Button1Click(TObject *Sender)
  77. {
  78.   AnsiString path;
  79.   path=GetCurrentDir();
  80.   path=path+"\"+ExtractFileName(Application->ExeName);
  81.   CreateDeskTopA(path,"我的桌面程序");
  82. }
  83. //---------------------------------------------------------------------------
  84. void __fastcall TForm1::Button2Click(TObject *Sender)
  85. {
  86.   CreateDeskTopB("附件","记事本");
  87. }
  88. //---------------------------------------------------------------------------