I. Créer un raccourci▲
Exemple un raccourci de la calculatrice sur le bureau au clic d'un bouton :
Sélectionnez
#include
<shlobj.h>
//.........
void
__fastcall TForm1::
Button1Click(TObject *
Sender)
{
HRESULT hres;
IShellLink*
psl;
WORD wsz[MAX_PATH];
CoInitialize(NULL
);
hres =
CoCreateInstance(CLSID_ShellLink, NULL
,CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID *
)&
psl);
if
(SUCCEEDED(hres))
{
IPersistFile*
ppf;
psl->
SetPath("C:
\\
Windows
\\
calc.exe"
);
psl->
SetDescription("Calculatrice"
);
psl->
QueryInterface(IID_IPersistFile,(LPVOID *
)&
ppf);
MultiByteToWideChar(CP_ACP, 0
,"C:
\\
Windows
\\
Bureau
\\
Calc.lnk"
, -
1
,
(LPWSTR)wsz, MAX_PATH);
ppf->
Save((LPWSTR)wsz, TRUE);
ppf->
Release();
}
psl->
Release();
CoUninitialize();
}
Pour ne pas surcharger l'exemple, je n'ai pas employé les fonctions adéquates pour chercher le chemin de Windows et du bureau.
J'ai testé la compilation avec BCB6. Pour ne pas avoir les messages d'erreurs sur le fichier « shlobj.h », il faut déclarer la constante NO_WIN32_LEAN_AND_MEAN au tout début du fichier « .cpp » avant #include <vcl.h>
Sélectionnez
#define NO_WIN32_LEAN_AND_MEAN
#include
<vcl.h>
#include
<shlobj.h>
#pragma hdrstop
//.......