Create shortcuts with BCB in Windows desktop

zhaozj2021-02-16  52

Create shortcuts with BCB in Windows desktop

The API provides a COM interface called ISHELLLINK Allows us to create shortcuts. To create shortcuts at the desktop, we create an ishellLink object, set its properties, then save this Link to the Desktop directory.

The following example code demonstrates how to create a shortcut. In this example, this shortcut is saved in the C: / Drive directory.

/ / -------------------------------------------------------------------------------------------- ---------------------- Include void __fastcall tform1 :: button1click (tobject * sender) {if (OpenDialog1-> Execute ()) Createshortcut (OpenDialog1-> filename);} // ---------------------------------------- ------------------------------ Void TForm1 :: CreateShortcut (const aion) {ishelllink * plink; ipersistfile * ppersistfile; if the 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: // bcbsho Rtcut.lnk "); pPersistfile-> save (strshortcutlocation.c_bs (), true); pPERSISTFILE-> Release ();} PLINK-> Release ();} Couninitialize ();}} // ------ -------------------------------------------------- -------------- The above example is just to save the shortcut file to the C: / DRIVE directory, but not saved to the Desktop directory.

To make the shortcut appear on the desktop, you only need to save the shortcut file to the Desktop directory. First we have to find Windows Desktop directory. Once we know the directory where Desktop is located, we can save the shortcut file to the Desktop directory. Then Windows can display the shortcut icon on the desktop. Here is an improved example:

/ / -------------------------------------------------------------------------------------------- ---------------------- 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 {ShellMalloc-> Free (DesktopPidl) (SHGetPathFromIDList (DesktopPidl, DesktopDir)!); 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) (IID_IPERSISTFILE, (Void) **) & pPersistFile))) {WideString strShortCutLocation (DesktopDir); strShortCutLocation = "//bcbshortcut.lnk"; pPersistFile-> Save (strShortCutLocation.c_bstr (), TRUE); pPersistFile-> Release ();} pLink-> Couninitialize ();

}} // ---------------------------------------------- ------------------------ Do not trap in a mud in COM, create shortcuts including some to COM. Don't let you fall into the complexity of COM. COM is just a way to create and use the object. In this example we can consider not using COM but use equivalent C technology. COM code C psuedo-equivalentIShellLink * 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 (link); pPERSISTFILE-> Save ("c: //", true); persistfile-> save ("c: //"); pPERSISTFILE-> Release (); delete PersistFilePlink -> Releas e (); delete link; couninitialize ();

转载请注明原文地址:https://www.9cbs.com/read-18889.html

New Post(0)