|
|
Create A Shortcut On Your Desktop 1. Open Borland C++ Builder. 2. Place a Button on the form. 3. Place an Edit box on the form. 4. Place another Edit box on the form. 5. Place a Label on the form. 6. Place an OpenDialog on the form (found under the Dialogs tab). 7. Double click the Button1. 8. Type the following red code in the Button1Click function: //This is what the Button1Click function should look like: void __fastcall TForm1::Button1Click(TObject *Sender) { { Label1->Caption = OpenDialog1->FileName; CreateShortCut(Form1->Label1->Caption, Form1->Edit1->Text, Form1->Edit2->Text); } //--------------------------------------------------------------------------- 9. Place this function prototype near the top of Unit1.cpp: BOOL CreateShortCut(const AnsiString &file, AnsiString Desc, AnsiString SName); 10. Go to the bottom of Unit1.cpp and add this function: BOOL CreateShortCut(const AnsiString &file, AnsiString Desc, AnsiString SName) { IShellLink* pLink; IPersistFile* pPersistFile; LPMALLOC ShellMalloc; LPITEMIDLIST DesktopPidl; char DesktopDir[MAX_PATH]; char szDeskTopFolder[MAX_PATH]; if(FAILED(SHGetMalloc(&ShellMalloc))) return 0; if(FAILED(SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOPDIRECTORY, &DesktopPidl))) return 0; if(!SHGetPathFromIDList(DesktopPidl, DesktopDir)) { ShellMalloc->Free(DesktopPidl); ShellMalloc->Release(); return 0; } 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(Desc.c_str()); pLink->SetShowCmd(SW_SHOW); if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile, (void **)&pPersistFile))) { wchar_t WC[256]; SHGetSpecialFolderPath(NULL, szDeskTopFolder, CSIDL_DESKTOP, FALSE); strcat(szDeskTopFolder, "\\"); AnsiString strShortCutLocation(szDeskTopFolder); strShortCutLocation+=SName+=".lnk"; MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, strShortCutLocation.c_str(), 256, WC, 256); pPersistFile->Save(WC, TRUE); pPersistFile->Release(); } pLink->Release(); } CoUninitialize(); } return 1; } //--------------------------------------------------------------------------- 11. Note: you will need to include: #include < shlobj.h > at hte top of Unit1.cpp 12. Run the program and type the shortcut description in Edit1 and the shortcut name in Edit2. Click Button1 and browse to a file. After selecting a file, a shortcut to the selected file will be placed on the desktop with the information from Edit1 and Edit2. |