Unit1.cpp
上传用户:lhxd_sz
上传日期:2014-10-02
资源大小:38814k
文件大小:3k
- //---------------------------------------------------------------------------
- #include <shlobj.h>
- #include <vcl.h>
- #pragma hdrstop
- #include "Unit1.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::CreateDeskTopA(AnsiString file,AnsiString name)
- {
- AnsiString ass;
- int ii,ll;
- IShellLink* pLink;
- IPersistFile* pPersistFile;
- LPMALLOC ShellMalloc;
- LPITEMIDLIST pDesktop;
- char DesktopDir[MAX_PATH];
- if(FAILED(SHGetMalloc(&ShellMalloc)))
- return;
- if(FAILED(SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOPDIRECTORY,&pDesktop)))
- return;
- if(!SHGetPathFromIDList(pDesktop, DesktopDir))
- {
- ShellMalloc->Free(pDesktop);
- ShellMalloc->Release();
- return;
- }
- ShellMalloc->Free(pDesktop);
- ShellMalloc->Release();
- if(SUCCEEDED(CoInitialize(NULL)))
- {
- if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,CLSCTX_INPROC_SERVER,IID_IShellLink, (void **) &pLink)))
- {
- pLink->SetPath(file.c_str());
- ll=file.Length();
- for(ii=ll;ii>0;ii--)
- {
- ass=file.SubString(ii,1);
- if(ass=="\")break;
- }
- ass=file.SubString(1,ii-1);
- pLink->SetWorkingDirectory(ass.c_str());
- pLink->SetDescription((name+" shortcut").c_str());
- pLink->SetShowCmd(SW_SHOW);
- if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,(void **)&pPersistFile)))
- {
- WideString strShortCutLocation(DesktopDir);
- strShortCutLocation += "\"+name+".lnk";
- pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
- pPersistFile->Release();
- }
- pLink->Release();
- }
- CoUninitialize();
- }
- }
- //----------------------------------------------------------------------
- void __fastcall TForm1::CreateDeskTopB(AnsiString ProgGroup,AnsiString ProgItem)
- {
- AnsiString WinDir,ass_dst,ass_src;
- char buff[MAX_PATH];
- GetWindowsDirectory(buff,MAX_PATH);
- WinDir=buff;
- ass_src =WinDir+"\Start Menu\Programs\"+ProgGroup+"\"+ProgItem+".lnk";
- ass_dst =WinDir+"\DESKTOP\"+ProgItem+".lnk";
- CopyFile(ass_src.c_str(),ass_dst.c_str(),False);
- }
- //----------------------------------------------------------------------
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- AnsiString path;
- path=GetCurrentDir();
- path=path+"\"+ExtractFileName(Application->ExeName);
- CreateDeskTopA(path,"我的桌面程序");
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button2Click(TObject *Sender)
- {
- CreateDeskTopB("附件","记事本");
- }
- //---------------------------------------------------------------------------